Пример #1
0
    def on_move_player(self, data):
        delta = data.get("delta", None)

        if delta is None:
            return

        # get the requested position
        player = entity_system.find_one("PlayerComponent")
        x, y = player.get("RenderComponent", "pos")
        dx, dy = delta
        pos = (x + dx, y + dy)

        # if the new spot isn't passible, ignore the command
        if not self.map.get(pos).passible:
            return

        # if the new spot is occupied, ignore the command
        blocker = entity_system.find_one("NpcComponent", {"RenderComponent#pos": pos})
        if blocker is not None:
            return

        player.set("RenderComponent", "pos", pos)
        fov_radius = player.get("UnitComponent", "fov_radius")
        self.map.get_fov_tiles(pos, fov_radius).seen = True
        inject_user_event("end_turn")
Пример #2
0
    def on_end_turn(self, data):
        # reset the action count
        self.actions = 1

        # clear the target component, if necessary
        if self.target is not None:
            self.target.remove_component("TargetComponent")
            self.target = None

        # check for killed npcs
        for npc in entity_system.find("NpcComponent"):
            if npc.get("UnitComponent", "cur_hp") > 0:
                continue

            inject_user_event("npc_killed", npc=npc)
            entity_system.remove(npc)

        # recharge any npc hitpoints
        for npc in entity_system.find("NpcComponent"):
            npc.get_component("UnitComponent").do_hp_recharge()

        # send an event on player death
        player = entity_system.find_one("PlayerComponent")
        if player.get("UnitComponent", "cur_hp") < 0:
            inject_user_event("player_killed")

        # recharge the player's heath
        player = entity_system.find_one("PlayerComponent")
        player.get_component("UnitComponent").do_hp_recharge()

        # refill the player's hand
        player.get("UnitComponent", "deck").fill_hand()

        inject_user_event("map_changed")
Пример #3
0
    def on_map_changed(self, data):
        self.image.fill(MapSprite.BLACK)

        # move the camera to the player
        player = entity_system.find_one("PlayerComponent")
        pos = player.get("RenderComponent", "pos")
        fov_radius = player.get("UnitComponent", "fov_radius")
        
        # move the camera to follow the player
        self.camera.move((pos[0] + 5, pos[1]))

        # render the tiles
        rect = self.camera.rect
        tiles = self.map.get_area(rect.left, rect.top, rect.width, rect.height).filter("seen", True)    
        tiles_fov = self.map.get_fov_tiles(pos, fov_radius)
        for tile in tiles:
            visible = tile in tiles_fov
            self._render_tile(self.image, tile, visible)

        # render the npcs
        npcs = entity_system.find("NpcComponent")
        for npc in npcs: 
            tile = self.map.get(npc.get("RenderComponent", "pos"))         
            if tile in tiles_fov:
                self._render_unit(self.image, npc)

        # render player
        self._render_unit(self.image, player)
Пример #4
0
    def do_update(self):
        player = entity_system.find_one("PlayerComponent")

        # setup the widgets
        do_button = Button()
        do_progress_bar = ProgressBar()
        do_label = Label()
        
        # Hit Points
        do_label(self, (10, 10), 'HP')
        cur_hp = player.get('UnitComponent', 'cur_hp') * 1.0 
        max_hp = player.get('UnitComponent', 'max_hp')
        do_progress_bar(self, (10, 30), (230, 22), cur_hp / max_hp)
        
        # Action Cards
        do_label(self, (10, 70), 'Action Cards')
        ctr = 0
        deck = player.get('UnitComponent', 'deck')
        for card in deck.hand:
            top = 90 + (30 * ctr)
            id = "card_button_%s" % ctr
            
            if do_button(self, id, (10, top), (230, 22), card.name):
                inject_user_event('action_card', card_num=ctr)
            
            ctr += 1
Пример #5
0
 def on_mouse_up(self, data):
     sprite = data.get('sprite', None)
     if sprite is not self:
         return
     
     pos = data.get('pos', None)
     array_pos = self.camera.to_array(pos)
     
     # if there is an entity under the cursor, it needs to be targeted
     entity = entity_system.find_one('RenderComponent', conditions={'RenderComponent#pos': array_pos})
     if entity:
         inject_user_event('select_target', target=entity)
Пример #6
0
    def on_action_card(self, data):
        index = data.get('card_num', None)
        if index is None:
            return

        # get the card from the player's hand
        player = entity_system.find_one('PlayerComponent')
        deck = player.get('UnitComponent', 'deck')
        card = deck.hand[index]

        # general data passed into all commands
        data = {
            'controller': self,
            'map': self.map,
            'source': player,
            'target': self.target,
        }

        # generate the card commands
        commands = []
        for name, kwargs in card.commands.items():
            # add any properties from the card definition to the general data
            card_data = data.copy()
            if kwargs is not None:
                card_data.update(kwargs)

            command = command_mapping[name](**card_data)
            commands.append(command)

        # make sure all the card commands are valid
        valid = True
        for command in commands:
            # if the command is not valid, notify the user of it and stop processing the card
            if not command.is_valid():
                valid = False
                inject_user_event('show_notification', text=command.msg)
                return

        # execute the card
        for command in commands:
            print "Command: %s" % command
            command.execute()

        # discard the card
        deck.discard(card)

        # note that we finished an action
        self.actions -= 1

        # if we have no more actions, or no more cards in our hand, end the turn
        if self.actions <= 0 or len(deck.hand) == 0:
            inject_user_event('end_turn')