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