-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsignal.go
102 lines (88 loc) · 3.09 KB
/
signal.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Copyright (C) 2021 Alexander Lunsford
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import "container/list"
type Signal int
const (
SIGNAL_PLAYER_MOVED Signal = iota //Fires continuously as long as the player is moving. For the tutorial mission.
SIGNAL_PLAYER_SHOT //Fires every time the player shoots. For the tutorial mission.
SIGNAL_PLAYER_EDGE //Fires when the camera reaches the edge of the map. For the tutorial mission.
SIGNAL_PLAYER_ASCEND //Fires to indicate when the player ascends
SIGNAL_CAT_RULE //Fires when the player tries to shoot the cat without being ascended
SIGNAL_CAT_DIE //Fires when the cat is killed
SIGNAL_GAME_START //Fires at the start of the game after the intro transition
SIGNAL_GAME_INIT //Fires before the game level is generated
SIGNAL_LOVE_CHANGE //Fires when the love meter is increased or decreased
)
//Represents the number of times a signal has been emitted
var __signal_counts map[Signal]int
//Returns the number of times the given signal has been emitted
func Get_Signal_Count(sig Signal) int {
return __signal_counts[sig]
}
type Observer interface {
HandleSignal(kind Signal, src interface{}, params map[string]interface{})
}
var __observers map[Signal]*list.List
//Retrieve observer map and initialize if needed
func observers() map[Signal]*list.List {
if __observers == nil {
__observers = make(map[Signal]*list.List)
}
return __observers
}
func Listen_Signal(kind Signal, obs Observer) {
//Initialize list if haven't already
lst, ok := observers()[kind]
if !ok {
lst = list.New()
observers()[kind] = lst
}
//Exit if observer is already registered
for itr := lst.Front(); itr != nil; itr = itr.Next() {
c_obs := itr.Value.(Observer)
if c_obs == obs {
println("Observer already added.")
return
}
}
//Add observer to list
lst.PushBack(obs)
}
func Emit_Signal(kind Signal, src interface{}, params map[string]interface{}) {
//Update signal count
if __signal_counts == nil {
__signal_counts = make(map[Signal]int)
}
_, ok := __signal_counts[kind]
if !ok {
__signal_counts[kind] = 1
} else {
__signal_counts[kind] += 1
}
//Exit when no observers
lst, ok := observers()[kind]
if !ok {
return
}
//Make empty map if passed nil
if params == nil {
params = make(map[string]interface{})
}
//Callback on all listening observers
for itr := lst.Front(); itr != nil; itr = itr.Next() {
obs := itr.Value.(Observer)
obs.HandleSignal(kind, src, params)
}
}