-
Notifications
You must be signed in to change notification settings - Fork 3
/
player.js
216 lines (185 loc) · 6.67 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
AstronautImgRight = new Image();
AstronautImgRight.src = 'images/astronautright.png'
AstronautImgLeft = new Image();
AstronautImgLeft.src = 'images/astronautleft.png'
AstronautImgDead = new Image();
AstronautImgDead.src = 'images/astronautdead.png'
function Player(game) {
// Singleton
if(Player.instance_){
return Player.instance_
}
this.instance_ = this
this.Game = game;
this.Space = null;
this.Platform = null;
this.canvasCtx = null;
this.canvas = null;
this.charX = 0; //left bound of the character
this.charY = 0;
this.rightPressed = false; //true iff right keyboard was pressed
this.leftPressed = false; //true iff left keyboard was pressed
this.document = document;
this.offEdge = false;
this.nImage = 0;
this.frameCount = 0;
this.frameLoopCycle = 0;
this.facingRight = false;
this.charWidth = 0
this.charHeight = 0
this.charSpeed = 0 /*default movement speed of the player per frame */
this.init();
}
Player.prototype = {
init: function(){
this.space = this.Game.Space
this.Platform = this.Game.Platform
this.offEdge = false
this.nImage = 0;
this.frameCount = 0;
this.frameLoopCycle = 5;
this.facingRight = false;
this.charWidth = 32
this.charHeight = 58
this.charSpeed = 3 /*default movement speed of the player per frame */
this.canvasCtx = this.Game.canvasCtx
this.canvas = this.canvasCtx.canvas;
this.charX = this.canvas.width / 2; //left bound of the character
this.charY = this.canvas.height-this.charHeight-this.Platform.platformHeight;
this.document.addEventListener("keydown", this.keyDownHandler.bind(this), false);
this.document.addEventListener("keyup", this.keyUpHandler.bind(this), false);
this.rightPressed = false; //true iff right keyboard was pressed during gameplay
this.leftPressed = false; //true iff left keyboard was pressed during gameplay
},
//Input Handling
keyDownHandler : function(e) {
if (!this.Game.gameOver) {
if (e.key == "Right" || e.key == "ArrowRight") {
this.rightPressed = true;
Player.c += 1;
} else if (e.key == "Left" || e.key == "ArrowLeft") {
this.leftPressed = true;
}
}
},
keyUpHandler : function(e) {
if(e.key == "Right" || e.key == "ArrowRight") {
this.rightPressed = false;
} else if (e.key == "Left" || e.key == "ArrowLeft") {
this.leftPressed = false;
}
},
update : function() {
if (!this.Game.gameOver) {
if (this.rightPressed && this.charX < this.canvas.width-this.charWidth) {
this.charX += this.charSpeed;
} else if (this.leftPressed && this.charX > 0) {
this.charX -= this.charSpeed;
}
if (this.checkStarCollisions(this.space.stars)) {
this.space.removeMeteors();
this.space.removeStars();
this.Game.Score.bonus += 100;
this.Game.Score.addBonus = true;
}
this.Game.gameOver = this.checkGameOverCollisions(this.space.meteors);
this.drawChar();
}
},
getTouch : function(obj1,obj2){
var Obj1Center=[obj1.x+obj1.r, obj1.y+obj1.r];
var Obj2Center=[obj2.x+obj2.r,obj2.y+obj2.r];
var distance=Math.sqrt(Math.pow(Obj2Center[0]-Obj1Center[0], 2) + Math.pow( Obj2Center[1]-Obj1Center[1], 2) );
return distance < (obj1.r+obj2.r);
},
collide : function(x1, y1, w1, h1, r1, i1, i2, x2, y2, r2) {
var circle1={x:x1+i1+r1, y:y1+i1+r1, r:r1};
var circle2={x:x2+r2, y:y2+r2, r:r2};
var rect={x:x1+i2, y:y1+0.8*i2, w:7*w1/16, h:h1-0.9*i2};
var distX = Math.abs(circle2.x - rect.x-rect.w/2);
var distY = Math.abs(circle2.y - rect.y-rect.h/2);
// this.canvasCtx.beginPath();
// this.canvasCtx.arc(circle2.x, circle2.y, circle2.r, 0, 2*Math.PI);
// this.canvasCtx.strokeStyle = "red";
// this.canvasCtx.stroke();
// this.canvasCtx.closePath();
// this.canvasCtx.beginPath();
// this.canvasCtx.arc(circle1.x, circle1.y, circle1.r, 0, 2*Math.PI);
// this.canvasCtx.strokeStyle = "blue";
// this.canvasCtx.stroke();
// this.canvasCtx.closePath();
// this.canvasCtx.beginPath();
// this.canvasCtx.rect(rect.x, rect.y, rect.w, rect.h);
// this.canvasCtx.stroke();
// this.canvasCtx.closePath();
if(this.getTouch(circle1, circle2)){
return true;
}
var dx=distX-rect.w/2;
var dy=distY-rect.h/2;
return (dx*dx+dy*dy<=(circle2.r*circle2.r));
},
checkGameOverCollisions : function(meteors) {
for (i = 0; i < meteors.length; i++) {
var radius = (meteors[i].imgWidth/4);
var ycircle = meteors[i].yPos + meteors[i].imgHeight - (3*radius);
var xcircle = meteors[i].xPos + radius;
var indent = 3*this.charWidth/32;
var astroRadius = 13*this.charWidth/32;
var rectIndent = 9*this.charWidth/32;
if(this.collide(this.charX, this.charY, this.charWidth, this.charHeight, astroRadius, indent, rectIndent, xcircle, ycircle, radius)){
return true;
}
}
if (this.charX + (this.charWidth * 0.8) < this.Platform.platformX || this.charX + (this.charWidth * 0.2) > this.Platform.platformX + this.Platform.platformWidth) {
this.offEdge = true;
return true;
}
return false;
},
checkStarCollisions: function (stars) {
for (i = 0; i < stars.length; i++) {
var radius = 0.9*(stars[i].imgWidth/2);
var ycircle = stars[i].yPos;
var xcircle = stars[i].xPos;
var indent = 3*this.charWidth/32;
var astroRadius = 13*this.charWidth/32;
var rectIndent = 9*this.charWidth/32;
if(this.collide(this.charX, this.charY, this.charWidth, this.charHeight, astroRadius, indent, rectIndent, xcircle, ycircle, radius)){
return true;
}
}
},
drawDeadChar : function(){
if (this.offEdge){
this.charY += .5
}
this.canvasCtx.drawImage(AstronautImgDead, 0, 0, this.charWidth, this.charHeight, this.charX, this.charY, this.charWidth, this.charHeight);
},
drawChar : function() {
if (this.rightPressed){
this.canvasCtx.drawImage(AstronautImgRight, this.nImage * this.charWidth, 0, this.charWidth, this.charHeight, this.charX, this.charY, this.charWidth, this.charHeight);
this.facingRight = true;
}
else if (this.leftPressed){
this.canvasCtx.drawImage(AstronautImgLeft, this.nImage * this.charWidth, 0, this.charWidth, this.charHeight, this.charX, this.charY, this.charWidth, this.charHeight);
this.facingRight = false;
}
else if (this.facingRight){
this.canvasCtx.drawImage(AstronautImgRight, this.charWidth, 0, this.charWidth, this.charHeight, this.charX, this.charY, this.charWidth, this.charHeight);
}
else if (!this.facingRight){
this.canvasCtx.drawImage(AstronautImgLeft, this.charWidth, 0, this.charWidth, this.charHeight, this.charX, this.charY, this.charWidth, this.charHeight);
}
if (this.frameCount % this.frameLoopCycle == 0) {
this.nImage++;
}
if (this.nImage > 3) {
this.nImage = 0;
}
this.frameCount++;
},
reset: function(){
this.init()
}
};