-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathEventManager.py
More file actions
84 lines (62 loc) · 2.09 KB
/
Copy pathEventManager.py
File metadata and controls
84 lines (62 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class EventManager:
'''
It coordinate communication between the Model, View, and Controller.
Model, View, and Controller are all listeners, the EventManager will broadcast an event to them by post()
'''
def __init__(self):
self.listeners = []
def register_listener(self, listener):
'''
Adds a listener to our spam list.
It will receive Post()ed events through it's notify(event) call.
'''
self.listeners.append(listener)
def unregister_listener(self, listener):
'''
Remove a listener from our spam list.
This is implemented but hardly used.
Our weak ref spam list will auto remove any listeners who stop existing.
'''
pass
def post(self, event):
'''
Post a new event to the message queue.
It will be broadcast to all listeners.
'''
# # this segment use to debug
# if not (isinstance(event, Event_EveryTick) or isinstance(event, Event_EverySec)):
# print( str(event) )
for listener in self.listeners:
listener.notify(event)
class BaseEvent:
'''
A superclass for any events that might be generated by
an object and sent to the EventManager.
'''
name = 'Generic event'
def __init__(self):
pass
def __str__(self):
# For Debug
return self.name
class EventInitialize(BaseEvent):
name = 'Initialize event'
class EventQuit(BaseEvent):
name = 'Quit event'
class EventStateChange(BaseEvent):
name = 'StateChange event'
def __init__(self, state):
self.state = state
def __str__(self):
return f'{self.name} => StateTo: {self.state}'
class EventEveryTick(BaseEvent):
name = 'Tick event'
class EventTimesUp(BaseEvent):
name = "Time's Up event"
class EventPlayerMove(BaseEvent):
name = 'PlayerMove event'
def __init__(self, player_id, direction):
self.player_id = player_id
self.direction = direction
def __str__(self):
return f'{self.name} => player_id {self.player_id} move {self.direction}'