all repos — Legends-RPG @ edcd5c2abec6a2f46cf35d2023c924c837cce7ac

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 attackitems
  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        self.level.damage_points.add(
101            attackitems.DamagePoints(player_damage,
102                                     self.player.rect.topright))
103
104        self.info_box.set_player_damage(player_damage)
105        self.info_box.state = c.DISPLAY_ENEMY_ATTACK_DAMAGE
106        self.level.state = c.DISPLAY_ENEMY_ATTACK_DAMAGE
107        self.level.set_timer_to_current_time()
108        self.level.player_damaged(player_damage)
109
110    def player_attack(self):
111        enemy_posx = self.arrow.rect.x + 60
112        enemy_posy = self.arrow.rect.y - 20
113        enemy_pos = (enemy_posx, enemy_posy)
114        enemy_to_attack = None
115
116        for enemy in self.enemies:
117            if enemy.rect.topleft == enemy_pos:
118                enemy_to_attack = enemy
119
120        self.player.enter_attack_state(enemy_to_attack)
121        self.arrow.become_invisible_surface()
122
123    def enemy_damaged(self):
124        """
125        Make an attack animation over attacked enemy.
126        """
127        enemy_damage = self.player.calculate_hit()
128        self.level.damage_points.add(
129            attackitems.DamagePoints(enemy_damage,
130                                     self.player.attacked_enemy.rect.topright))
131
132        self.info_box.set_enemy_damage(enemy_damage)
133        self.info_box.state = c.ENEMY_HIT
134
135        self.arrow.state = c.SELECT_ACTION
136        self.arrow.index = 0
137        self.level.attack_enemy(enemy_damage)
138        self.level.set_timer_to_current_time()
139        self.level.state = c.ENEMY_HIT
140
141    def run_away(self):
142        self.level.end_battle()
143
144    def battle_won(self):
145        self.level.end_battle()
146