all repos — Legends-RPG @ 6631839e2ae03b23f4881a7a0de242c63c8dd613

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.attack_animation,
 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                      c.PLAYER_FINISHED_ATTACK: self.player_finished_attack}
 44
 45        return event_dict
 46
 47    def on_notify(self, event):
 48        """
 49        Notify Observer of event.
 50        """
 51        if event in self.event_dict:
 52            self.event_dict[event]()
 53
 54    def end_battle(self):
 55        """
 56        End Battle and flip to previous state.
 57        """
 58        self.level.end_battle()
 59
 60    def select_action(self):
 61        """
 62        Set components to select action.
 63        """
 64        self.level.state = c.SELECT_ACTION
 65        self.arrow.index = 0
 66        self.arrow.state = c.SELECT_ACTION
 67        self.arrow.image = setup.GFX['smallarrow']
 68        self.info_box.state = c.SELECT_ACTION
 69
 70    def select_enemy(self):
 71        self.level.state = c.SELECT_ENEMY
 72        self.arrow.index = 0
 73        self.arrow.state = c.SELECT_ENEMY
 74
 75    def select_item(self):
 76        self.level.state = c.SELECT_ITEM
 77        self.info_box.state = c.SELECT_ITEM
 78        self.arrow.become_select_item_state()
 79
 80    def select_magic(self):
 81        self.level.state = c.SELECT_MAGIC
 82        self.info_box.state = c.SELECT_MAGIC
 83        self.arrow.become_select_magic_state()
 84
 85    def enemy_attack(self):
 86        enemy = self.level.enemy_list[self.level.enemy_index]
 87        enemy.enter_enemy_attack_state()
 88        self.info_box.state = c.ENEMY_ATTACK
 89
 90    def switch_enemy(self):
 91        """Switch which enemy is attacking player."""
 92        if self.level.enemy_index < len(self.level.enemy_list) - 1:
 93            self.level.enemy_index += 1
 94            self.on_notify(c.ENEMY_ATTACK)
 95
 96    def display_enemy_attack_damage(self):
 97        self.info_box.state = c.DISPLAY_ENEMY_ATTACK_DAMAGE
 98        self.level.state = c.DISPLAY_ENEMY_ATTACK_DAMAGE
 99        self.level.set_timer_to_current_time()
100        self.level.player_damaged(5)
101
102    def player_attack(self):
103        enemy_posx = self.arrow.rect.x + 60
104        enemy_posy = self.arrow.rect.y - 20
105        enemy_pos = (enemy_posx, enemy_posy)
106        enemy_to_attack = None
107
108        for enemy in self.enemies:
109            if enemy.rect.topleft == enemy_pos:
110                enemy_to_attack = enemy
111
112        self.player.enter_attack_state(enemy_to_attack)
113        self.arrow.become_invisible_surface()
114
115    def attack_animation(self):
116        """
117        Make an attack animation over attacked enemy.
118        """
119        self.arrow.remove_pos(self.player.attacked_enemy)
120        self.arrow.state = c.SELECT_ACTION
121        self.arrow.index = 0
122        self.level.attack_enemy()
123        self.level.set_timer_to_current_time()
124        self.level.state = c.ENEMY_HIT
125        self.info_box.state = c.ENEMY_HIT
126
127    def run_away(self):
128        self.level.end_battle()
129
130    def battle_won(self):
131        self.level.end_battle()
132
133    def player_finished_attack(self):
134        pass