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