data/observer.py (view raw)
1"""
2Module for all game observers.
3"""
4from . import constants as c
5from .components import attack
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.set_observer_for_enemies()
20 self.event_dict = self.make_event_dict()
21
22 def set_observer_for_enemies(self):
23 for enemy in self.enemies:
24 enemy.observers.append(self)
25
26 def make_event_dict(self):
27 """
28 Make a dictionary of events the Observer can
29 receive.
30 """
31 event_dict = {c.END_BATTLE: self.end_battle,
32 c.SELECT_ACTION: self.select_action,
33 c.SELECT_ITEM: self.select_item,
34 c.SELECT_ENEMY: self.select_enemy,
35 c.SELECT_MAGIC: self.select_magic,
36 c.ENEMY_ATTACK: self.enemy_attack,
37 c.SWITCH_ENEMY: self.switch_enemy,
38 c.PLAYER_ATTACK: self.player_attack,
39 c.ATTACK_ANIMATION: self.attack_animation,
40 c.RUN_AWAY: self.run_away,
41 c.BATTLE_WON: self.battle_won,
42 c.PLAYER_FINISHED_ATTACK: self.player_finished_attack}
43
44 return event_dict
45
46 def on_notify(self, event):
47 """
48 Notify Observer of event.
49 """
50 if event in self.event_dict:
51 self.event_dict[event]()
52
53 def end_battle(self):
54 """
55 End Battle and flip to previous state.
56 """
57 self.level.end_battle()
58
59 def select_action(self):
60 """
61 Set components to select action.
62 """
63 self.level.state = c.SELECT_ACTION
64 self.arrow.index = 0
65 self.arrow.state = c.SELECT_ACTION
66 self.arrow.image = setup.GFX['smallarrow']
67 self.info_box.state = c.SELECT_ACTION
68
69 def select_enemy(self):
70 self.level.state = c.SELECT_ENEMY
71 self.arrow.index = 0
72 self.arrow.state = c.SELECT_ENEMY
73
74 def select_item(self):
75 self.level.state = c.SELECT_ITEM
76 self.info_box.state = c.SELECT_ITEM
77 self.arrow.become_select_item_state()
78
79 def select_magic(self):
80 self.level.state = c.SELECT_MAGIC
81 self.info_box.state = c.SELECT_MAGIC
82 self.arrow.become_select_magic_state()
83
84 def enemy_attack(self):
85 enemy = self.level.enemy_list[self.level.enemy_index]
86 enemy.enter_enemy_attack_state()
87 self.info_box.state = c.ENEMY_ATTACK
88
89 def switch_enemy(self):
90 """Switch which enemy is attacking player."""
91 if self.level.enemy_index < len(self.level.enemy_list) - 1:
92 self.level.enemy_index += 1
93 self.on_notify(c.ENEMY_ATTACK)
94 else:
95 self.on_notify(c.SELECT_ACTION)
96
97 def player_attack(self):
98 enemy_posx = self.arrow.rect.x + 60
99 enemy_posy = self.arrow.rect.y - 20
100 enemy_pos = (enemy_posx, enemy_posy)
101 enemy_to_attack = None
102
103 for enemy in self.enemies:
104 if enemy.rect.topleft == enemy_pos:
105 enemy_to_attack = enemy
106
107 self.player.enter_attack_state(enemy_to_attack)
108 self.arrow.become_invisible_surface()
109
110 def attack_animation(self):
111 """
112 Make an attack animation over attacked enemy.
113 """
114 self.arrow.remove_pos(self.player.attacked_enemy)
115 self.arrow.state = c.SELECT_ACTION
116 self.arrow.index = 0
117 self.level.attack_enemy()
118 self.info_box.state = c.ENEMY_HIT
119
120 def run_away(self):
121 self.level.end_battle()
122
123 def battle_won(self):
124 self.level.end_battle()
125
126 def player_finished_attack(self):
127 pass