123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
-
- /*
- * 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;
-
- if (this.game.started)
- this.force.add({ x: 0.09, 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);
- this.img = assets.imgs.player;
-
- game.started = false;
- this.moves = true;
- this.inputListener = true;
- this.layer = 1;
-
- this.invincible = false;
- this.invincibleTimeout = null;
- this.started = false;
- this.rotation = 0;
- this.bigBox = new Box(60, 40, { x: -30, y: -20 });
- this.box = new Box();
- this.shape.push(this.box);
-
- 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);
- this.setBox();
- }
- Player.prototype.lower = function() {
- if (this.invincible)
- return;
-
- this.erectLevel -= 1;
- if (this.erectLevel === 0)
- this.lose();
- else
- this.setInvincible(500);
-
- this.setBox();
- }
- Player.prototype.setInvincible = function(time) {
- clearTimeout(this.invincibleTimeout);
-
- this.invincible = true;
- this.invincibleTimeout = setTimeout(function() {
- this.invincible = false;
- }.bind(this), time);
- }
- Player.prototype.onInput = function(name, down) {
-
- // Jump
- if (name === "jump" && down) {
- 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 (!this.shape.collidesWith(ent.shape))
- continue;
-
- if (ent instanceof Obstacle) {
- this.lower();
- return;
- } else if (ent instanceof PowerUp) {
- this.rise();
- 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.game.stop(this.pos.x / 10);
- }
- Player.prototype.draw = function(ctx) {
- ctx.rotate(this.rotation);
- var scale = 0.4 + this.erectLevel / 8;
- ctx.scale(scale, scale);
- ctx.translate(70, -50);
- ctx.rotate(Math.PI / 2);
- this.img.draw(ctx);
- }
-
- /*
- * Obstacle
- */
-
- function Obstacle(game, x, y) {
- makeEnt(this, game, 100);
- this.img = assets.imgs.wall;
-
- 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.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;
- }
|