bzr branch
http://9ix.org/moongirl
4
by Josh C
platformer! |
1 |
// player |
2 |
var PlayerEntity = me.ObjectEntity.extend({ |
|
3 |
// Constructor |
|
4 |
init: function(x, y, settings) { |
|
5 |
// superclass constructor? |
|
6 |
this.parent(x, y, settings); |
|
7 |
||
13
by Josh C
add friction, change X velocities to fight friction |
8 |
this.debugtimer = 0.0; |
9 |
||
4
by Josh C
platformer! |
10 |
// walking, jumping speed: |
13
by Josh C
add friction, change X velocities to fight friction |
11 |
this.setVelocity(1, 17); // set accel + max vel |
11
by Josh C
built system to have multiple "max velocities" for different sources of |
12 |
this.setMaxVelocity(255,255); // just something big so we don't hit it |
6
by Josh C
tweaking speed and gravity |
13 |
this.gravity = 0.8; |
14
by Josh C
only apply friction on ground. adjust some X speeds to account for that |
14 |
this.friction.x = 0.3; |
6
by Josh C
tweaking speed and gravity |
15 |
|
4
by Josh C
platformer! |
16 |
//camera should follow us |
17 |
me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH); |
|
18 |
}, |
|
19 |
||
11
by Josh C
built system to have multiple "max velocities" for different sources of |
20 |
addXVel: function(ddx, maxVel) { |
21 |
absMaxVel = Math.abs(maxVel); |
|
22 |
||
23 |
// if ddx.sign() and vel.x.sign() are the same |
|
24 |
// (trying to accelerate in the direction we're going) |
|
25 |
// and vel.x.abs() > maxVel.abs() |
|
26 |
// then do nothing |
|
27 |
if ((ddx.sign() == this.vel.x.sign()) && |
|
28 |
(Math.abs(this.vel.x) > absMaxVel)) { |
|
29 |
//console.log('at max velocity, aborting'); |
|
30 |
return; |
|
31 |
} |
|
32 |
||
33 |
// otherwise, apply the velocity, but clamp it |
|
34 |
this.vel.x += ddx.clamp(-absMaxVel, absMaxVel); |
|
35 |
}, |
|
36 |
||
37 |
// identical to above. Could probably be factored out. |
|
38 |
addYVel: function(ddy, maxVel) { |
|
39 |
absMaxVel = Math.abs(maxVel); |
|
40 |
if ((ddy.sign() == this.vel.y.sign()) && |
|
41 |
(Math.abs(this.vel.y) > absMaxVel)) |
|
42 |
return; |
|
43 |
this.vel.y += ddy.clamp(-absMaxVel, absMaxVel); |
|
44 |
}, |
|
45 |
||
4
by Josh C
platformer! |
46 |
// input |
47 |
update: function() { |
|
13
by Josh C
add friction, change X velocities to fight friction |
48 |
MAX_WALK_VEL = 3.7; |
11
by Josh C
built system to have multiple "max velocities" for different sources of |
49 |
JUMP_SPEED = 17; |
14
by Josh C
only apply friction on ground. adjust some X speeds to account for that |
50 |
JETPACK_X_VEL = new me.Vector2d(11,17); |
12
by Josh C
and, jetpack logic. |
51 |
JETPACK_Y_VEL = 25; |
11
by Josh C
built system to have multiple "max velocities" for different sources of |
52 |
|
4
by Josh C
platformer! |
53 |
if (me.input.isKeyPressed('left')) { |
11
by Josh C
built system to have multiple "max velocities" for different sources of |
54 |
this.flipX(true); |
55 |
this.addXVel(-this.accel.x * me.timer.tick, MAX_WALK_VEL); |
|
4
by Josh C
platformer! |
56 |
} else if (me.input.isKeyPressed('right')) { |
11
by Josh C
built system to have multiple "max velocities" for different sources of |
57 |
this.flipX(false); |
58 |
this.addXVel(this.accel.x * me.timer.tick, MAX_WALK_VEL); |
|
4
by Josh C
platformer! |
59 |
} else { |
60 |
//stop immediately?? |
|
13
by Josh C
add friction, change X velocities to fight friction |
61 |
//this.vel.x = 0; |
4
by Josh C
platformer! |
62 |
} |
63 |
||
64 |
if (me.input.isKeyPressed('jump')) { |
|
12
by Josh C
and, jetpack logic. |
65 |
if (this.jumping || this.falling) { |
66 |
if (me.input.isKeyPressed('left')) { |
|
67 |
this.vel.y = -JETPACK_X_VEL.y; |
|
68 |
this.vel.x = -JETPACK_X_VEL.x; |
|
69 |
} else if (me.input.isKeyPressed('right')) { |
|
70 |
this.vel.y = -JETPACK_X_VEL.y; |
|
71 |
this.vel.x = JETPACK_X_VEL.x; |
|
72 |
} else { |
|
73 |
// go straight up |
|
74 |
this.vel.y = -JETPACK_Y_VEL; |
|
75 |
} |
|
76 |
} else { |
|
77 |
//if (!this.jumping && !this.falling) { |
|
11
by Josh C
built system to have multiple "max velocities" for different sources of |
78 |
//this.vel.y = -this.maxVel.y * me.timer.tick; |
79 |
this.vel.y = -JUMP_SPEED; |
|
80 |
this.jumping = true; |
|
81 |
||
8
by Josh C
jump noise |
82 |
me.audio.play('jump'); |
83 |
} |
|
4
by Josh C
platformer! |
84 |
} |
85 |
||
11
by Josh C
built system to have multiple "max velocities" for different sources of |
86 |
// messy method. will need to replace eventually. |
4
by Josh C
platformer! |
87 |
this.updateMovement(); |
88 |
||
13
by Josh C
add friction, change X velocities to fight friction |
89 |
/* this.debugtimer += me.timer.tick; |
90 |
if (this.debugtimer > 100) { |
|
91 |
dbg = "XVel: " + this.vel.x; |
|
92 |
dbg += "<br>me.timer.tick: " + me.timer.tick; |
|
93 |
||
94 |
dbgEl = document.getElementById('debug'); |
|
95 |
//dbgEl.replaceChild(document.createTextNode(dbg), dbgEl.firstChild); |
|
96 |
dbgEl.innerHTML = dbg; |
|
97 |
||
98 |
this.debugtimer = 0.0; |
|
99 |
} */ |
|
100 |
||
4
by Josh C
platformer! |
101 |
// "update animation if necessary" |
102 |
if (this.vel.x != 0 || this.vel.y != 0) { |
|
103 |
this.parent(this); // this has to be the real work |
|
104 |
return true; // but what does this do? |
|
105 |
} |
|
106 |
||
107 |
return false; |
|
11
by Josh C
built system to have multiple "max velocities" for different sources of |
108 |
}, |
109 |
||
110 |
computeVelocity : function(vel) { |
|
111 |
//console.log('Fancy new overloaded computeVelocity!'); |
|
112 |
TERMINAL_VEL = 17; |
|
113 |
||
114 |
// apply gravity |
|
115 |
if (this.gravity) { |
|
14
by Josh C
only apply friction on ground. adjust some X speeds to account for that |
116 |
// check if falling / jumping |
117 |
this.falling = (vel.y > 0); |
|
118 |
this.jumping = this.falling?false:this.jumping; |
|
119 |
||
11
by Josh C
built system to have multiple "max velocities" for different sources of |
120 |
// apply a constant gravity (if not on a ladder) |
121 |
if (!this.onladder) |
|
122 |
this.addYVel(this.gravity * me.timer.tick, TERMINAL_VEL); |
|
123 |
} |
|
124 |
||
125 |
// apply friction |
|
14
by Josh C
only apply friction on ground. adjust some X speeds to account for that |
126 |
if (!this.jumping && !this.falling) { |
127 |
if (this.friction.x) |
|
128 |
vel.x = me.utils.applyFriction(vel.x,this.friction.x); |
|
129 |
if (this.friction.y) |
|
130 |
vel.y = me.utils.applyFriction(vel.y,this.friction.y); |
|
131 |
} |
|
11
by Josh C
built system to have multiple "max velocities" for different sources of |
132 |
|
133 |
// cap velocity |
|
134 |
/*if (vel.y !=0) |
|
135 |
vel.y = vel.y.clamp(-this.maxVel.y,this.maxVel.y); |
|
136 |
if (vel.x !=0) |
|
137 |
vel.x = vel.x.clamp(-this.maxVel.x,this.maxVel.x); */ |
|
138 |
||
139 |
return vel; |
|
4
by Josh C
platformer! |
140 |
} |
11
by Josh C
built system to have multiple "max velocities" for different sources of |
141 |
|
4
by Josh C
platformer! |
142 |
}); //PlayerEntity definition |