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