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.event_dict = self.make_event_dict()
20
21 def make_event_dict(self):
22 """
23 Make a dictionary of events the Observer can
24 receive.
25 """
26 event_dict = {c.END_BATTLE: self.end_battle,
27 c.SELECT_ACTION: self.select_action,
28 c.SELECT_ITEM: self.select_item,
29 c.SELECT_ENEMY: self.select_enemy,
30 c.ENEMY_ATTACK: self.enemy_attack,
31 c.PLAYER_ATTACK: self.player_attack,
32 c.ATTACK_ANIMATION: self.attack_animation,
33 c.RUN_AWAY: self.run_away,
34 c.BATTLE_WON: self.battle_won,
35 c.PLAYER_FINISHED_ATTACK: self.player_finished_attack}
36
37 return event_dict
38
39 def on_notify(self, event):
40 """
41 Notify Observer of event.
42 """
43 if event in self.event_dict:
44 self.event_dict[event]()
45
46 def end_battle(self):
47 """
48 End Battle and flip to previous state.
49 """
50 self.level.end_battle()
51
52 def select_action(self):
53 """
54 Set components to select action.
55 """
56 self.level.state = c.SELECT_ACTION
57 self.arrow.index = 0
58 self.arrow.state = c.SELECT_ACTION
59 self.arrow.image = setup.GFX['smallarrow']
60 self.info_box.state = c.SELECT_ACTION
61
62 def select_enemy(self):
63 self.level.state = c.SELECT_ENEMY
64 self.arrow.index = 0
65 self.arrow.state = c.SELECT_ENEMY
66
67 def select_item(self):
68 self.level.state = c.SELECT_ITEM
69 self.info_box.state = c.SELECT_ITEM
70 self.arrow.become_select_item_state()
71
72 def enemy_attack(self):
73 pass
74
75 def player_attack(self):
76 enemy_posx = self.arrow.rect.x + 60
77 enemy_posy = self.arrow.rect.y - 20
78 enemy_pos = (enemy_posx, enemy_posy)
79 enemy_to_attack = None
80
81 for enemy in self.enemies:
82 if enemy.rect.topleft == enemy_pos:
83 enemy_to_attack = enemy
84
85 self.player.enter_attack_state(enemy_to_attack)
86 self.arrow.become_invisible_surface()
87
88 def attack_animation(self):
89 """
90 Make an attack animation over attacked enemy.
91 """
92 self.arrow.remove_pos(self.player.attacked_enemy)
93 self.arrow.state = c.SELECT_ACTION
94 self.level.attack_enemy()
95 self.info_box.state = c.ENEMY_HIT
96
97 def run_away(self):
98 self.level.end_battle()
99
100 def battle_won(self):
101 self.level.end_battle()
102
103 def player_finished_attack(self):
104 pass