123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
-
- /*
- * Player
- */
-
- function Player(game) {
- makeEnt(this, game, 100);
- this.moves = true;
- this.invincible = false;
- 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.setBox = function() {
- var w = 30 + ((this.erectLevel - 1) * 15);
- 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.update = function() {
- // Jump
- if (this.game.presses.jump) {
- this.started = true;
- this.vel.y = -1.3;
- }
-
- // Gravity and movement
- if (this.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() {
- alert("You died!");
- this.game.stop();
- }
- Player.prototype.draw = function(ctx) {
- if (this.invincible)
- ctx.fillStyle = "#d5d5bf";
- else
- ctx.fillStyle = "#f5f5dc";
- ctx.rotate(this.rotation);
-
- this.shape.draw(ctx);
- }
-
- /*
- * Obstacle
- */
-
- function Obstacle(game, x, y) {
- makeEnt(this, game, 100);
- this.pos.set({ x: x, y: y });
-
- 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) }));
- }
- Obstacle.prototype.draw = function(ctx) {
- this.shape.draw(ctx);
- }
- Obstacle.prototype.update = function() {
- if (this.game.camera.x > this.pos.x + this.shape.width())
- this.dead = true;
- }
-
- /*
- * PowerUp
- */
-
- function PowerUp(game, x, y, type) {
- makeEnt(this, game, 100);
- this.pos.set({ x: x, y: y });
- this.type = 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())
- this.dead = true;
- }
|