-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentities.js
290 lines (243 loc) · 8.21 KB
/
entities.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
289
/*-------------------
a player entity
-------------------------------- */
var PlayerEntity = me.ObjectEntity.extend({
/* -----
constructor
------ */
init: function(x, y, settings) {
// call the constructor
this.parent(x, y, settings);
this.collidable = true;
// set the default horizontal & vertical speed (accel vector)
this.setVelocity(6, 15);
// set the display to follow our position on both axis
me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
// set audio not playing
this.audioPlaying = false;
this.bounceRight = false;
this.bounceLeft = false;
},
onBeatAction: function () {
if (this.onBeat){
// console.debug("######## Moved On Beat!");
me.game.viewport.fadeOut("#00FF00", 0.2, 50);
onBeat_cooldown += 100; // TODO: the nearer on the real beat, the more ms he can see the hidden elements
// console.debug("onBeat cooldown is now:", onBeat_cooldown);
} else {
onBeat_cooldown -= 200; //400; // TODO: the nearer on the real beat, the more ms he can see the hidden elements
}
},
update: function() {
this.onBeat = checkIfOnBeat();
if (me.input.isKeyPressed('left')) {
// flip the sprite on horizontal axis
this.flipX(true);
// update the entity velocity
this.vel.x -= this.accel.x * me.timer.tick;
// TODO: replace, just a test
if ( !this.audioPlaying ) {
// me.audio.play("test_pulse_cycle", false, function() { this.audioPlaying = false }); // TODO, add again
}
// me.game.viewport.shake(2, 30, me.game.viewport.AXIS.HORIZONTAL); // melonjs' shake is buggy
this.onBeatAction();
//
} else if (me.input.isKeyPressed('right')) {
// unflip the sprite
this.flipX(false);
// update the entity velocity
this.vel.x += this.accel.x * me.timer.tick;
// me.game.viewport.shake(2, 30, me.game.viewport.AXIS.HORIZONTAL); // melonjs' shake is buggy
this.onBeatAction();
} else {
this.vel.x = 0;
}
if (me.input.isKeyPressed('jump')) {
// make sure we are not already jumping or falling
if (!this.jumping && !this.falling) {
// set current vel to the maximum defined value
// gravity will then do the rest
this.vel.y = -this.maxVel.y * me.timer.tick;
// set the jumping flag
this.jumping = true;
this.onBeatAction();
}
// me.game.viewport.shake(2, 30, me.game.viewport.AXIS.VERTICAL); // melonjs' shake is buggy
}
if (this.bounceRight && !this.falling) {
this.vel.x = 3*this.maxVel.x * me.timer.tick;
} else {
this.bounceRight = false;
}
if (this.bounceLeft && !this.falling) {
this.vel.x = -3*this.maxVel.x * me.timer.tick;
} else {
this.bounceLeft = false;
}
if (onBeat_cooldown < 0) { onBeat_cooldown = 0 };
// console.debug("onBeat cooldown is now:", onBeat_cooldown);
if (me.input.isKeyPressed('whistle1')) {
me.audio.play("test_whistle1", false, function() { this.audioPlaying = false }, 0.3);
}
if (me.input.isKeyPressed('whistle2')) {
if(whistle2_ready()){
me.audio.play("test_whistle2", false, function() { this.audioPlaying = false }, 0.3);
whistle2_cooldown = new Date().getTime();
// clearOverlay();
}
}
if (me.input.isKeyPressed('whistle3')) {
if(whistle3_ready()){
me.audio.play("transfer_middle", false, function() { }, 0.2);
whistle3_cooldown = new Date().getTime();
whistle3_ring_there = false;
// console.debug("Whistle 3 ready, go!");
}
}
// check & update player movement
this.updateMovement();
// check for collision
var res = me.game.collide(this);
if (res) {
// console.debug("Collision!, object type:", res.obj.type);
if (res.obj.type == "endGame") {
me.input.bindKey(me.input.KEY.ENTER, "enter", true);
me.state.change(me.state.ENDGAME);
}
if (res.obj.type == "whistled_r") {
// console.debug("Whistled r type collision");
// bounce (force jump)
this.bounceRight = true;
this.falling = false;
this.vel.y = -this.maxVel.y * me.timer.tick;
this.jumping = true;
} else if (res.obj.type == "whistled_l") {
// console.debug("Whistled r type collision");
// bounce (force jump)
this.bounceLeft = true;
this.falling = false;
this.vel.y = -this.maxVel.y * me.timer.tick;
// this.vel.x = 200 * me.timer.tick;
// this.pos.x += 1000;
// this.vel.x += 200*this.accel.x * me.timer.tick;
this.jumping = true;
} else if (res.obj.type == me.game.INVISIBLE_OBJECT) {
// check if we jumped on it
// console.debug("Normal invisvle collision");
if ((res.y > 0) && ! this.jumping) {
// bounce (force jump)
this.falling = false;
this.vel.y = -this.maxVel.y * me.timer.tick;
// set the jumping flag
this.jumping = true;
}
}
}
// update animation if necessary
if (this.vel.x!=0 || this.vel.y!=0) {
// update object animation
this.parent();
return true;
}
// else inform the engine we did not perform
// any update (e.g. position, animation)
return false;
},
onCollision: function(res, obj) {
// console.log("player entity collision!");
}
});
var WhistledEntity = me.InvisibleEntity.extend({
/* -----
constructor
------ */
init: function(x, y, settings) {
// call the constructor
this.parent(x, y, settings);
// console.debug("whistled init settings:", settings);
if (settings.endGame == "endIt") {
this.type = "endGame";
} else if (settings.direction == "r") {
// console.debug("whistled RIGHT");
this.type = "whistled_r";
} else if (settings.direction == "l") {
// console.debug("whistled RIGHT");
this.type = "whistled_l";
}
// console.log("whistled initalized!");
},
update: function() {
if (me.input.isKeyPressed('whistle1')) {
// TODO: show target on second screen
}
// check for collision
var res = me.game.collide(this);
if (res) {
// if we collide with an enemy
if (res.obj.type == me.game.PLAYER_OBJECT) {
// check if we jumped on it
//console.debug("############### invisible object touched")
}
}
return false;
},
// call by the engine when colliding with another object
// obj parameter corresponds to the other object (typically the player) touching this one
onCollision: function(res, obj) {
// console.log("whistled entity collision!");
}
});
var StartScreen = me.ScreenObject.extend({
init: function() {
this.parent(true);
this.title = null;
},
onResetEvent: function() {
if (this.title == null) {
this.title = me.loader.getImage("title_screen");
}
me.input.bindKey(me.input.KEY.ENTER, "enter", true);
},
// update function
update: function() {
// enter pressed ?
if (me.input.isKeyPressed('enter')) {
me.state.change(me.state.PLAY);
}
return true;
},
// draw function
draw: function(context) {
context.drawImage(this.title, 0, 0);
},
onDestroyEvent: function() {
me.input.unbindKey(me.input.KEY.ENTER);
}
});
var EndScreen = me.ScreenObject.extend({
init: function() {
this.parent(true);
this.title = null;
},
onResetEvent: function() {
if (this.title == null) {
this.title = me.loader.getImage("thx_screen");
}
me.input.bindKey(me.input.KEY.ENTER, "enter", true);
},
// update function
update: function() {
// enter pressed ?
if (me.input.isKeyPressed('enter')) {
me.state.change(me.state.PLAY);
}
return true;
},
// draw function
draw: function(context) {
context.drawImage(this.title, 0, 0);
},
onDestroyEvent: function() {
me.input.unbindKey(me.input.KEY.ENTER);
}
});