|
| 1 | +var sprites = { |
| 2 | + ship: { sx: 0, sy: 0, w: 37, h: 42, frames: 1 }, |
| 3 | + missile: { sx: 0, sy: 30, w: 2, h: 10, frames: 1 }, |
| 4 | + enemy_purple: { sx: 37, sy: 0, w: 42, h: 43, frames: 1 }, |
| 5 | + enemy_bee: { sx: 79, sy: 0, w: 37, h: 43, frames: 1 }, |
| 6 | + enemy_ship: { sx: 116, sy: 0, w: 42, h: 43, frames: 1 }, |
| 7 | + enemy_circle: { sx: 158, sy: 0, w: 32, h: 33, frames: 1 }, |
| 8 | + explosion: { sx: 0, sy: 64, w: 64, h: 64, frames: 12 }, |
| 9 | + enemy_missile: { sx: 9, sy: 42, w: 3, h: 20, frame: 1 }, |
| 10 | +}; |
| 11 | + |
| 12 | +var enemies = { |
| 13 | + straight: { x: 0, y: -50, sprite: "enemy_ship", health: 10, E: 100 }, |
| 14 | + ltr: { |
| 15 | + x: 0, |
| 16 | + y: -100, |
| 17 | + sprite: "enemy_purple", |
| 18 | + health: 10, |
| 19 | + B: 75, |
| 20 | + C: 1, |
| 21 | + E: 100, |
| 22 | + missiles: 2, |
| 23 | + }, |
| 24 | + circle: { |
| 25 | + x: 250, |
| 26 | + y: -50, |
| 27 | + sprite: "enemy_circle", |
| 28 | + health: 10, |
| 29 | + A: 0, |
| 30 | + B: -100, |
| 31 | + C: 1, |
| 32 | + E: 20, |
| 33 | + F: 100, |
| 34 | + G: 1, |
| 35 | + H: Math.PI / 2, |
| 36 | + }, |
| 37 | + wiggle: { |
| 38 | + x: 100, |
| 39 | + y: -50, |
| 40 | + sprite: "enemy_bee", |
| 41 | + health: 20, |
| 42 | + B: 50, |
| 43 | + C: 4, |
| 44 | + E: 100, |
| 45 | + firePercentage: 0.001, |
| 46 | + missiles: 2, |
| 47 | + }, |
| 48 | + step: { |
| 49 | + x: 0, |
| 50 | + y: -50, |
| 51 | + sprite: "enemy_circle", |
| 52 | + health: 10, |
| 53 | + B: 150, |
| 54 | + C: 1.2, |
| 55 | + E: 75, |
| 56 | + }, |
| 57 | +}; |
| 58 | + |
| 59 | +var OBJECT_PLAYER = 1, |
| 60 | + OBJECT_PLAYER_PROJECTILE = 2, |
| 61 | + OBJECT_ENEMY = 4, |
| 62 | + OBJECT_ENEMY_PROJECTILE = 8, |
| 63 | + OBJECT_POWERUP = 16; |
| 64 | + |
| 65 | +var startGame = function () { |
| 66 | + var ua = navigator.userAgent.toLowerCase(); |
| 67 | + |
| 68 | + // Only 1 row of stars |
| 69 | + if (ua.match(/android/)) { |
| 70 | + Game.setBoard(0, new Starfield(50, 0.6, 100, true)); |
| 71 | + } else { |
| 72 | + Game.setBoard(0, new Starfield(20, 0.4, 100, true)); |
| 73 | + Game.setBoard(1, new Starfield(50, 0.6, 100)); |
| 74 | + Game.setBoard(2, new Starfield(100, 1.0, 50)); |
| 75 | + } |
| 76 | + Game.setBoard( |
| 77 | + 3, |
| 78 | + new TitleScreen("Alien Invasion", "Press fire to start playing", playGame) |
| 79 | + ); |
| 80 | +}; |
| 81 | + |
| 82 | +var level1 = [ |
| 83 | + // Start, End, Gap, Type, Override |
| 84 | + [0, 4000, 500, "step"], |
| 85 | + [6000, 13000, 800, "ltr"], |
| 86 | + [10000, 16000, 400, "circle"], |
| 87 | + [17800, 20000, 500, "straight", { x: 50 }], |
| 88 | + [18200, 20000, 500, "straight", { x: 90 }], |
| 89 | + [18200, 20000, 500, "straight", { x: 10 }], |
| 90 | + [22000, 25000, 400, "wiggle", { x: 150 }], |
| 91 | + [22000, 25000, 400, "wiggle", { x: 100 }], |
| 92 | +]; |
| 93 | + |
| 94 | +var playGame = function () { |
| 95 | + var board = new GameBoard(); |
| 96 | + board.add(new PlayerShip()); |
| 97 | + board.add(new Level(level1, winGame)); |
| 98 | + Game.setBoard(3, board); |
| 99 | + Game.setBoard(5, new GamePoints(0)); |
| 100 | +}; |
| 101 | + |
| 102 | +var winGame = function () { |
| 103 | + Game.setBoard( |
| 104 | + 3, |
| 105 | + new TitleScreen("You win!", "Press fire to play again", playGame) |
| 106 | + ); |
| 107 | +}; |
| 108 | + |
| 109 | +var loseGame = function () { |
| 110 | + Game.setBoard( |
| 111 | + 3, |
| 112 | + new TitleScreen("You lose!", "Press fire to play again", playGame) |
| 113 | + ); |
| 114 | +}; |
| 115 | + |
| 116 | +var Starfield = function (speed, opacity, numStars, clear) { |
| 117 | + // Set up the offscreen canvas |
| 118 | + var stars = document.createElement("canvas"); |
| 119 | + stars.width = Game.width; |
| 120 | + stars.height = Game.height; |
| 121 | + var starCtx = stars.getContext("2d"); |
| 122 | + |
| 123 | + var offset = 0; |
| 124 | + |
| 125 | + // If the clear option is set, |
| 126 | + // make the background black instead of transparent |
| 127 | + if (clear) { |
| 128 | + starCtx.fillStyle = "#000"; |
| 129 | + starCtx.fillRect(0, 0, stars.width, stars.height); |
| 130 | + } |
| 131 | + |
| 132 | + // Now draw a bunch of random 2 pixel |
| 133 | + // rectangles onto the offscreen canvas |
| 134 | + starCtx.fillStyle = "#FFF"; |
| 135 | + starCtx.globalAlpha = opacity; |
| 136 | + for (var i = 0; i < numStars; i++) { |
| 137 | + starCtx.fillRect( |
| 138 | + Math.floor(Math.random() * stars.width), |
| 139 | + Math.floor(Math.random() * stars.height), |
| 140 | + 2, |
| 141 | + 2 |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + // This method is called every frame |
| 146 | + // to draw the starfield onto the canvas |
| 147 | + this.draw = function (ctx) { |
| 148 | + var intOffset = Math.floor(offset); |
| 149 | + var remaining = stars.height - intOffset; |
| 150 | + |
| 151 | + // Draw the top half of the starfield |
| 152 | + if (intOffset > 0) { |
| 153 | + ctx.drawImage( |
| 154 | + stars, |
| 155 | + 0, |
| 156 | + remaining, |
| 157 | + stars.width, |
| 158 | + intOffset, |
| 159 | + 0, |
| 160 | + 0, |
| 161 | + stars.width, |
| 162 | + intOffset |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + // Draw the bottom half of the starfield |
| 167 | + if (remaining > 0) { |
| 168 | + ctx.drawImage( |
| 169 | + stars, |
| 170 | + 0, |
| 171 | + 0, |
| 172 | + stars.width, |
| 173 | + remaining, |
| 174 | + 0, |
| 175 | + intOffset, |
| 176 | + stars.width, |
| 177 | + remaining |
| 178 | + ); |
| 179 | + } |
| 180 | + }; |
| 181 | + |
| 182 | + // This method is called to update |
| 183 | + // the starfield |
| 184 | + this.step = function (dt) { |
| 185 | + offset += dt * speed; |
| 186 | + offset = offset % stars.height; |
| 187 | + }; |
| 188 | +}; |
| 189 | + |
| 190 | +var PlayerShip = function () { |
| 191 | + this.setup("ship", { vx: 0, reloadTime: 0.25, maxVel: 200 }); |
| 192 | + |
| 193 | + this.reload = this.reloadTime; |
| 194 | + this.x = Game.width / 2 - this.w / 2; |
| 195 | + this.y = Game.height - Game.playerOffset - this.h; |
| 196 | + |
| 197 | + this.step = function (dt) { |
| 198 | + if (Game.keys["left"]) { |
| 199 | + this.vx = -this.maxVel; |
| 200 | + } else if (Game.keys["right"]) { |
| 201 | + this.vx = this.maxVel; |
| 202 | + } else { |
| 203 | + this.vx = 0; |
| 204 | + } |
| 205 | + |
| 206 | + this.x += this.vx * dt; |
| 207 | + |
| 208 | + if (this.x < 0) { |
| 209 | + this.x = 0; |
| 210 | + } else if (this.x > Game.width - this.w) { |
| 211 | + this.x = Game.width - this.w; |
| 212 | + } |
| 213 | + |
| 214 | + this.reload -= dt; |
| 215 | + if (Game.keys["fire"] && this.reload < 0) { |
| 216 | + Game.keys["fire"] = false; |
| 217 | + this.reload = this.reloadTime; |
| 218 | + |
| 219 | + this.board.add(new PlayerMissile(this.x, this.y + this.h / 2)); |
| 220 | + this.board.add(new PlayerMissile(this.x + this.w, this.y + this.h / 2)); |
| 221 | + } |
| 222 | + }; |
| 223 | +}; |
| 224 | + |
| 225 | +PlayerShip.prototype = new Sprite(); |
| 226 | +PlayerShip.prototype.type = OBJECT_PLAYER; |
| 227 | + |
| 228 | +PlayerShip.prototype.hit = function (damage) { |
| 229 | + if (this.board.remove(this)) { |
| 230 | + loseGame(); |
| 231 | + } |
| 232 | +}; |
| 233 | + |
| 234 | +var PlayerMissile = function (x, y) { |
| 235 | + this.setup("missile", { vy: -700, damage: 10 }); |
| 236 | + this.x = x - this.w / 2; |
| 237 | + this.y = y - this.h; |
| 238 | +}; |
| 239 | + |
| 240 | +PlayerMissile.prototype = new Sprite(); |
| 241 | +PlayerMissile.prototype.type = OBJECT_PLAYER_PROJECTILE; |
| 242 | + |
| 243 | +PlayerMissile.prototype.step = function (dt) { |
| 244 | + this.y += this.vy * dt; |
| 245 | + var collision = this.board.collide(this, OBJECT_ENEMY); |
| 246 | + if (collision) { |
| 247 | + collision.hit(this.damage); |
| 248 | + this.board.remove(this); |
| 249 | + } else if (this.y < -this.h) { |
| 250 | + this.board.remove(this); |
| 251 | + } |
| 252 | +}; |
| 253 | + |
| 254 | +var Enemy = function (blueprint, override) { |
| 255 | + this.merge(this.baseParameters); |
| 256 | + this.setup(blueprint.sprite, blueprint); |
| 257 | + this.merge(override); |
| 258 | +}; |
| 259 | + |
| 260 | +Enemy.prototype = new Sprite(); |
| 261 | +Enemy.prototype.type = OBJECT_ENEMY; |
| 262 | + |
| 263 | +Enemy.prototype.baseParameters = { |
| 264 | + A: 0, |
| 265 | + B: 0, |
| 266 | + C: 0, |
| 267 | + D: 0, |
| 268 | + E: 0, |
| 269 | + F: 0, |
| 270 | + G: 0, |
| 271 | + H: 0, |
| 272 | + t: 0, |
| 273 | + reloadTime: 0.75, |
| 274 | + reload: 0, |
| 275 | +}; |
| 276 | + |
| 277 | +Enemy.prototype.step = function (dt) { |
| 278 | + this.t += dt; |
| 279 | + |
| 280 | + this.vx = this.A + this.B * Math.sin(this.C * this.t + this.D); |
| 281 | + this.vy = this.E + this.F * Math.sin(this.G * this.t + this.H); |
| 282 | + |
| 283 | + this.x += this.vx * dt; |
| 284 | + this.y += this.vy * dt; |
| 285 | + |
| 286 | + var collision = this.board.collide(this, OBJECT_PLAYER); |
| 287 | + if (collision) { |
| 288 | + collision.hit(this.damage); |
| 289 | + this.board.remove(this); |
| 290 | + } |
| 291 | + |
| 292 | + if (Math.random() < 0.01 && this.reload <= 0) { |
| 293 | + this.reload = this.reloadTime; |
| 294 | + if (this.missiles == 2) { |
| 295 | + this.board.add(new EnemyMissile(this.x + this.w - 2, this.y + this.h)); |
| 296 | + this.board.add(new EnemyMissile(this.x + 2, this.y + this.h)); |
| 297 | + } else { |
| 298 | + this.board.add(new EnemyMissile(this.x + this.w / 2, this.y + this.h)); |
| 299 | + } |
| 300 | + } |
| 301 | + this.reload -= dt; |
| 302 | + |
| 303 | + if (this.y > Game.height || this.x < -this.w || this.x > Game.width) { |
| 304 | + this.board.remove(this); |
| 305 | + } |
| 306 | +}; |
| 307 | + |
| 308 | +Enemy.prototype.hit = function (damage) { |
| 309 | + this.health -= damage; |
| 310 | + if (this.health <= 0) { |
| 311 | + if (this.board.remove(this)) { |
| 312 | + Game.points += this.points || 100; |
| 313 | + this.board.add(new Explosion(this.x + this.w / 2, this.y + this.h / 2)); |
| 314 | + } |
| 315 | + } |
| 316 | +}; |
| 317 | + |
| 318 | +var EnemyMissile = function (x, y) { |
| 319 | + this.setup("enemy_missile", { vy: 200, damage: 10 }); |
| 320 | + this.x = x - this.w / 2; |
| 321 | + this.y = y; |
| 322 | +}; |
| 323 | + |
| 324 | +EnemyMissile.prototype = new Sprite(); |
| 325 | +EnemyMissile.prototype.type = OBJECT_ENEMY_PROJECTILE; |
| 326 | + |
| 327 | +EnemyMissile.prototype.step = function (dt) { |
| 328 | + this.y += this.vy * dt; |
| 329 | + var collision = this.board.collide(this, OBJECT_PLAYER); |
| 330 | + if (collision) { |
| 331 | + collision.hit(this.damage); |
| 332 | + this.board.remove(this); |
| 333 | + } else if (this.y > Game.height) { |
| 334 | + this.board.remove(this); |
| 335 | + } |
| 336 | +}; |
| 337 | + |
| 338 | +var Explosion = function (centerX, centerY) { |
| 339 | + this.setup("explosion", { frame: 0 }); |
| 340 | + this.x = centerX - this.w / 2; |
| 341 | + this.y = centerY - this.h / 2; |
| 342 | +}; |
| 343 | + |
| 344 | +Explosion.prototype = new Sprite(); |
| 345 | + |
| 346 | +Explosion.prototype.step = function (dt) { |
| 347 | + this.frame++; |
| 348 | + if (this.frame >= 12) { |
| 349 | + this.board.remove(this); |
| 350 | + } |
| 351 | +}; |
| 352 | + |
| 353 | +window.addEventListener("load", function () { |
| 354 | + Game.initialize("game", sprites, startGame); |
| 355 | +}); |
0 commit comments