当前位置: 首页>>代码示例>>Python>>正文


Python Schedule.add_action方法代码示例

本文整理汇总了Python中schedule.Schedule.add_action方法的典型用法代码示例。如果您正苦于以下问题:Python Schedule.add_action方法的具体用法?Python Schedule.add_action怎么用?Python Schedule.add_action使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在schedule.Schedule的用法示例。


在下文中一共展示了Schedule.add_action方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Npc

# 需要导入模块: from schedule import Schedule [as 别名]
# 或者: from schedule.Schedule import add_action [as 别名]
class Npc(object):
    SLEEP_DURATION = 7 * 60
    DEFAULT_FOOD_CONSUMPTION = 1 #amount of food needed per unit of time (now minute...)

    def __init__(self, occupation, name="Unknown"):
        self.occupation = occupation
        self.occupation.npc = self
        self.schedule = Schedule()
        #TODO: Cannot think... just advance schedule so that it is already done when Advance is called the first time...
        self.schedule.advance(Schedule.MAX_TIME)
        self.possession = Possession()
        self.hungerLevel = 24 * 60 #enough for one day
        self.food_consumption = Npc.DEFAULT_FOOD_CONSUMPTION
        self.alive = True
        self.strategy = None
        self.city = None
        self.x = 100
        self.y = 100
        self.home_x = 0
        self.home_y = 0
        self.name = name

    def print_status(self):
        if not self.alive:
            print "Npc is DEAD!"
        print "Npc (" + str(self.occupation) + ") has " + str(self.possession.money) + " money, owns:"
        for pos in self.possession.resources:
            print pos

    #TODO: How to handle time advancing. Now advancing goes fine if the time interval is smalles possible.
    #If the interval is increased, first schedule is advanced and then food. This leads to not wanted scenarios
    #where npc can do work without food.
    def advance(self, time):
        while (time > 0):
            if not self.alive:
                return

            #Day is completed, create new schedule
            if (self.schedule.is_done()):
                self.create_schedule()
            
            timeLeft = self.schedule.advance(time) 
            self._consume_food(time - timeLeft)
            time = timeLeft
 
    def create_schedule(self):
        self.schedule = Schedule()
        if self.strategy == None:
            self._add_mandatory_actions()
            self.occupation.add_default_schedule(self, self.possession)
        else:
            self._add_mandatory_actions()
            self.strategy.create_schedule()

    def _consume_food(self, time):
        while (time > 0):
            if (self.hungerLevel <= 0):
                foods = self.possession.get_foods()
                if len(foods) == 0:
                    self.alive = False
                    print "NPC (" + str(self.occupation) + ") died from hunger!"
                    return
                #just eat the first thing from the inventory...
                self.possession.destroy_resource(foods[0])
                self.hungerLevel += foods[0].NUTRITIONAL_VALUE
            consumedAmount = min(time, self.hungerLevel)
            self.hungerLevel -= consumedAmount * self.food_consumption            
            time -= consumedAmount

    def _add_mandatory_actions(self):
        self.schedule.add_action(Action(self, "Sleep", Npc.SLEEP_DURATION))
开发者ID:timoruokonen,项目名称:latrappe,代码行数:73,代码来源:npc.py


注:本文中的schedule.Schedule.add_action方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。