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
|
// player
var PlayerEntity = me.ObjectEntity.extend({
// Constructor
init: function(x, y, settings) {
// superclass constructor?
this.parent(x, y, settings);
// walking, jumping speed:
this.setVelocity(4, 17); // set accel + max vel
this.setMaxVelocity(255,255); // just something big so we don't hit it
this.gravity = 0.8;
//camera should follow us
me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
},
addXVel: function(ddx, maxVel) {
absMaxVel = Math.abs(maxVel);
// if ddx.sign() and vel.x.sign() are the same
// (trying to accelerate in the direction we're going)
// and vel.x.abs() > maxVel.abs()
// then do nothing
if ((ddx.sign() == this.vel.x.sign()) &&
(Math.abs(this.vel.x) > absMaxVel)) {
//console.log('at max velocity, aborting');
return;
}
// otherwise, apply the velocity, but clamp it
this.vel.x += ddx.clamp(-absMaxVel, absMaxVel);
},
// identical to above. Could probably be factored out.
addYVel: function(ddy, maxVel) {
absMaxVel = Math.abs(maxVel);
if ((ddy.sign() == this.vel.y.sign()) &&
(Math.abs(this.vel.y) > absMaxVel))
return;
this.vel.y += ddy.clamp(-absMaxVel, absMaxVel);
},
// input
update: function() {
MAX_WALK_VEL = 2;
JUMP_SPEED = 17;
if (me.input.isKeyPressed('left')) {
this.flipX(true);
this.addXVel(-this.accel.x * me.timer.tick, MAX_WALK_VEL);
} else if (me.input.isKeyPressed('right')) {
this.flipX(false);
this.addXVel(this.accel.x * me.timer.tick, MAX_WALK_VEL);
} else {
//stop immediately??
this.vel.x = 0;
}
if (me.input.isKeyPressed('jump')) {
if (!this.jumping && !this.falling) {
//this.vel.y = -this.maxVel.y * me.timer.tick;
this.vel.y = -JUMP_SPEED;
this.jumping = true;
me.audio.play('jump');
}
}
// messy method. will need to replace eventually.
this.updateMovement();
// "update animation if necessary"
if (this.vel.x != 0 || this.vel.y != 0) {
this.parent(this); // this has to be the real work
return true; // but what does this do?
}
return false;
},
computeVelocity : function(vel) {
//console.log('Fancy new overloaded computeVelocity!');
TERMINAL_VEL = 17;
// apply gravity
if (this.gravity) {
// apply a constant gravity (if not on a ladder)
if (!this.onladder)
this.addYVel(this.gravity * me.timer.tick, TERMINAL_VEL);
// check if falling / jumping
this.falling = (vel.y > 0);
this.jumping = this.falling?false:this.jumping;
}
// apply friction
if (this.friction.x)
vel.x = me.utils.applyFriction(vel.x,this.friction.x);
if (this.friction.y)
vel.y = me.utils.applyFriction(vel.y,this.friction.y);
// cap velocity
/*if (vel.y !=0)
vel.y = vel.y.clamp(-this.maxVel.y,this.maxVel.y);
if (vel.x !=0)
vel.x = vel.x.clamp(-this.maxVel.x,this.maxVel.x); */
return vel;
}
}); //PlayerEntity definition
|