-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgame-core.js
More file actions
155 lines (136 loc) · 4.23 KB
/
Copy pathgame-core.js
File metadata and controls
155 lines (136 loc) · 4.23 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* The core part of the game, which doesn't touch the UI.
*/
class GameCore {
constructor(connection) {
this.connection = connection;
this.me = null;
this.members = {};
this.onMemberListChangedCallback = [];
}
run(context) {
this.ctx = context;
this.previousElapsed = 0;
this.stop = false;
const p = this.load();
this.isLoad = true;
Promise.all(p).then((loaded) => {
this.init();
window.requestAnimationFrame(this.tick);
});
}
initCurrentUser(map, name, character) {
this.me = new Hero(map, 160, 160, character, name);
}
updateMemberList() {
if (!this.connection || !this.connection.room) return;
let changed = false;
const room = this.connection.room;
const participants = room.participants;
for (const id in participants) {
if (!participants.hasOwnProperty(id)) continue;
const participant = participants[id];
if (this.members[id] === undefined) {
const x = parseInt(participant.getProperty('left'));
const y = parseInt(participant.getProperty('top'));
this.members[id] = new Hero(
map, x, y,
participant.getProperty('character'),
participant.getDisplayName());
changed = true;
}
}
for (const id in this.members) {
if (!this.members.hasOwnProperty(id)) continue;
if (!participants.hasOwnProperty(id) && id !== 'me') {
// Somehow, this user has left, but onUserLeft is not triggered.
delete this.members[id];
changed = true;
}
}
if (changed) {
this.onMemberListChangedCallback.map((callback) => {
callback(this.members);
});
}
}
updateMember(id, {name}) {
if (name !== undefined) {
this.members[id].name = name || 'nobody';
}
}
onUserJoined(id, user) {
addLog(`${user.getDisplayName()} joined`);
this.updateMemberList();
}
onMessageReceived(id, text, timestamp, nick, other) {
if (this.members[id] === undefined) return;
const now = (new Date).getTime();
if (timestamp === undefined) {
timestamp = now;
} else {
timestamp = (new Date(timestamp)).getTime();
}
if (timestamp + 20 * 1000 < now) return;
this.members[id].messages.push([text, timestamp + 20 * 2000]);
}
onUserLeft(id, user) {
addLog(`${user.getDisplayName()} left`);
delete this.members[id];
this.updateMemberList();
}
onParticipantPropertyChanged(user, text, timestamp) {
if (['top', 'left', 'character'].indexOf(text) === -1) {
console.log(text);
return;
}
const id = user.getId();
if (this.members[id] === undefined) {
console.log(`received message from unknown user: ${id}`);
}
if (!this.connection || !this.connection.room) {
// Really??
return;
}
const room = this.connection.room;
const x = parseInt(room.participants[id].getProperty('left'));
const y = parseInt(room.participants[id].getProperty('top'));
if (!Number.isNaN(x) && !Number.isNaN(y)) {
this.members[id].target_x = x;
this.members[id].target_y = y;
if (this.members[id].x === 0 && this.members[id].y === 0) {
this.members[id].x = x;
this.members[id].y = y;
}
}
const roomCharacter = room.participants[id].getProperty('character');
if (this.members[id].character != roomCharacter) {
this.members[id].changeCharacter(roomCharacter);
this.updateMemberList();
}
}
onEndpointMessageReceived(participant, message) {
if (message.type === 'teleport') {
const id = participant.getId();
if (this.members[id] !== undefined) {
this.members[id].x = this.members[id].target_x = message.message[0];
this.members[id].y = this.members[id].target_y = message.message[1];
}
}
}
onDisplayNameChanged(id, name) {
addLog(`${this.members[id].name} changed name to ${name}`);
if (this.members[id] !== undefined) {
this.members[id].name = name;
}
this.updateMemberList();
}
onTrackAudioLevelChanged(id, audioLevel) {
if (this.members[id] !== undefined) {
this.members[id].audioLevel = audioLevel;
}
}
registerOnMemberListChanged(callback) {
this.onMemberListChangedCallback.push(callback);
}
}