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.state = c.SELECT_ENEMY
65
66 def select_item(self):
67 self.level.state = c.SELECT_ITEM
68 self.info_box.state = c.SELECT_ITEM
69
70 def enemy_attack(self):
71 pass
72
73 def player_attack(self):
74 enemy_posx = self.arrow.rect.x + 60
75 enemy_posy = self.arrow.rect.y - 20
76 enemy_pos = (enemy_posx, enemy_posy)
77 enemy_to_attack = None
78
79 for enemy in self.enemies:
80 if enemy.rect.topleft == enemy_pos:
81 enemy_to_attack = enemy
82
83 self.player.enter_attack_state(enemy_to_attack)
84 self.arrow.become_invisible_surface()
85
86 def attack_animation(self):
87 """
88 Make an attack animation over attacked enemy.
89 """
90 enemy = self.player.attacked_enemy
91 if enemy:
92 enemy.kill()
93 posx = enemy.rect.x - 32
94 posy = enemy.rect.y - 64
95 fire_sprite = attack.Fire(posx, posy)
96 self.level.attack_animations.add(fire_sprite)
97
98 self.info_box.state = c.ENEMY_HIT
99
100 def run_away(self):
101 self.level.end_battle()
102
103 def battle_won(self):
104 self.level.end_battle()
105
106 def player_finished_attack(self):
107 pass