Permalink
Browse files

Mario gets bullets

added bullet firing functionality to player class
  • Loading branch information...
1 parent 6bafeb0 commit 5f2a5b76b67862ce61eb971708ed0dbde3448582 @usernameusername usernameusername committed Dec 19, 2012
Showing with 83 additions and 1 deletion.
  1. +83 −1 player.js
View
@@ -1,4 +1,21 @@
-Player = function()
+Bullet = function(vel, rectangle)
+{
+ this.velocity = vel;
+ this.rect = rectangle;
+
+ this.Update = function()
+ {
+ this.rect.x += this.velocity.x;
+ this.rect.y += this.velocity.y;
+ }
+
+ this.Draw = function()
+ {
+ this.rect.Draw(ctx);
+ }
+}
+
+Player = function() // Player class
{
this.rect = new Rectangle(250, 0, 16, 16);
this.rect.color.r = 0;
@@ -8,6 +25,11 @@ Player = function()
this.speed = 1.5;
this.moving = false;
+ this.lookingRight = true;
+
+ this.bullets = new Array();
+ this.shotBullet = false;
+
this.mayJump = false;
this.jumping = false;
this.JUMP_MAX = 2.5;
@@ -31,6 +53,54 @@ Player = function()
}
}
+ this.Shoot = function()
+ {
+ if (!this.shotBullet)
+ {
+ var b = new Rectangle(this.rect.x + (this.rect.width / 2) - 4, this.rect.y + (this.rect.height / 2) - 4, 8, 8);
+ b.color.g = 0;
+ b.color.b = 0;
+
+ var vel = new Vector2(0, 0);
+ if (this.lookingRight)
+ vel.x = 3;
+ else
+ vel.x = -3;
+
+ var bul = new Bullet(vel, b);
+
+ this.bullets.push(bul);
+
+ this.shotBullet = true;
+ }
+ }
+
+ this.UpdateBullets = function()
+ {
+ for (var i = 0; i < this.bullets.length; i++)
+ {
+ this.bullets[i].Update();
+
+ var b = this.bullets[i];
+
+ var done = false;
+ if (b.rect.x + b.rect.width < 0)
+ done = true;
+ else if (b.rect.x > canvas.width)
+ done = true;
+ if (b.rect.y + b.rect.height < 0)
+ done = true;
+ else if (b.rect.y > canvas.height)
+ done = true;
+
+ if (done)
+ {
+ this.bullets.RemoveAt(i);
+ i--;
+ }
+ }
+ }
+
this.Jump = function()
{
if (this.mayJump)
@@ -49,13 +119,22 @@ Player = function()
this.animation.SetRow(2);
this.rect.x -= this.speed;
this.moving = true;
+ this.lookingRight = false;
}
if (input.right || input.d)
{
this.animation.SetRow(0);
this.rect.x += this.speed;
this.moving = true;
+ this.lookingRight = true;
}
+ if (input.space)
+ this.Shoot();
+ else
+ this.shotBullet = false;
+
+ this.UpdateBullets();
+
if (input.up || input.w)
this.Jump();
if (this.jumping)
@@ -82,6 +161,9 @@ Player = function()
this.Draw = function(ctx)
{
+ for (var i = 0; i < this.bullets.length; i++)
+ this.bullets[i].Draw(ctx);
+
//this.rect.Draw(ctx);
this.animation.Draw(ctx);
}

0 comments on commit 5f2a5b7

Please sign in to comment.