-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayerEntity.js
76 lines (61 loc) · 2.51 KB
/
playerEntity.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
/*------------------- a player entity -------------------------------- */
var PlayerEntity = me.ObjectEntity.extend({
/* ----- constructor ------ */
init: function(x, y, settings) {
// call the constructor
this.parent(x, y, settings);
// set the walking speed, on start player moves to the right
this.vel.x=3;
this.vel.y=0;
// set the gravity to 0
this.gravity = 0;
this.collidable = true;
this.addAnimation ("walk", [0,3]);
this.animationspeed = 3;
this.setCurrentAnimation("walk");
// adjust the bounding box
this.updateColRect(1, 30, 1, 30);
// set the display to follow our position on both axis
me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
},
/* ----- update the player pos ------ */
update: function() {
if ((this.vel.x < 0) && (this.pos.x <= 2)) {
this.pos.x = 672-32;
} else if ((this.vel.x > 0) && (this.pos.x >= 672-34)) {
this.pos.x = 0;
}
// This variable we might need in the future for restoring direction if after selecting a new one
// the mainPlayer would hit the wall
var oldVel = this.vel.clone();
if (me.input.isKeyPressed('left')) {
this.vel.x = -3;
this.vel.y = 0;
}
if (me.input.isKeyPressed('right')) {
this.vel.x = 3;
this.vel.y = 0;
}
if (me.input.isKeyPressed('down')) {
this.vel.x = 0;
this.vel.y = 3;
}
if (me.input.isKeyPressed('up')) {
this.vel.x = 0;
this.vel.y = -3;
}
// Try to update player's movement. Then check results, a non-zero res means there is a collision, so
// the player should not change direction but rather continue going in the old direction
var res = this.updateMovement();
if (res.x || res.y) {
this.vel.x = oldVel.x;
this.vel.y = oldVel.y;
this.updateMovement();
}
// check for collision NOTE MelonJS seems to not process collisions without this line
me.game.collide(this);
//always update animation
this.parent(this);
return true;
},
});