-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleSignalListener.ts
102 lines (89 loc) · 3.03 KB
/
simpleSignalListener.ts
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
import { _decorator, Component, Node, find, EventHandler, TextAsset } from 'cc';
import { CONSTANTS, SIMPLE_EVENT } from './constants';
import AppSettings from './persistentData/appSettings';
const { ccclass, property } = _decorator;
@ccclass('SimpleSignalListener')
export class SimpleSignalListener extends Component {
@property({type: Node, visible: false})
public appSettingsNode: Node = null!;
private appSettings: AppSettings = null!;
@property({visible: true})
private _active = true;
public get active() {
return this._active;
}
public set active(value: boolean) {
this._active = value;
}
@property({visible: true})
private _logOnRaise = false;
public get logOnRaise() {
return this._logOnRaise;
}
public set logOnRaise(value: boolean) {
this._logOnRaise = value;
}
@property({type: SIMPLE_EVENT, visible: true})
private _simpleSignal: number = null!;
public get simpleSignal() {
return this._simpleSignal;
}
public set simpleSignal(value: number) {
this._simpleSignal = value;
}
@property({type: TextAsset, visible: true})
private _variableReference: TextAsset = null!;
public get variableReference() {
return this._variableReference;
}
public set variableReference(value: TextAsset) {
this._variableReference = value;
}
@property({type: [EventHandler], visible: true})
private _action: EventHandler[] = [];
public get action() {
return this._action;
}
public set action(value: EventHandler[]) {
this._action = value;
}
onLoad () {
this.appSettingsNode = find(CONSTANTS.APP_SETTINGS_PATH) as Node;
this.appSettings = this.appSettingsNode.getComponent(AppSettings) as AppSettings;
if(this.simpleSignal !== null) {
this.appSettingsNode.on(Object.keys(SIMPLE_EVENT)[this.simpleSignal], this.performActions, this);
}
if(this.variableReference) {
this.appSettingsNode.on(this.variableReference.name, this.performActions, this);
}
}
onDestroy() {
if(this.simpleSignal) {
this.appSettingsNode.off(Object.keys(SIMPLE_EVENT)[this.simpleSignal], this.performActions, this);
}
if(this.variableReference) {
this.appSettingsNode.off(this.variableReference.name, this.performActions, this);
}
}
performActions() {
if(this.active) {
if(this.logOnRaise) {
if(this.simpleSignal) {
console.log(this.name + " was raised by listening to " + Object.keys(SIMPLE_EVENT)[this.simpleSignal])
}
if(this.variableReference) {
console.log(this.name + " was raised by listening to " + this.variableReference.name)
}
}
EventHandler.emitEvents(this.action);
}
}
/// Activate() and Deactivate() exist so that we can change
/// the sequence controller's status via Cocos editor event handlers
activate() {
this.active = true;
}
deactivate() {
this.active = false;
}
}