-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
247 lines (211 loc) · 6.73 KB
/
player.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
var LEFT = 0;
var RIGHT = 1;
var ANIM_IDLE_LEFT = 0;
var ANIM_JUMP_LEFT = 1;
var ANIM_WALK_LEFT = 2;
var ANIM_IDLE_RIGHT = 3;
var ANIM_JUMP_RIGHT = 4;
var ANIM_WALK_RIGHT = 5;
var ANIM_MAX = 6;
var Player = function() {
this.sprite = new Sprite("ChuckNorris.png");
this.sprite.buildAnimation(12, 8, 165, 126, 0.05, [0, 1, 2, 3, 4, 5, 6, 7]);
this.sprite.buildAnimation(12, 8, 165, 126, 0.05, [8, 9, 10, 11, 12]);
this.sprite.buildAnimation(12, 8, 165, 126, 0.05, [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]);
this.sprite.buildAnimation(12, 8, 165, 126, 0.05, [52, 53, 54, 55, 56, 57, 58, 59]);
this.sprite.buildAnimation(12, 8, 165, 126, 0.05, [60, 61, 62, 63, 64]);
this.sprite.buildAnimation(12, 8, 165, 126, 0.05, [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78]);
for(var i=0; i<ANIM_MAX; i++)
{
this.sprite.setAnimationOffset(i, -55, -87);
}
this.startPosition = new Vector2();
this.startPosition.set(9*TILE, 0*TILE); // 'TILE' is the width of a tile
this.position = new Vector2();
this.position.set(this.startPosition.x, this.startPosition.y);
this.width = 159;
this.height = 163;
this.velocity = new Vector2();
this.falling = true;
this.jumping = false;
this.direction = LEFT;
this.lives = 3;
this.isAlive = true;
this.cooldownTimer = 0;
};
Player.prototype.update = function(deltaTime)
{
this.sprite.update(deltaTime);
var left = false;
var right = false;
var jump = false;
// check keypress events
if(keyboard.isKeyDown(keyboard.KEY_LEFT) == true) {
left = true;
this.direction = LEFT;
if (this.sprite.currentAnimation != ANIM_WALK_LEFT && this.jumping == false)
{
this.sprite.setAnimation(ANIM_WALK_LEFT);
}
}
else if(keyboard.isKeyDown(keyboard.KEY_RIGHT) == true) {
right = true;
this.direction = RIGHT;
if (this.sprite.currentAnimation != ANIM_WALK_RIGHT && this.jumping == false)
{
this.sprite.setAnimation(ANIM_WALK_RIGHT);
}
}
else
{
if (this.jumping == false && this.falling == false)
{
if (this.direction == LEFT)
{
if (this.sprite.currentAnimation != ANIM_IDLE_LEFT)
{
this.sprite.setAnimation(ANIM_IDLE_LEFT);
}
}
else
{
if (this.sprite.currentAnimation != ANIM_IDLE_RIGHT)
{
this.sprite.setAnimation(ANIM_IDLE_RIGHT);
}
}
}
}
if(keyboard.isKeyDown(keyboard.KEY_UP) == true) {
jump = true;
}
if (this.cooldownTimer > 0)
{
this.cooldownTimer -= deltaTime;
}
if (keyboard.isKeyDown(keyboard.KEY_SPACE) == true && this.cooldownTimer <= 0)
{
sfxFire.play();
this.cooldownTimer = 0.3;
}
var wasleft = this.velocity.x < 0;
var wasright = this.velocity.x > 0;
var falling = this.falling;
var ddx = 0; // acceleration
var ddy = GRAVITY;
if (left)
ddx = ddx - ACCEL; // player wants to go left
else if (wasleft)
ddx = ddx + FRICTION; // player was going left, but not any more
if (right)
ddx = ddx + ACCEL; // player wants to go right
else if (wasright)
ddx = ddx - FRICTION; // player was going right, but not any more
if (jump && !this.jumping && !falling)
{
ddy = ddy - JUMP; // apply an instantaneous (large) vertical impulse
this.jumping = true;
if (this.direction == LEFT)
{
this.sprite.setAnimation(ANIM_JUMP_LEFT);
}
else
{
this.sprite.setAnimation(ANIM_JUMP_RIGHT);
}
}
// calculate the new position and velocity:
this.position.y = Math.floor(this.position.y + (deltaTime * this.velocity.y));
this.position.x = Math.floor(this.position.x + (deltaTime * this.velocity.x));
this.velocity.x = bound(this.velocity.x + (deltaTime * ddx), -MAXDX, MAXDX);
this.velocity.y = bound(this.velocity.y + (deltaTime * ddy), -MAXDY, MAXDY);
if ((wasleft && (this.velocity.x > 0)) ||
(wasright && (this.velocity.x < 0)))
{
// clamp at zero to prevent friction from making us jiggle side to side
this.velocity.x = 0;
}
// we’ll insert code here later
// collision detection
// Our collision detection logic is greatly simplified by the fact that the
// player is a rectangle and is exactly the same size as a single tile.
// So we know that the player can only ever occupy 1, 2 or 4 cells.
// This means we can short-circuit and avoid building a general purpose
// collision detection engine by simply looking at the 1 to 4 cells that
// the player occupies:
var tx = pixelToTile(this.position.x);
var ty = pixelToTile(this.position.y);
var nx = (this.position.x)%TILE; // true if player overlaps right
var ny = (this.position.y)%TILE; // true if player overlaps below
var cell = cellAtTileCoord(LAYER_PLATFORMS, tx, ty);
var cellright = cellAtTileCoord(LAYER_PLATFORMS, tx + 1, ty);
var celldown = cellAtTileCoord(LAYER_PLATFORMS, tx, ty + 1);
var celldiag = cellAtTileCoord(LAYER_PLATFORMS, tx + 1, ty + 1);
// If the player has vertical velocity, then check to see if they have hit a platform
// below or above, in which case, stop their vertical velocity, and clamp their
// y position:
if (this.velocity.y > 0) {
if ((celldown && !cell) || (celldiag && !cellright && nx)) {
// clamp the y position to avoid falling into platform below
this.position.y = tileToPixel(ty);
this.velocity.y = 0; // stop downward velocity
this.falling = false; // no longer falling
this.jumping = false; // (or jumping)
ny = 0; // no longer overlaps the cells below
}
}
else if (this.velocity.y < 0) {
if ((cell && !celldown) || (cellright && !celldiag && nx)) {
// clamp the y position to avoid jumping into platform above
this.position.y = tileToPixel(ty + 1);
this.velocity.y = 0; // stop upward velocity
// player is no longer really in that cell, we clamped them to the cell below
cell = celldown;
cellright = celldiag; // (ditto)
ny = 0; // player no longer overlaps the cells below
}
}
if (this.velocity.x > 0) {
if ((cellright && !cell) || (celldiag && !celldown && ny)) {
// clamp the x position to avoid moving into the platform we just hit
this.position.x = tileToPixel(tx);
this.velocity.x = 0; // stop horizontal velocity
}
}
else if (this.velocity.x < 0) {
if ((cell && !cellright) || (celldown && !celldiag && ny)) {
// clamp the x position to avoid moving into the platform we just hit
this.position.x = tileToPixel(tx + 1);
this.velocity.x = 0; // stop horizontal velocity
}
}
if (this.position.y > canvas.height)
{
this.onDeath();
}
}
Player.prototype.draw = function()
{
this.sprite.draw(context, this.position.x - worldOffsetX, this.position.y);
}
Player.prototype.onDeath = function()
{
if (this.isAlive == true)
{
this.lives -= 1;
if (this.lives >= 0)
{
this.respawn();
}
else
{
this.isAlive = false;
this.lives = 0;
}
}
}
Player.prototype.respawn = function()
{
this.position.set(this.startPosition.x, this.startPosition.y);
this.sprite.setAnimation(ANIM_IDLE_RIGHT);
}