def process(self, dt): for ent, health in self.world.get_component(Health): if health.amount < health.maximum and not health.effects: if health.regen_timeout: health.regen_timeout -= dt if health.regen_timeout < 0: health.amount += 1 health.regen_timeout = 0 else: health.regen_timeout = 10 for effect in list(health.effects): health.regen_timeout = 0 if not self.world.has_component(ent, Invulnerable): health.amount -= effect.amount if self.world.has_component(ent, Player): self.world.add_component(ent, Invulnerable(1)) if not self.world.has_component(ent, SpriteEffects): self.world.add_component(ent, SpriteEffects()) effects = self.world.component_for_entity( ent, SpriteEffects) effects.effects.append( SpinEffect(play_time=0.3, speed=1000)) effects.effects.append( FlashEffect(play_time=1, speed=10000)) create_sound(self.world, HURT_SOUND) health.effects.remove(effect) if health.amount <= 0: dispatch(self.world, ENTITY_DIED, ent)
def process(self, dt): # collisions will have a complete bidirectional map of entity -> collisions # # (2 entries per collision) collisions = {} collidables = [] # filter to active level for e, (ca, position) in self.world.get_components(Collidable, Position): level = self.world.component_for_entity(position.level, Level) if level.active: collidables.append((e, (ca, position))) # detection for ea, (ca, position) in collidables: for eb, cb in collidables: if ea == eb: continue if does_collide(self.world, ea, ca, eb, cb): # print(f"found collision! {ca} {cb}") collisions.setdefault(ea, []).append(eb) # resolution for ea, collisions in collisions.items(): for eb in collisions: # print(f"collision! {ea} {eb}") dispatch(self.world, COLLISION, (ea, eb))
def use_slot(world, message): inventory = world.component_for_entity(message.payload["player_ent"], Inventory) item_ent = inventory.item_ents[message.payload["index"]] if item_ent is None: return dispatch( world, USE_ITEM, dict(player_ent=message.payload["player_ent"], item_ent=item_ent), )
def on_update(self, dt): if self.first_update or not self.menu.show: self.world.process(dt) self.first_update = False for player_ent, p in self.world.get_component(Player): while p.input_source.state.events: dispatch( self.world, INPUT, dict( player_ent=player_ent, input=p.input_source.state.events.pop(0), ), ) self.menu.update(dt)
def collision(world, message): source, dest = message.payload if not world.has_component(source, Button) or world.has_component( source, Timeout): return button = world.component_for_entity(source, Button) if button.down_state: return button.down_state = True level_ent = None if button.in_level: level_ent = world.component_for_entity(source, Position).level dispatch(world, CHANNEL, (source, dest, Channel(button.channel, level_ent))) world.add_component(source, Timeout(1)) create_sound(world, BUTTON_SOUND, volume=0.5)
def process(self, dt): # Hmm, this is a singleton, we don't need it to be an entity with a component lp = self.world.get_component(LevelProgression) if not lp: return _, current_level = lp[0] if not current_level.level_ent: return current_level_name = self.world.component_for_entity( current_level.level_ent, Level).name for _, level in self.world.get_component(Level): if level.tmx_mtime < map_mtime(level.name): # print(f"RELOAD {level.tmx_mtime} < {map_mtime(level.name)}") dispatch(self.world, RELOAD_MAPS, current_level_name) return
def update(self, dt): for _, input_source in self.world.get_component(InputSource): if input_source.state.get(MENU): self.show = True if self.show: self.joystick_check += dt menu_activator = None if self.joystick_check > JOYSTICK_CHECK_FREQUENCY: check_joysticks(self.world) self.joystick_check = 0 for e, source in self.world.get_component(InputSource): source.state.update() events = source.state.events for event in list(events): if event.input == DOWN: self.highlight_button(1) events.remove(event) if event.input == UP: self.highlight_button(-1) events.remove(event) if event.input == ACTIVATE and self.selected_button is not None: menu_activator = source arcade_button = self.buttons[BUTTONS[self.selected_button]] arcade_button.pressed = True arcade_button.hovered = False self.selected_button = None events.remove(event) if self.buttons[BUTTON_PLAY].pressed: self.show = False if menu_activator is None: for _, input_source in self.world.get_component(InputSource): if input_source.name == "Keyboard": menu_activator = input_source self.spawn_player(menu_activator) self.buttons[BUTTON_PLAY].pressed = False if self.buttons[BUTTON_RESTART].pressed: dispatch(self.world, RESTART_GAME) self.buttons[BUTTON_RESTART].pressed = False if self.buttons[BUTTON_EXIT].pressed: arcade.close_window()
def handle_input(world, message): input_event = message.payload["input"] player_ent = message.payload["player_ent"] # CONVERT ITEM_N input into activate item N if input_event.input in ITEM_INVENTORY_INDEX: input_source = world.component_for_entity(player_ent, Player).input_source message_type = USE_SLOT if input_source.state.get(DROP): message_type = DROP_ITEM dispatch( world, message_type, dict( player_ent=player_ent, index=ITEM_INVENTORY_INDEX.index(input_event.input), ), )