def test_skill_check(): wins = 0 iters = 1000 em = EntityManager() em.set_component(em.pc, SkillComponent()) skills = em.get_component(em.pc, SkillComponent) # 7 has ~41% odds of success skills.skills['melee.swords.dagger'] = SkillSubComponent(7) sys = SkillSystem() sys.set_entity_manager(em) event.register(sys) for x in range(iters): ch = SkillCheckEvent(em.pc, "melee.swords.dagger") event.dispatch(ch) if ch.result[2]: wins += 1 # Use a fudge factor, because random assert wins >= (iters * 0.38) assert wins <= (iters * 0.44)
def explore(self): if not self._explored: self._explored = True if self.feature: msg = "You have found a {feature}!".format( feature=self.feature.name) event.dispatch(MessageEvent(msg))
def move_handler(self, myevent): pos = self._entity_manager.get_component(self._entity_manager.pc, PositionComponent) pos.x += myevent.dx pos.y += myevent.dy if myevent.dx and myevent.dy: # Diagonal movement costs 1,400 Fatigue fatigue = 1400 else: # Orthoganal movement costs 1,000 Fatigue fatigue = 1000 event.dispatch(ActionCompleteEvent(myevent.entity, fatigue))
def execute(self): # Can't kill an entity while iterating, so stash a list of deadites deadites = [] for entity, components in self._entity_manager.get_entities_with_component( HealthComponent): health = components[HealthComponent] if health.hp <= 0: deadites.append(entity) for entity in deadites: self._entity_manager.destroy_entity(entity) event.dispatch(MessageEvent("{} dies!".format(entity)))
def _init_pc(self): # Get a random place to put the PC x,y = self._world.current_map.get_random_cell() pc_pos = components.PositionComponent(x, y) pc_sprite = components.SpriteComponent('@') self._entities.set_component(self._entities.pc, pc_pos) self._entities.set_component(self._entities.pc, pc_sprite) self._entities.set_component(self._entities.pc, components.FoVComponent()) self._entities.set_component(self._entities.pc, components.FatigueComponent()) self._entities.set_component(self._entities.pc, components.AttributesComponent(15,13,11,9)) pc_skills = components.SkillsComponent() pc_skills['melee.swords.short_sword'] = components.SkillsComponent.Skill(12) self._entities.set_component(self._entities.pc, pc_skills) event.dispatch(SpawnEntityEvent(self._entities.pc))
def dmg_handler(self, dmg_event): try: health = self._entity_manager.get_component(dmg_event.defender, HealthComponent) except exceptions.NoComponentForEntityError: # TODO How do we want to handle damaging Entities with no health? return dmg = 0 # Different damage types have different multipliers; apply those now dmg += dmg_event.dmg.piercing * 0.8 dmg += dmg_event.dmg.bludgeoning * 1.0 dmg += dmg_event.dmg.slashing * 1.5 dmg += dmg_event.dmg.impaling * 2.0 # Round down any fractions dmg = int(dmg) # Now reduce damage from HP health.hp -= dmg # TODO: Temporary debugging log message event.dispatch(MessageEvent("You dealt {} damage to it!".format(dmg)))
def map_handler(self, myevent): epos = self._dm._wm._em.get_component(myevent.entity, PositionComponent) if myevent.__class__ == MoveEvent: tx = epos.x + myevent.dx ty = epos.y + myevent.dy if not self.map.tiles[tx][ty].passable: # Illegal move, prevent this event from continuing myevent.stop() if self.map.tiles[tx][ty].feature == features.closed_door: # Stopped the movement, but we can open this door event.dispatch(OpenDoorEvent(myevent.entity, tx, ty)) elif myevent.__class__ == ClimbDownEvent: if self.map.tiles[epos.x][epos.y] != features.stairs_down: # Can't descend without stairs, dummy! myevent.stop() elif myevent.__class__ == ClimbUpEvent: if self.map.tiles[epos.x][epos.y] != features.stairs_up: # Can't ascend without stairs, dummy! myevent.stop()
def execute(self): """Wait for player input, dispatching appropriate events.""" pc = self._entity_manager.pc pc_fatigue = self._entity_manager.get_component(pc, FatigueComponent) if pc_fatigue.fatigue > 0: # PC's still fatigued, need to wait until they can act return event.dispatch(PreInputEvent()) key = self.get_keypress() if key == libtcod.KEY_ESCAPE or libtcod.console_is_window_closed(): event.dispatch(QuitEvent()) #exit game # Movement keys if key in self.MOVEMENT_KEYS: event.dispatch(MoveEvent(pc, *self.MOVEMENT_KEYS[key])) elif key == '>': event.dispatch(ClimbDownEvent(pc)) elif key == '<': event.dispatch(ClimbUpEvent(pc))
def open_door_handler(self, openevent): # Change the door Feature to an open door x, y = openevent.x, openevent.y self._world.current_map.tiles[x][y].add_feature(features.open_door) # Dispatch a new message event.dispatch(MessageEvent("You open the door")) # We've changed the map, signal that event.dispatch(MapChangedEvent()) # Now dispatch the ActionCompleteEvent; 1000 Fatigue to open a door event.dispatch(ActionCompleteEvent(openevent.entity, 1000))