forked from christkv/mongoman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remoteplayer.js
288 lines (259 loc) · 10.7 KB
/
remoteplayer.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
var createRemoteGhostPlayer = function(data) {
return {
conId:data.conId,
id:"ghost"+data.id,
group:"ghosts",
tileset:data.tileset,
status: 'chasing',
time: 0,
initialize: function() {
toys.topview.initialize(this,{
colh:gbox.getTiles(this.tileset).tileh, // That is like capman...
colw:gbox.getTiles(this.tileset).tilew,
staticspeed:4,
nodiagonals:true,
noreset:true,
frames:{
still:{ speed:2, frames:[0] },
hit:{speed:1,frames:[0,1,0,1]},
standup:{ speed:1, frames:[0] },
standdown:{ speed:1, frames:[1] },
standleft:{ speed:1, frames:[2] },
standright:{ speed:1, frames:[2] },
movingup:{speed:3,frames:[0] },
movingdown:{speed:3,frames:[1] },
movingleft:{speed:3,frames:[2] },
movingright:{speed:3,frames:[2] }
},
// Set start point for the ghost
x:data.x,
y:data.y
});
},
first: function() {
// Ensure we are showing the current correct frame out of 10 possible
this.counter = (this.counter+1) % 10;
// If capman is still alive and the game is not "hold" (level changing fadein/fadeouts etc.) and the "bullet timer" is not stopping the game.
if(!maingame.gameIsHold() && !maingame.bullettimer) {
// Grab the mongoman object
var mongoman = isMongoman ? gbox.getObject("player", "mongoman") : gbox.getObject("ghosts", "mongoman");
// The nuber of ticks the ghost i in danger
if(this.status == 'running' && this.time > 0) {
this.time--;
if(this.time > 50) {
this.tileset = 'ghostscared'
} else {
this.tileset = Math.floor(this.time/4)%2 == 0 ? "ghostscared" : "playerghost";
}
} else if(this.status == 'running') {
this.tileset = 'playerghost'
this.status = 'chasing';
// Up the combo
mongoman.scorecombo = 1;
}
// Return to the house
if(this.status == 'eaten') {
toys.topview.setStaticSpeed(this, 4); // We're in a hurry now!
if((this.x == maze.hw - this.hw) && (this.y == maze.hh - 38))
this.status = "goin";
else {
if((this.facing == toys.FACE_UP) || (this.facing == toys.FACE_DOWN)) {
if(maze.hw - this.hw >= this.x) {
toys.topview.controlKeys(this,{pressright: 1});
} else if(maze.hw - this.hw < this.x) {
toys.topview.controlKeys(this,{pressleft: 1});
}
} else {
if(maze.hh - 38 > this.y) {
toys.topview.controlKeys(this, {pressdown: 1});
} else if(maze.hh - 38 < this.y) {
toys.topview.controlKeys(this, {pressup: 1});
}
}
}
toys.topview.applyForces(this);
} else if(this.status == 'goin') {
toys.topview.setStaticSpeed(this, 1);
toys.topview.controlKeys(this, {pressdown: 1});
toys.topview.applyForces(this);
toys.topview.tileCollision(this,maze, "map", null, {tolerance:0, approximation:1});
if(this.toucheddown) {
this.tileset = this.id;
toys.topview.setStaticSpeed(this, 2)
this.time = 75;
this.status = "chasing";
}
} else {
if(remotePlayerPositionUpdates[this.conId] != null && remotePlayerPositionUpdates[this.conId].nextPos != null) {
// Grab the next position
var nextPos = remotePlayerPositionUpdates[this.conId].nextPos;
// Reset position
remotePlayerPositionUpdates[this.conId] = {pos:nextPos, nextPos:null};
// Ensure we start at the same x, y coordinates
var obj = gbox.getObject('ghosts', this.id);
obj.x = nextPos.x;
obj.y = nextPos.y;
obj.accx = nextPos.accx;
obj.accy = nextPos.accy;
obj.xpushing = nextPos.xpushing;
obj.ypushing = nextPos.ypushing;
obj.facing = nextPos.facing;
// Apply forces and do tileCollision detection
toys.topview.applyForces(this);
toys.topview.tileCollision(this, maze, "map", null, {tolerance:0,approximation:1});
if(this.touchedup) {
this.status = "chasing";
}
}
// The side warp. If capman reach one of the left or right side of the maze, is spawn on the other side,in the same direction
if((this.x < 0) && (this.facing == toys.FACE_LEFT)) {
this.x = maze.w - this.w;
} else if((this.x > (maze.w - this.w)) && (this.facing == toys.FACE_RIGHT)) {
this.x = 0;
}
// setFrame sets the right frame checking the facing and the defined animations in "initialize"
toys.topview.setFrame(this);
// Check if we have a collision
if(isMongoman && mongoman != null && gbox.collides(this, mongoman, 2)) {
// If we are chasing him he is dead
if(this.status == "chasing") {
// Stop the game for a time
maingame.bullettimer = 10;
// kill mongoman
mongoman.kill();
} else if(this.status == "running") {
// We get a score combo bonus for eating ghosts
maingame.hud.addValue("score","value",mongoman.scorecombo * 100);
// Up the combo
mongoman.scorecombo++;
// Signal eaten a ghost
if(sound) SoundJS.play("eatghost");
// Fire off I'm dead message
client.dispatchCommand({type:'ghostdead', id:this.conId});
// gbox.hitAudio("eatghost");
maingame.bullettimer = 10;
toys.generate.sparks.popupText(mongoman,"sparks",null,{font:"small",jump:5,text:'',keep:20});
this.tileset = "ghosteaten";
this.status = "eaten";
}
}
}
}
},
makeeatable:function() {
this.status = 'running';
this.time = 150;
},
blit:function() {
gbox.blitTile(gbox.getBufferContext(),{tileset:this.tileset,tile:this.frame,dx:this.x,dy:this.y,fliph:this.fliph,flipv:this.flipv,camera:this.camera,alpha:1});
},
// And now, a custom method. This one will kill the player and will be called by ghosts, when colliding with capman.
kill:function() {
if(sound) SoundJS.play("die");
// Set status to eaten
this.status = 'eaten';
// Change tileset
this.tileset = "ghosteaten";
}
};
}
var createRemoteMongoManPlayer = function(data) {
return {
conId:data.conId,
id:"mongoman",
group:"ghosts",
tileset:"mongoman",
killed:false,
render:false,
initialize: function() {
toys.topview.initialize(this,{
colh:gbox.getTiles(this.tileset).tileh,
colw:gbox.getTiles(this.tileset).tilew,
staticspeed:4,
nodiagonals:true,
noreset:true,
frames:{
still:{ speed:2, frames:[0] },
hit:{speed:1,frames:[0,1,0,1]},
standup:{ speed:1, frames:[0] },
standdown:{ speed:1, frames:[0] },
standleft:{ speed:1, frames:[0] },
standright:{ speed:1, frames:[0] },
movingup:{speed:3,frames:[0,2,1,2] },
movingdown:{speed:3,frames:[0,4,3,4] },
movingleft:{speed:3,frames:[0,6,5,6] },
movingright:{speed:3,frames:[0,6,5,6] }
},
// Set start point for the ghost
x:data.x,
y:data.y
});
},
first: function() {
// Ensure we are showing the current correct frame out of 10 possible
this.counter = (this.counter+1) % 10;
// If capman is still alive and the game is not "hold" (level changing fadein/fadeouts etc.) and the "bullet timer" is not stopping the game.
if(!maingame.gameIsHold() && !maingame.bullettimer) {
if(remotePlayerPositionUpdates[this.conId] != null && remotePlayerPositionUpdates[this.conId].nextPos != null) {
// Grab the next position
var nextPos = remotePlayerPositionUpdates[this.conId].nextPos;
// Reset position
remotePlayerPositionUpdates[this.conId] = {pos:nextPos, nextPos:null};
// Ensure we start at the same x, y coordinates
var obj = gbox.getObject('ghosts', "mongoman");
obj.x = nextPos.x;
obj.y = nextPos.y;
obj.accx = nextPos.accx;
obj.accy = nextPos.accy;
obj.xpushing = nextPos.xpushing;
obj.ypushing = nextPos.ypushing;
obj.facing = nextPos.facing;
// Apply forces and do tileCollision detection
toys.topview.applyForces(this);
toys.topview.tileCollision(this, maze, "map", null, {tolerance: 0, approximation: 1});
}
// The side warp. If capman reach one of the left or right side of the maze, is spawn on the other side,in the same direction
if((this.x < 0) && (this.facing == toys.FACE_LEFT)) {
this.x = maze.w - this.w;
} else if((this.x > (maze.w - this.w)) && (this.facing == toys.FACE_RIGHT)) {
this.x = 0;
}
// setFrame sets the right frame checking the facing and the defined animations in "initialize"
toys.topview.setFrame(this);
// Grab the current tile in the map object
var inmouth = help.getTileInMap(this.x + this.hw, this.y + this.hh, maze, 0);
// Handle pills
if(inmouth>7) {
if(inmouth == 9) {
if(sound) SoundJS.play("powerpill");
if(gbox.getObject("ghosts","ghost1")) gbox.getObject("ghosts","ghost1").makeeatable();
if(gbox.getObject("ghosts","ghost2")) gbox.getObject("ghosts","ghost2").makeeatable();
if(gbox.getObject("ghosts","ghost3")) gbox.getObject("ghosts","ghost3").makeeatable();
if(gbox.getObject("ghosts","ghost4")) gbox.getObject("ghosts","ghost4").makeeatable();
if(gbox.getObject("player", "playerghost")) gbox.getObject("player","playerghost").makeeatable();
} else {
if(sound) SoundJS.play("eat");
}
var mouthx = help.xPixelToTileX(maze,this.x + this.hw);
var mouthy = help.yPixelToTileY(maze,this.y + this.hh);
help.setTileInMap(gbox.getCanvasContext("mazecanvas"), maze, mouthx, mouthy, null);
maingame.pillscount--;
}
}
},
blit:function() {
// if(this.render) {
gbox.blitTile(gbox.getBufferContext(),{tileset:this.tileset,tile:this.frame,dx:this.x,dy:this.y,fliph:this.fliph,flipv:this.flipv,camera:this.camera,alpha:1});
// }
},
// And now, a custom method. This one will kill the player and will be called by ghosts, when colliding with capman.
kill:function() {
if(!this.killed) {
this.killed = true;
if(sound) SoundJS.play("die");
maingame.playerDied({wait:50});
toys.generate.sparks.simple(this,"sparks",null,{tileset:this.tileset,frames:{speed:4,frames:[6,5,7,8,9,9,9,9]}});
}
}
};
}