-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
185 lines (155 loc) · 4.28 KB
/
game.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
;(function() {
var Game = function(canvasId) {
var canvas = document.getElementById(canvasId);
var screen = canvas.getContext('2d');
var gameSize = {x: canvas.width, y: canvas.height };
this.bodies = createInvaders(this).concat([new Player(this, gameSize)]);
var self = this;
loadSound("shoot.wav", function(shootSound) {
self.shootSound = shootSound;
var tick = function() {
self.update();
self.draw(screen, gameSize);
requestAnimationFrame(tick);
}
tick();
});
};
Game.prototype = {
update: function() {
var bodies = this.bodies;
var notCollidingWithAnything = function(b1) {
return bodies.filter(function(b2) {
return colliding(b1, b2);
}).length === 0;
}
this.bodies = this.bodies.filter(notCollidingWithAnything);
for (var i = 0; i < this.bodies.length; i++) {
this.bodies[i].update();
}
},
draw: function(screen, gameSize) {
screen.clearRect(0, 0, gameSize.x, gameSize.y);
for (var i = 0; i < this.bodies.length; i++) {
drawRect(screen, this.bodies[i]);
}
},
addBody: function(body) {
this.bodies.push(body);
},
invadersBelow: function(invader) {
return this.bodies.filter(function(b) {
return b instanceof Invader &&
b.center.y > invader.center.y &&
b.center.x - invader.center.x < invader.size.x;
}).length > 0;
}
};
var Player = function(game, gameSize) {
this.game = game;
this.size = {x: 15, y: 15};
this.center = {x: gameSize.x / 2, y: gameSize.y - this.size.x };
this.keyboarder = new Keyboarder();
};
Player.prototype = {
update: function() {
// move left and right
if (this.keyboarder.isDown(this.keyboarder.KEYS.LEFT)) {
this.center.x -= 2;
} else if (this.keyboarder.isDown(this.keyboarder.KEYS.RIGHT)) {
this.center.x += 2;
}
// fire bullets
if (this.keyboarder.isDown(this.keyboarder.KEYS.SPACE)) {
var bullet = new Bullet(
{x: this.center.x, y: this.center.y - this.size.x /2 },
{x: 0, y: -6}
);
this.game.addBody(bullet);
this.game.shootSound.load();
this.game.shootSound.play();
}
}
};
var Invader = function(game, center) {
this.game = game;
this.size = {x: 15, y: 15};
this.center = center;
this.patrolX = 0;
this.speedX = 0.3;
};
Invader.prototype = {
update: function() {
if (this.patrolX < 0 || this.patrolX > 40) {
this.speedX = -this.speedX;
}
this.center.x += this.speedX;
this.patrolX += this.speedX;
if (Math.random() > 0.995 && !this.game.invadersBelow(this)) {
var bullet = new Bullet(
{x: this.center.x, y: this.center.y + this.size.x /2 },
{x: Math.random() - 0.5, y: 2}
);
this.game.addBody(bullet);
}
}
};
var createInvaders = function(game) {
var invaders = [];
for (var i = 0; i < 24; i++) {
var x = 30 + (i % 8) * 30;
var y = 30 + (i % 3) * 30;
invaders.push(new Invader(game, {x: x, y: y}));
}
return invaders;
}
var Bullet = function(center, velocity) {
this.size = {x: 3, y: 3};
this.center = center;
this.velocity = velocity;
};
Bullet.prototype = {
update: function() {
this.center.x += this.velocity.x;
this.center.y += this.velocity.y;
}
};
// draws al the bodies in screen
var drawRect = function(screen, body) {
screen.fillRect(body.center.x - body.size.x / 2,
body.center.y - body.size.y / 2,
body.size.x, body.size.y)
}
var Keyboarder = function() {
var keyState = {};
window.onkeydown = function(e) {
keyState[e.keyCode] = true;
}
window.onkeyup = function(e) {
keyState[e.keyCode] = false;
}
this.isDown = function(keyCode) {
return keyState[keyCode] === true;
}
this.KEYS = {LEFT: 37, RIGHT: 39, SPACE: 32}
}
var colliding = function(b1, b2) {
return !(b1 == b2 ||
b1.center.x + b1.size.x / 2 < b2.center.x - b2.size.x / 2 ||
b1.center.y + b1.size.y / 2 < b2.center.y - b2.size.y / 2 ||
b1.center.x - b1.size.x / 2 > b2.center.x + b2.size.x / 2 ||
b1.center.y - b1.size.y / 2 > b2.center.y + b2.size.y / 2)
};
var loadSound = function(url, callback) {
var loaded = function() {
callback(sound);
sound.removeEventListener('canplaythrough', loaded);
};
var sound = new Audio(url);
sound.addEventListener('canplaythrough', loaded);
sound.load();
}
window.onload = function() {
new Game("screen");
}
})();