all repos — Legends-RPG @ bc9dbbaff0f24475c11c5f6a992e77532b9db02c

A fantasy mini-RPG built with Python and Pygame.

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.player = level.player
15        self.set_observer_for_enemies()
16        self.event_dict = self.make_event_dict()
17
18    def set_observer_for_enemies(self):
19        for enemy in self.level.enemy_list:
20            enemy.observers.append(self)
21
22    def make_event_dict(self):
23        """
24        Make a dictionary of events the Observer can
25        receive.
26        """
27        event_dict = {c.ENEMY_DEAD: self.enemy_dead,
28                      c.ENEMY_DAMAGED: self.enemy_damaged,
29                      c.PLAYER_DAMAGED: self.player_damaged}
30
31        return event_dict
32
33    def on_notify(self, event):
34        """
35        Notify Observer of event.
36        """
37        if event in self.event_dict:
38            self.event_dict[event]()
39
40    def player_damaged(self):
41        self.level.enter_player_damaged_state()
42
43    def enemy_damaged(self):
44        """
45        Make an attack animation over attacked enemy.
46        """
47        self.level.enter_enemy_damaged_state()
48
49    def enemy_dead(self):
50        """
51        Eliminate all traces of enemy.
52        """
53        self.level.player.attacked_enemy = None
54
55
56
57
58
59
60
61
62
63
64