/* * Background */ function Background(game) { makeEnt(this, game, 100); this.img = assets.imgs.background; this.img1x = 0; this.img1y = -300; this.img2x = 0; this.img2y = -100; this.collude = false; this.moves = true; this.started = false; this.pos.set({ x: -400, y: 0 }); } Background.prototype.update = function() { if (!this.img.ready) return; if (this.img.ready && this.img2x === 0) this.img2x = this.img1x + this.img.width * 2; this.vel.set({ x: this.game.ids.player.vel.x * 0.9, y: 0 }); if (this.img1x + this.img.width * 2 + this.pos.x < this.game.camera.x) { this.img1x = this.img2x; this.img2x += this.img.width * 2; var tmpy = this.img1y; this.img1y = this.img2y; this.img2y = tmpy; } } Background.prototype.draw = function(ctx){ ctx.save(); ctx.translate(this.img1x, this.img1y); ctx.scale(2, 2); this.img.draw(ctx); ctx.restore(); ctx.translate(this.img2x, this.img2y); ctx.scale(2, 2); this.img.draw(ctx); } /* * Player */ function Player(game) { makeEnt(this, game, 100, "player"); this.img = assets.imgs.player; this.img_flipped = assets.imgs.player_flipped; this.hurt = assets.audio.hurt; this.loss = assets.audio.loss; this.flap = assets.audio.flap; this.chomp = assets.audio.chomp; this.hurtLoop = null; game.started = false; this.moves = true; this.inputListener = true; this.layer = 1; this.invincible = false; this.visible = true; this.invincibleTimeout = null; this.started = false; this.rotation = 0; this.bigBox = new Box(0, 0); this.box = new Box(); this.shape.push(this.box); this.bigShape = new Shape(this); this.bigShape.push(new Box(100, 70, { x: -50, y: -35 })); this.erectLevel = 0; this.rise(); this.pos.set({ x: 0, y: game.canvas.height / 2 }); } Player.prototype.setBox = function() { var w = 25 + ((this.erectLevel - 1) * 10); var h = 15 + ((this.erectLevel - 1) * 6); this.box.width = w; this.box.height = h; this.box.pos.x = -(w / 2); this.box.pos.y = -(h / 2); } Player.prototype.rise = function() { this.erectLevel += 1; this.setInvincible(200, false); this.setBox(); } Player.prototype.lower = function() { if (this.invincible) return; this.erectLevel -= 1; if (this.erectLevel === 0) { this.lose(); } else { this.hurt.play(); this.setInvincible(2000, true); } this.setBox(); } Player.prototype.setInvincible = function(time, hurt) { clearTimeout(this.invincibleTimeout); if (hurt) { clearInterval(this.hurtLoop); this.hurtLoop = setInterval(function() { this.visible = !this.visible; }.bind(this), 60); } this.invincible = true; this.invincibleTimeout = setTimeout(function() { this.invincible = false; this.visible = true; clearInterval(this.hurtLoop); this.hurtLoop = null; }.bind(this), time); } Player.prototype.onInput = function(name, down) { // Jump if (name === "jump" && down) { this.flap.play(); this.game.started = true; this.vel.y = -1.3; } } Player.prototype.update = function() { // Gravity and movement if (this.game.started) { this.force.y = 0.4; this.force.x = 0.13 + ((this.erectLevel - 1) * 0.02); } // Lose if we hit the edge if ( this.pos.y < 0 || this.pos.y > this.game.canvas.height - this.shape.height()) { this.lose(); return; } // Collide for (var i in this.game.entities) { var ent = this.game.entities[i]; if (ent === this) continue; if (ent instanceof Obstacle && this.shape.collidesWith(ent.shape)) { this.lower(); this.vel.x = -1; } else if (ent instanceof PowerUp && this.bigShape.collidesWith(ent.shape)) { this.rise(); this.chomp.play(); ent.dead = true; } } } Player.prototype.move = function() { // Move camera this.game.camera.x = this.pos.x - (this.game.canvas.width / 7); // Rotate this.rotation = this.vel.angle(); } Player.prototype.lose = function() { this.loss.play(); this.game.stop(this.pos.x / 100); } Player.prototype.draw = function(ctx) { if (!this.visible) return; ctx.rotate(this.rotation); var scale = 0.4 + this.erectLevel / 8; ctx.scale(scale, scale); ctx.translate(70, -50); ctx.rotate(Math.PI / 2); if (this.vel.y < 0) this.img.draw(ctx); else this.img_flipped.draw(ctx); } /* * Obstacle */ function Obstacle(game, x, y) { makeEnt(this, game, 100); this.img = assets.imgs.wall; this.overlap = assets.imgs.wall_overlap; this.collude = false; var w = 70; var h = game.canvas.height; var gap = 200; this.shape.push(new Box(w, h, { x: 0, y: -(h / 2) - (gap / 2) })); this.shape.push(new Box(w, h, { x: 0, y: (h / 2) + (gap / 2) })); this.pos.set({ x: x, y: y }); } Obstacle.prototype.draw = function(ctx) { ctx.translate(-100, this.game.canvas.height / 2 - 340); ctx.scale(0.9, 1.1); this.img.draw(ctx); } Obstacle.prototype.drawOverlay =function(ctx) { ctx.translate(-100, this.game.canvas.height / 2 - 340); ctx.scale(0.9, 1.1); this.overlap.draw(ctx); } Obstacle.prototype.update = function() { if (this.game.camera.x > this.pos.x + 800) this.dead = true; } /* * PowerUp */ function PowerUp(game, x, y, type) { makeEnt(this, game, 100); this.layer = 1; this.type = type; this.shape.push(new Box(30, 30)); this.pos.set({ x: x, y: y }); } PowerUp.prototype.draw = function(ctx) { ctx.fillStyle = "#33ee33"; ctx.strokeStyle = "#227722"; ctx.beginPath(); ctx.arc(15, 15, 15, 0, 2 * Math.PI); ctx.stroke(); ctx.fill(); } PowerUp.prototype.update = function() { if (this.game.camera.x > this.pos.x + this.shape.width()) this.dead = true; }