/moongirl

To get this branch, use:
bzr branch /bzr/moongirl
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
// player
var PlayerEntity = me.ObjectEntity.extend({
	// Constructor
	init: function(x, y, settings) {
	    // superclass constructor?
	    this.parent(x, y, settings);

	    this.debugtimer = 0.0;

	    // walking, jumping speed:
	    this.setVelocity(1, 17); // set accel + max vel
	    this.setMaxVelocity(255,255); // just something big so we don't hit it
	    this.gravity = 0.8;
	    this.friction.x = 0.3;

	    //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 = 3.7;
	    JUMP_SPEED = 17;
	    JETPACK_X_VEL = new me.Vector2d(11,17);
	    JETPACK_Y_VEL = 25;

	    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) {
		    if (me.input.isKeyPressed('left')) {
			this.vel.y = -JETPACK_X_VEL.y;
			this.vel.x = -JETPACK_X_VEL.x;
		    } else if (me.input.isKeyPressed('right')) {
			this.vel.y = -JETPACK_X_VEL.y;
			this.vel.x = JETPACK_X_VEL.x;
		    } else {
			// go straight up
			this.vel.y = -JETPACK_Y_VEL;
		    }
		} else {
		    //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();

	    /* this.debugtimer += me.timer.tick;
	    if (this.debugtimer > 100) {
		dbg = "XVel: " + this.vel.x;
		dbg += "<br>me.timer.tick: " + me.timer.tick;

		dbgEl = document.getElementById('debug');
		//dbgEl.replaceChild(document.createTextNode(dbg), dbgEl.firstChild);
		dbgEl.innerHTML = dbg;

		this.debugtimer = 0.0;
		} */

	    // "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) {
		// check if falling / jumping
		this.falling = (vel.y > 0);
		this.jumping = this.falling?false:this.jumping;

		// apply a constant gravity (if not on a ladder)
		if (!this.onladder)
		    this.addYVel(this.gravity * me.timer.tick, TERMINAL_VEL);
	    }
	    
	    // apply friction
	    if (!this.jumping && !this.falling) {
		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