Exemple #1
0
 def __init__(self, player_name, starting_room):
     self.player_name = player_name
     self.economy = PlayerEconomy()
     self.units = self._starting_units(starting_room)
Exemple #2
0
class Player(object):
    def __init__(self, player_name, starting_room):
        self.player_name = player_name
        self.economy = PlayerEconomy()
        self.units = self._starting_units(starting_room)

    def _starting_units(self, starting_room):
        worker_list = [
            SlowWorkerUnit(
                player=self,
                room=starting_room
            ) for _ in range(5)
        ]
        building_list = [
            TownHall(
                player=self,
                room=starting_room
            )
        ]

        starting_units = dict(enumerate(
            building_list + worker_list
        ))
        return starting_units

    def add_unit(self, unit):
        self.units.update(
            {len(self.units): unit}
        )  # nasty

    @staticmethod
    def _display_units(units):
        for unit_id, unit in units.iteritems():
            print('[{}], unit: {}'.format(
                unit_id, unit
            ))

    def display_all_units(self):
        self._display_units(self.units)

    def display_worker_units(self):
        self._display_units(filter(
            lambda unit: isinstance(unit, Worker),
            self.units
        ))

    def display_attacker_units(self):
        self._display_units(filter(
            lambda unit: isinstance(unit, Attacker),
            self.units
        ))

    def input_orders(self):
        print("Player {}'s turn".format(self.player_name))
        self.economy.display_economy()
        self.display_all_units()
#        print(
#            'Enter integer for changing action. '
#            'Enter "done" when you are done with actions'
#        )
        done = False
        while not done:
            input_ = insist_get_input(
                    '>>>',
                    allowed=self.units.keys()+['done', 'debug']
                )
            if input_ == 'done':
                done = True
            elif input_ == 'debug':
                import ipdb; ipdb.set_trace()
                self.economy.display_economy()
                self.display_all_units()
            else:
                self.units[input_].change_order()
                self.input_orders()  # FIXME Horrible
                done = True

    def execute_orders(self):
        for unit in self.units.values():
            unit.execute_order()

    def update_states(self):
        sorted_unit_list = filter(
            lambda unit: unit.alive,
            sorted(
                self.units.values(),
                key=lambda unit: unit.priority
            )
        )
        dead_units = filter(
            lambda unit: not unit.alive,
            self.units.values()
        )  # not sorted
        for dead_unit in dead_units:
            dead_unit.room.remove_unit_from_room(dead_unit)

        self.units = {
            unit_id: unit for (unit_id, unit) in enumerate(
                sorted_unit_list
            )
        }

    def __str__(self):
        return '{}({})'.format(
            self.__class__.__name__,
            self.player_name
        )