def time_till(self): """ 离下一次计算的时间 >0时说明还差n秒进行下一次计算 <0时visit会执行一次计算 :return: """ return self.last_time or 0. + self.period - now()
def visit(self): current_time = now() if self.status != RUNNING: self.wake_time = current_time + self.wait_time self.status = RUNNING else: if current_time >= self.wake_time: self.status = SUCCESS
def fire(self): shot = None for ammo in self.ammo: if ammo.type == ShotType.UNUSED: shot = ammo break if not shot: return fn = getattr(self, self.currentShotType.lower()) fn and fn(shot) shot.particle.setPosition(0.0, 1.5, 0.0) shot.startTime = now() shot.type = self.currentShotType
def visit(self): """ :return: """ # 如果没计算过,或者周期到了 do_eval = not self.last_time or not self.period or self.time_till < 0. if do_eval: # --------------------------------------------------- # 遍历子节点 # 找到第一个成功的节点 # --------------------------------------------------- old_event = None if self.index is not None: child = self.children[self.index] old_event = child if isinstance(child, EventNode) else None self.last_time = now() found = False for index, child in enumerate(self.children): should_test_anyway = old_event and isinstance(child, EventNode) and old_event.priority <= child.priority if not found or should_test_anyway: if child.status == FAILED or child.status == SUCCESS: child.reset() child.visit() cs = child.status if cs == SUCCESS or cs == RUNNING: if should_test_anyway and self.index != index: self.children[self.index].reset() self.status = cs found = True self.index = index else: child.reset() if not found: self.status = FAILED else: # --------------------------------------------------- # 运行当前节点 # --------------------------------------------------- if self.index is not None: child = self.children[self.index] if child.status == RUNNING: child.visit() self.status = child.status if self.status != RUNNING: self.last_time = None
def go_to_state(self, state_name, *params): """ / :param state_name: :param params: Table :return: """ assert state_name in self.sg.states, "TRIED TO GO TO INVALID STATE %s" % state_name from_state = self.current_state from_state and from_state.on_exit and from_state.on_exit( self.inst, state_name) to_state = self.sg.states[state_name] self.tags = set( to_state.tags) if to_state.tags else set() # 拷贝新状态的tags self.last_state, self.current_state = self.current_state, to_state to_state.on_enter and to_state.on_enter(self.inst, params) self.inst.push_event('new_state', Table(state_name=state_name)) self.state_start_time = now() self.timeline_index = 0 if to_state.timeline else None SGManager.on_enter_new_state(self)
def __str__(self): return '{:2.2f}'.format(self.wake_time - now())
def lifetime(self): return now() - self.startTime
def time_in_state(self): """ 当前状态开始到现在的时间差 :return: """ return now() - self.state_start_time