Browse Source

changes

master
mortie 7 years ago
parent
commit
c130996c46
3 changed files with 50 additions and 18 deletions
  1. 30
    8
      js/assets.js
  2. 19
    9
      js/entities.js
  3. 1
    1
      js/vec2.js

+ 30
- 8
js/assets.js View File

@@ -31,6 +31,8 @@ function ImageSource(src, frameh) {
"Height isn't evenly divisible by frame height. "+
"Height: "+this.height+", frame height: "+this.frameh);
}

console.log(this);
}.bind(this);

this.img.src = "assets/"+src+ext;
@@ -38,9 +40,10 @@ function ImageSource(src, frameh) {
ImageSource.prototype.draw = function(ctx, step) {
if (!step) step = 0;

ctx.drawImage(this.img, 0, 0,
this.width, this.frameh,
ctx.drawImage(this.img,
0, this.frameh * step,
this.width, this.frameh,
0, 0,
this.width, this.frameh);
}

@@ -50,9 +53,8 @@ function Animation(imgSrc, type, loop, fps) {
this.imgSrc = imgSrc;
this.fps = fps || 12;
this.waitTime = 1000 / this.fps;
this.done = false;

this.doStep = true;
this.doStep = false;

if (type === "forward" || type === "bounce") {
this.step = 0;
@@ -81,10 +83,17 @@ Animation.prototype.nextFrame = function() {
var next = this.step + this.direction;
if (next < 0 || next > this.imgSrc.steps - 1) {
if (this.loop) {
this.direction = -this.direction;
next = this.step + this.direction;
if (this.type === "bounce") {
this.direction = -this.direction;
next = this.step + this.direction;
} else if (this.type === "forward") {
if (next < 0)
next = this.imgSrc.steps - 1;
else
next = 0;
}
} else {
this.done = true;
this.doStep = false;
return;
}
}
@@ -99,8 +108,21 @@ Animation.prototype.draw = function(ctx) {
if (this.doStep) {
this.nextFrame();
this.doStep = false;
this.setTimeout(function() {
setTimeout(function() {
this.doStep = true;
}.bind(this), this.waitTime);
}
}
Animation.prototype.play = function() {
if (this.step !== -1)
this.step = 0;
this.doStep = true;
}
Animation.prototype.setStep = function(step) {
if (step > this.imgSrc.steps - 1)
this.step = this.imgSrc.steps - 1;
else if (step < 0)
this.step = 0;
else
this.step = step;
}

+ 19
- 9
js/entities.js View File

@@ -10,32 +10,38 @@ function Player(game) {
this.invincibleTimeout = null;
this.started = false;
this.rotation = 0;
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.rise = function() {
this.erectLevel += 1;

Player.prototype.setBox = function() {
var w = 30 + ((this.erectLevel - 1) * 15);
var h = 15 + ((this.erectLevel - 1) * 6);
this.shape.push(new Box(w, h, { x: -(w/2), y: -(h/2) }));

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.shape.pop();

this.erectLevel -= 1;
if (this.erectLevel === 0)
this.lose();
else
this.setInvincible(500);

this.setBox();
}
Player.prototype.setInvincible = function(time) {
clearTimeout(this.invincibleTimeout);
@@ -91,7 +97,7 @@ Player.prototype.move = function() {
this.game.camera.x = this.pos.x - (this.game.canvas.width / 7);

// Rotate
this.rotation = this.vel.rotation();
this.rotation = this.vel.angle();
}
Player.prototype.lose = function() {
alert("You died!");
@@ -103,6 +109,7 @@ Player.prototype.draw = function(ctx) {
else
ctx.fillStyle = "#f5f5dc";
ctx.rotate(this.rotation);

this.shape.draw(ctx);
}

@@ -114,7 +121,7 @@ function Obstacle(game, x, y) {
makeEnt(this, game, 100);
this.pos.set({ x: x, y: y });

var w = 40;
var w = 70;
var h = game.canvas.height;
var gap = 200;

@@ -141,9 +148,12 @@ function PowerUp(game, x, y, type) {
this.shape.push(new Box(30, 30));
}
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())

+ 1
- 1
js/vec2.js View File

@@ -63,6 +63,6 @@ Vec2.prototype.rotate = function(rad) {
return this;
}

Vec2.prototype.rotation = function() {
Vec2.prototype.angle = function() {
return Math.atan2(this.y, this.x);
}

Loading…
Cancel
Save