all repos — Legends-RPG @ ac0b622d09ad6f6bc948f4a6ed8f83fb7ad3b60c

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