all repos — Legends-RPG @ 9f06f3e87d4e17cfc5237ecb438224bc376b95ed

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.ENEMY_DEAD: self.enemy_dead,
 40                      c.SWITCH_ENEMY: self.switch_enemy,
 41                      c.PLAYER_ATTACK: self.player_attack,
 42                      c.ATTACK_ANIMATION: self.enemy_damaged,
 43                      c.RUN_AWAY: self.try_to_run_away,
 44                      c.FLEE: self.flee,
 45                      c.BATTLE_WON: self.battle_won,
 46                      c.ENEMY_ATTACK_DAMAGE: self.player_damaged,
 47                      c.DRINK_HEALING_POTION: self.drink_healing_potion,
 48                      c.DRINK_ETHER_POTION: self.drink_ether_potion,
 49                      c.CURE_SPELL: self.cure_spell,
 50                      c.FIRE_SPELL: self.fire_spell}
 51
 52        return event_dict
 53
 54    def on_notify(self, event):
 55        """
 56        Notify Observer of event.
 57        """
 58        if event in self.event_dict:
 59            self.event_dict[event]()
 60
 61    def end_battle(self):
 62        """
 63        End Battle and flip to previous state.
 64        """
 65        self.level.end_battle()
 66
 67    def select_action(self):
 68        """
 69        Set components to select action.
 70        """
 71        self.level.state = c.SELECT_ACTION
 72        self.arrow.index = 0
 73        self.arrow.state = c.SELECT_ACTION
 74        self.arrow.image = setup.GFX['smallarrow']
 75        self.info_box.state = c.SELECT_ACTION
 76
 77    def select_enemy(self):
 78        self.level.state = c.SELECT_ENEMY
 79        self.arrow.index = 0
 80        self.arrow.state = c.SELECT_ENEMY
 81
 82    def select_item(self):
 83        self.level.state = c.SELECT_ITEM
 84        self.info_box.state = c.SELECT_ITEM
 85        self.arrow.become_select_item_state()
 86
 87    def select_magic(self):
 88        self.level.state = c.SELECT_MAGIC
 89        self.info_box.state = c.SELECT_MAGIC
 90        self.arrow.become_select_magic_state()
 91
 92    def enemy_attack(self):
 93        enemy = self.level.enemy_list[self.level.enemy_index]
 94        enemy.enter_enemy_attack_state()
 95        self.info_box.state = c.ENEMY_ATTACK
 96
 97    def switch_enemy(self):
 98        """Switch which enemy is attacking player."""
 99        if self.level.enemy_index < len(self.level.enemy_list) - 1:
100            self.level.enemy_index += 1
101            self.on_notify(c.ENEMY_ATTACK)
102
103    def player_damaged(self):
104        if self.enemy_index > len(self.enemy_list) - 1:
105            self.enemy_index = 0
106        enemy = self.enemy_list[self.enemy_index]
107        player_damage = enemy.calculate_hit()
108        self.level.damage_points.add(
109            attackitems.HealthPoints(player_damage,
110                                     self.player.rect.topright))
111        self.info_box.set_player_damage(player_damage)
112        self.info_box.state = c.DISPLAY_ENEMY_ATTACK_DAMAGE
113        self.level.state = c.DISPLAY_ENEMY_ATTACK_DAMAGE
114        self.level.set_timer_to_current_time()
115        self.level.player_damaged(player_damage)
116        if player_damage:
117            self.player.damaged = True
118            self.player.enter_knock_back_state()
119
120    def player_attack(self):
121        enemy_posx = self.arrow.rect.x + 60
122        enemy_posy = self.arrow.rect.y - 20
123        enemy_pos = (enemy_posx, enemy_posy)
124        enemy_to_attack = None
125
126        for enemy in self.enemies:
127            if enemy.rect.topleft == enemy_pos:
128                enemy_to_attack = enemy
129
130        self.player.enter_attack_state(enemy_to_attack)
131        self.arrow.become_invisible_surface()
132
133    def enemy_damaged(self):
134        """
135        Make an attack animation over attacked enemy.
136        """
137        enemy_damage = self.player.calculate_hit()
138        self.level.damage_points.add(
139            attackitems.HealthPoints(enemy_damage,
140                                     self.player.attacked_enemy.rect.topright))
141
142        self.info_box.set_enemy_damage(enemy_damage)
143        self.info_box.state = c.ENEMY_HIT
144
145        self.arrow.state = c.SELECT_ACTION
146        self.arrow.index = 0
147        self.level.attack_enemy(enemy_damage)
148        self.level.set_timer_to_current_time()
149        self.level.state = c.ENEMY_HIT
150
151
152    def try_to_run_away(self):
153        """
154        Sets a flag in the battle state indicating a desire to run.
155        Allows enemies to get one last hit in.
156        """
157        self.level.run_away = True
158        self.level.state = c.ENEMY_ATTACK
159        self.arrow.become_invisible_surface()
160        self.level.enemy_index = 0
161        self.on_notify(c.ENEMY_ATTACK)
162
163    def flee(self):
164        """
165        Actually run away after enemies get there chance to attack.
166        """
167        self.info_box.state = c.RUN_AWAY
168        self.level.set_timer_to_current_time()
169        self.arrow.become_invisible_surface()
170        self.player.state = c.RUN_AWAY
171
172
173    def battle_won(self):
174        self.info_box.state = c.BATTLE_WON
175        self.level.set_timer_to_current_time()
176        self.player.state = c.VICTORY_DANCE
177
178
179    def drink_healing_potion(self):
180        """
181        Give player a healing potion.
182        """
183        self.level.drink_healing_potion()
184
185    def drink_ether_potion(self):
186        """
187        Give player a ether potion.
188        """
189        self.level.drink_ether()
190
191    def cure_spell(self):
192        """
193        Cast cure spell on player.
194        """
195        self.level.cast_cure()
196
197    def fire_spell(self):
198        """
199        Cast fire spell on all enemies.
200        """
201        self.level.cast_fire_blast()
202
203    def enemy_dead(self):
204        """
205        Eliminate all traces of enemy.
206        """
207        self.player.attacked_enemy = None
208
209
210
211
212
213
214
215
216