This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontroller.js
79 lines (75 loc) · 2.06 KB
/
controller.js
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
import { EventEmitter } from "events";
const defaultHp = 100;
const defaultColor = "green";
export default class Controller extends EventEmitter {
constructor(name, commandRunner) {
super();
this.client = null;
this.commandRunner = commandRunner;
this.name = name;
this.setHp(defaultHp);
this.setIsOni(false);
this.setLink(null);
this.setColor(defaultColor);
}
setHp(hp) {
this.hp = hp;
if (this.client !== null) {
this.client.sendCustomMessage("hp", this.hp);
}
this.emit("hp", this.hp);
}
setIsOni(isOni) {
this.isOni = isOni;
if (this.client !== null) {
this.client.sendCustomMessage("oni", this.isOni);
}
this.emit("oni", this.isOni);
}
setLink(orb) {
// client も持っているが、それに左右されずにするため link は別に持つ必要がある
this.linkedOrb = orb !== null ? orb : null;
if (this.client !== null) {
if (orb === null) {
this.client.unlink();
} else {
this.client.setLinkedOrb(orb);
}
}
updateColor.call(this);
}
setClient(client) {
this.client = client;
if (this.client !== null) {
this.client.sendCustomMessage("hp", this.hp);
this.client.sendCustomMessage("oni", this.isOni);
this.client.sendCustomMessage("color", this.color);
}
if (this.linkedOrb !== null && this.client !== null) {
// HPなどの Orb -> Client への伝達で、
// client にも linkedOrb を入れておく必要がある。
this.client.setLinkedOrb(this.linkedOrb);
}
}
setColor(color) {
this.color = color;
updateColor.call(this);
if (this.client !== null) {
this.client.sendCustomMessage("color", this.color);
}
}
getStates() {
return {
hp: this.hp,
isOni: this.isOni,
link: this.linkedOrb !== null ? this.linkedOrb.name : null,
key: this.client !== null ? this.client.key : null,
color: this.color
};
}
}
function updateColor() {
if (this.linkedOrb !== null) {
this.linkedOrb.command("color", [this.color]);
}
}