-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayerChar.js
More file actions
90 lines (78 loc) · 2.14 KB
/
Copy pathplayerChar.js
File metadata and controls
90 lines (78 loc) · 2.14 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
// Simple sprite class
var px = PLAYER_START_X;
var py = PLAYER_START_Y;
var playerBullets = [];
var bulletPositionX;
var bulletPositionY;
var count = 0;
var playerAlive = true;
function playerSprite(args) {
this.image = new Image();
this.image.src = args.imgSrc;
this.x = px;
this.y = py;
this.imgFrames = args.frames || 1;
this.frame = 0;
// Draw the sprite to the screen
this.Draw = function() {
if(playerAlive) {
ctx.globalAlpha = 1;
var x = px;
var y = py;
ctx.drawImage(this.image, this.frame, 0, args.spw,
args.spw, x, y, args.spw, args.spw);
this.frame += PLAYER_CHAR_WIDTH;
if (this.frame >= this.image.width) this.frame = 0;
}
};
};
function Rectangle(x, y, w, h, fill){
this.xVal = x || 0;
this.yVal = y || 0;
this.w = w || 0;
this.h = h || 0;
this.fill = fill || "#FFFF00";
this.dx = 0;
this.dy = -8;
this.isVisible = true;
}
function addBullet () {
if(count % 2 === 0){
bulletPositionX = px;
bulletPositionY = py;
}
else{
bulletPositionX = px + PLAYER_CHAR_WIDTH;
bulletPositionY = py;
}
count++;
var rect = new Rectangle(bulletPositionX, bulletPositionY, 2, 6);
playerBullets.push(rect);
}
function inBounds(r){
if(r.xVal >= 0 && r.xVal <= STAGE_WIDTH && r.yVal >= 0 && r.yVal <= STAGE_HEIGHT)
return true;
else
return false;
}
function updateBullets(){
for(var i = 0; i < playerBullets.length; i++){
if(playerBullets[i].isVisible && inBounds(playerBullets[i])){
playerBullets[i].isVisible = true;
playerBullets[i].yVal += playerBullets[i].dy;
}
else
playerBullets[i].isVisible = false;
}
}
function drawBullets(ctx){
this.ctx = ctx;
for(var i = playerBullets.length-1; i > 0; i--){
if(playerBullets[i].isVisible) {
ctx.globalAlpha = 1;
var rec = playerBullets[i];
ctx.fillStyle = rec.fill;
ctx.fillRect(rec.xVal + rec.dx, rec.yVal + rec.dy, rec.w, rec.h);
}
}
}