-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
executable file
·177 lines (172 loc) · 5.07 KB
/
Copy pathplayer.js
File metadata and controls
executable file
·177 lines (172 loc) · 5.07 KB
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
Player = function() {
C.debug("created player");
this.xp = 0;
this.weapon = Pistol;
this.x = C.canvas.w / 2;
this.y = C.canvas.h / 2;
this.radius = 10;
this.direction = 0;
this.maxspeed = 0.5; //maximum speed the player can move
this.vel = 0; //w,s keys velocity
this.vel2 = 0; //a,d keys velocity
this.bullets = this.weapon.mag_size;
this.fire_anim = false;
this.sprite_idx = 1; //holds the index for the firing animation sprites
this.sprite = C.images.player[0];
this.reload_time = C.fps; //as fps
this.health = 100;
}
Player.prototype.bind_keys = function() {
//bind the keyboard keys for player movement
C.debug("binding keys");
document.onkeydown = function(event) {
var keyCode = (event === null ? window.event.keyCode : event.keyCode);
switch(keyCode) {
case 27:
C.pause = !C.pause;
case 87:
C.player.vel = -C.player.maxspeed;
break; //w
case 83:
C.player.vel = C.player.maxspeed;
break; //s
case 65:
C.player.vel2 = -C.player.maxspeed;
break; //a
case 68:
C.player.vel2 = C.player.maxspeed;
break; //d
}
};
document.onkeyup = function(event) {
var keyCode = (event === null ? window.event.keyCode : event.keyCode);
switch(keyCode) {
case 83:
case 87:
C.player.vel = 0;
break;
case 65:
case 68:
C.player.vel2 = 0;
C.player.angle = 0;
break;
}
};
}
Player.prototype.move = function() {
//calculate the next player coordinates
//don't move from the view point of the actor but the player.
this.direction = Math.atan2(C.mouse.currentx - this.x,
C.mouse.currenty - this.y);
this.x += this.vel2;
this.y += this.vel;
if (this.vel2 != 0 || this.vel != 0) {
var interval = Math.ceil((C.frame / C.fps)*2);
var len = C.images.player.length;
this.sprite_idx = (interval % len);
this.sprite = C.images.player[this.sprite_idx];
}
if (this.fire_anim) {
var interval = Math.ceil(C.frame / C.fps);
var len = C.images.playerfire.length;
this.sprite = C.images.playerfire[(interval % len)];
} else {
this.sprite = C.images.player[this.sprite_idx];
}
this.perk_collision_detect();
}
Player.prototype.draw = function() {
//draw the player on the canvas
var w = this.sprite.width/2;
var h = this.sprite.height/2;
C.ctx.save();
C.ctx.translate(this.x,this.y);
C.ctx.rotate(Math.PI-this.direction);
C.ctx.drawImage(this.sprite,0-w,0-h);
C.ctx.restore();
this.draw_healthbar();
this.check_reload_finished();
if (this.reloading)
this.reload_anim();
}
Player.prototype.perk_collision_detect = function() {
var perks = C.perks;
for (var i=0,len=C.perks.length;i<len;i++) {
var p = perks[i];
if (Util.detect_collision(p.x,p.y,p.radius,
this.x,this.y,this.radius)) {
console.log('collected perk of type %s', p.type.name);
if (p.type.name == "submachine gun") this.weapon = Submachinegun;
if (p.type.name == 'pistol') this.weapon = Pistol;
if (p.type.name == 'shotgun') this.weapon = Shotgun;
this.bullets = this.weapon.mag_size;
C.perks.splice(i,1);
break;
}
}
}
Player.prototype.draw_healthbar = function() {
C.ctx.drawImage(C.images.heart, C.canvas.w-170,12);
C.ctx.beginPath();
C.ctx.strokeStyle = "red";
C.ctx.moveTo(C.canvas.w-150, 20);
C.ctx.lineTo(C.canvas.w-150+this.health, 20);
C.ctx.closePath();
C.ctx.stroke();
C.ctx.beginPath();
C.ctx.strokeStyle = "white";
C.ctx.moveTo(C.canvas.w-150+this.health, 20);
C.ctx.lineTo(C.canvas.w-150+this.health+(100-this.health), 20);
C.ctx.closePath();
C.ctx.stroke();
}
Player.prototype.fire = function() {
//file a projectile form the weapon
if (this.reloading) return;
if (C.frame < this.weapon.last_fire_frame + this.weapon.fire_interval)
return;
if (this.bullets < 1) {
this.reload();
return;
}
this.weapon.fire();
C.player.fire_anim = true;
setTimeout(function() {C.player.fire_anim = false;}, 100);
}
Player.prototype.check_reload_finished = function() {
if (this.reloading && C.frame > this.reload_start_frame + this.reload_time) {
console.log("reloading finished");
this.bullets = this.weapon.mag_size;
this.reloading = false;
}
}
Player.prototype.reload = function() {
//reload the weapon, the bullets are loaded at the reload finished event.
console.log("reloading started");
this.reload_start_frame = C.frame;
this.reloading = true;
}
Player.prototype.reload_anim = function() {
C.ctx.save();
C.ctx.translate(this.x+this.radius+5,this.y+this.radius+5);
var sector = (C.frame - this.reload_start_frame)/this.reload_time;
C.ctx.beginPath();
C.ctx.strokeStyle = "rgb(255,0,0)";
C.ctx.arc(0,0,11,0,Math.PI*2,true);
C.ctx.closePath();
C.ctx.stroke();
C.ctx.beginPath();
C.ctx.fillStyle = "rgba(128,128,128,0.6)";
C.ctx.arc(0, 0, 10,
0, 2 * Math.PI * sector, true);
C.ctx.fill();
C.ctx.restore();
}
Player.prototype.add_xp = function() {
var p = C.dead.length/C.total_fired
this.xp += Math.floor(1000 * p);
}
Player.prototype.zombie_collide = function(zombie) {
this.health -= zombie.damage;
if (this.health<0) C.gameover();
}