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