all repos — Legends-RPG @ b5733447a499f57ee5e055cff73e18dbfac21354

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.set_observer_for_enemies()
 20        self.event_dict = self.make_event_dict()
 21
 22    def set_observer_for_enemies(self):
 23        for enemy in self.enemies:
 24            enemy.observers.append(self)
 25
 26    def make_event_dict(self):
 27        """
 28        Make a dictionary of events the Observer can
 29        receive.
 30        """
 31        event_dict = {c.END_BATTLE: self.end_battle,
 32                      c.SELECT_ACTION: self.select_action,
 33                      c.SELECT_ITEM: self.select_item,
 34                      c.SELECT_ENEMY: self.select_enemy,
 35                      c.SELECT_MAGIC: self.select_magic,
 36                      c.ENEMY_ATTACK: self.enemy_attack,
 37                      c.SWITCH_ENEMY: self.switch_enemy,
 38                      c.PLAYER_ATTACK: self.player_attack,
 39                      c.ATTACK_ANIMATION: self.enemy_damaged,
 40                      c.RUN_AWAY: self.run_away,
 41                      c.BATTLE_WON: self.battle_won,
 42                      c.ENEMY_ATTACK_DAMAGE: self.display_enemy_attack_damage}
 43
 44        return event_dict
 45
 46    def on_notify(self, event):
 47        """
 48        Notify Observer of event.
 49        """
 50        if event in self.event_dict:
 51            self.event_dict[event]()
 52
 53    def end_battle(self):
 54        """
 55        End Battle and flip to previous state.
 56        """
 57        self.level.end_battle()
 58
 59    def select_action(self):
 60        """
 61        Set components to select action.
 62        """
 63        self.level.state = c.SELECT_ACTION
 64        self.arrow.index = 0
 65        self.arrow.state = c.SELECT_ACTION
 66        self.arrow.image = setup.GFX['smallarrow']
 67        self.info_box.state = c.SELECT_ACTION
 68
 69    def select_enemy(self):
 70        self.level.state = c.SELECT_ENEMY
 71        self.arrow.index = 0
 72        self.arrow.state = c.SELECT_ENEMY
 73
 74    def select_item(self):
 75        self.level.state = c.SELECT_ITEM
 76        self.info_box.state = c.SELECT_ITEM
 77        self.arrow.become_select_item_state()
 78
 79    def select_magic(self):
 80        self.level.state = c.SELECT_MAGIC
 81        self.info_box.state = c.SELECT_MAGIC
 82        self.arrow.become_select_magic_state()
 83
 84    def enemy_attack(self):
 85        enemy = self.level.enemy_list[self.level.enemy_index]
 86        enemy.enter_enemy_attack_state()
 87        self.info_box.state = c.ENEMY_ATTACK
 88
 89    def switch_enemy(self):
 90        """Switch which enemy is attacking player."""
 91        if self.level.enemy_index < len(self.level.enemy_list) - 1:
 92            self.level.enemy_index += 1
 93            self.on_notify(c.ENEMY_ATTACK)
 94
 95    def display_enemy_attack_damage(self):
 96        self.info_box.state = c.DISPLAY_ENEMY_ATTACK_DAMAGE
 97        self.level.state = c.DISPLAY_ENEMY_ATTACK_DAMAGE
 98        self.level.set_timer_to_current_time()
 99        self.level.player_damaged(5)
100
101    def player_attack(self):
102        enemy_posx = self.arrow.rect.x + 60
103        enemy_posy = self.arrow.rect.y - 20
104        enemy_pos = (enemy_posx, enemy_posy)
105        enemy_to_attack = None
106
107        for enemy in self.enemies:
108            if enemy.rect.topleft == enemy_pos:
109                enemy_to_attack = enemy
110
111        self.player.enter_attack_state(enemy_to_attack)
112        self.arrow.become_invisible_surface()
113
114    def enemy_damaged(self):
115        """
116        Make an attack animation over attacked enemy.
117        """
118        enemy_damage = self.player.calculate_hit()
119
120        self.info_box.set_enemy_damage(enemy_damage)
121        self.info_box.state = c.ENEMY_HIT
122
123        self.arrow.state = c.SELECT_ACTION
124        self.arrow.index = 0
125        self.level.attack_enemy(enemy_damage)
126        self.level.set_timer_to_current_time()
127        self.level.state = c.ENEMY_HIT
128
129    def run_away(self):
130        self.level.end_battle()
131
132    def battle_won(self):
133        self.level.end_battle()
134