-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.js
76 lines (55 loc) · 1.79 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
// ==========
// player STUFF
// ==========
"use strict";
/* jshint browser: true, devel: true, globalstrict: true */
/*
0 1 2 3 4 5 6 7 8
12345678901234567890123456789012345678901234567890123456789012345678901234567890
*/
// A generic contructor which accepts an arbitrary descriptor object
function player(descr) {
// Common inherited setup logic from Entity
this.setup(descr);
this.rememberResets();
// Default sprite, if not otherwise specified
this.sprite = this.sprite || g_sprites.player;
// Set normal drawing scale, and warp state off
this._scale = 1;
this.cy = g_canvas.height - g_sprites.player.height / 2;
};
player.prototype = new Entity();
player.prototype.rememberResets = function () {
// Remember my reset positions
this.reset_cx = this.cx;
this.reset_cy = this.cy;
};
player.prototype.KEY_LEFT = 'A'.charCodeAt(0);
player.prototype.KEY_RIGHT = 'D'.charCodeAt(0);
player.prototype.KEY_FIRE = ' '.charCodeAt(0);
// Initial, inheritable, default values
player.prototype.cx = g_canvas.width/2;
player.prototype.cy = g_canvas.height;
// HACKED-IN AUDIO (no preloading)
player.prototype.warpSound = new Audio(
"sounds/shipWarp.ogg");
player.prototype.update = function (du) {
if(keys[this.KEY_LEFT] && this.cx > g_sprites.player.width/2) this.cx -= 2;
if(keys[this.KEY_RIGHT] && this.cx < g_canvas.width-g_sprites.player.width/2) this.cx += 2;
// Handle firing
this.maybeFireWire();
};
player.prototype.maybeFireWire = function () {
if (keys[this.KEY_FIRE]) {
entityManager.fireWire(
this.cx);
}
};
player.prototype.render = function (ctx) {
var origScale = this.sprite.scale;
this.sprite.scale = this._scale;
this.sprite.drawCentredAt(
ctx, this.cx, this.cy, 0
);
this.sprite.scale = origScale;
};