all repos — Legends-RPG @ 4b9f2008aa45d1c1cfd5b0f335eca68b8313b26f

A fantasy mini-RPG built with Python and Pygame.

data/observer.py (view raw)

  1"""
  2Module for all game observers.
  3"""
  4from . import constants as c
  5from .components import attack
  6from . import setup
  7
  8class Battle(object):
  9    """
 10    Observes events of battle and passes info to components.
 11    """
 12    def __init__(self, level):
 13        self.level = level
 14        self.info_box = level.info_box
 15        self.select_box = level.info_box
 16        self.arrow = level.arrow
 17        self.player = level.player
 18        self.enemies = level.enemy_group
 19        self.event_dict = self.make_event_dict()
 20
 21    def make_event_dict(self):
 22        """
 23        Make a dictionary of events the Observer can
 24        receive.
 25        """
 26        event_dict = {c.END_BATTLE: self.end_battle,
 27                      c.SELECT_ACTION: self.select_action,
 28                      c.SELECT_ITEM: self.select_item,
 29                      c.SELECT_ENEMY: self.select_enemy,
 30                      c.SELECT_MAGIC: self.select_magic,
 31                      c.ENEMY_ATTACK: self.enemy_attack,
 32                      c.PLAYER_ATTACK: self.player_attack,
 33                      c.ATTACK_ANIMATION: self.attack_animation,
 34                      c.RUN_AWAY: self.run_away,
 35                      c.BATTLE_WON: self.battle_won,
 36                      c.PLAYER_FINISHED_ATTACK: self.player_finished_attack}
 37
 38        return event_dict
 39
 40    def on_notify(self, event):
 41        """
 42        Notify Observer of event.
 43        """
 44        if event in self.event_dict:
 45            self.event_dict[event]()
 46
 47    def end_battle(self):
 48        """
 49        End Battle and flip to previous state.
 50        """
 51        self.level.end_battle()
 52
 53    def select_action(self):
 54        """
 55        Set components to select action.
 56        """
 57        self.level.state = c.SELECT_ACTION
 58        self.arrow.index = 0
 59        self.arrow.state = c.SELECT_ACTION
 60        self.arrow.image = setup.GFX['smallarrow']
 61        self.info_box.state = c.SELECT_ACTION
 62
 63    def select_enemy(self):
 64        self.level.state = c.SELECT_ENEMY
 65        self.arrow.index = 0
 66        self.arrow.state = c.SELECT_ENEMY
 67
 68    def select_item(self):
 69        self.level.state = c.SELECT_ITEM
 70        self.info_box.state = c.SELECT_ITEM
 71        self.arrow.become_select_item_state()
 72
 73    def select_magic(self):
 74        self.level.state = c.SELECT_MAGIC
 75        self.info_box.state = c.SELECT_MAGIC
 76        self.arrow.become_select_magic_state()
 77
 78    def enemy_attack(self):
 79        enemy = self.level.enemy_list[self.level.enemy_index]
 80        enemy.enter_enemy_attack_state()
 81
 82    def player_attack(self):
 83        enemy_posx = self.arrow.rect.x + 60
 84        enemy_posy = self.arrow.rect.y - 20
 85        enemy_pos = (enemy_posx, enemy_posy)
 86        enemy_to_attack = None
 87
 88        for enemy in self.enemies:
 89            if enemy.rect.topleft == enemy_pos:
 90                enemy_to_attack = enemy
 91
 92        self.player.enter_attack_state(enemy_to_attack)
 93        self.arrow.become_invisible_surface()
 94
 95    def attack_animation(self):
 96        """
 97        Make an attack animation over attacked enemy.
 98        """
 99        self.arrow.remove_pos(self.player.attacked_enemy)
100        self.arrow.state = c.SELECT_ACTION
101        self.level.attack_enemy()
102        self.info_box.state = c.ENEMY_HIT
103
104    def run_away(self):
105        self.level.end_battle()
106
107    def battle_won(self):
108        self.level.end_battle()
109
110    def player_finished_attack(self):
111        pass