all repos — Legends-RPG @ 0c36050b5841a8b230a30b7dcda0ff5d32b310ed

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 . import setup
 6from .components import attackitems
 7from . import setup
 8
 9class Battle(object):
10    """
11    Observes events of battle and passes info to components.
12    """
13    def __init__(self, level):
14        self.level = level
15        self.player = level.player
16        self.set_observer_for_enemies()
17        self.event_dict = self.make_event_dict()
18
19    def set_observer_for_enemies(self):
20        for enemy in self.level.enemy_list:
21            enemy.observers.append(self)
22
23    def make_event_dict(self):
24        """
25        Make a dictionary of events the Observer can
26        receive.
27        """
28        event_dict = {c.ENEMY_DEAD: self.enemy_dead,
29                      c.ENEMY_DAMAGED: self.enemy_damaged,
30                      c.PLAYER_DAMAGED: self.player_damaged}
31
32        return event_dict
33
34    def on_notify(self, event):
35        """
36        Notify Observer of event.
37        """
38        if event in self.event_dict:
39            self.event_dict[event]()
40
41    def player_damaged(self):
42        self.level.enter_player_damaged_state()
43
44    def enemy_damaged(self):
45        """
46        Make an attack animation over attacked enemy.
47        """
48        self.level.enter_enemy_damaged_state()
49
50    def enemy_dead(self):
51        """
52        Eliminate all traces of enemy.
53        """
54        self.level.player.attacked_enemy = None
55
56
57class SoundEffects(object):
58    """
59    Observer for sound effects.
60    """
61    def on_notify(self, event):
62        """
63        Observer is notified of SFX event.
64        """
65        if event in setup.SFX:
66            setup.SFX[event].play()
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85