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