Skip to content

Commit

Permalink
you may now kill yourself
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpvar committed Jul 4, 2011
1 parent a9261f1 commit 2e6326f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
Binary file added .DS_Store
Binary file not shown.
36 changes: 32 additions & 4 deletions shared/projectile.js
Expand Up @@ -5,20 +5,44 @@
var CONST = require('./trig').CONST
}

exports.Projectile = function(data) {
this._ = {};
exports.Projectile = function(ship, data) {
this.ship = ship;
this._ = {
active : true
};
this.update(data);
};

exports.Projectile.prototype = {
tick : function() {

tick : function(scene) {
if (!this._.active) {
return;
}
var x = 10 * Math.cos(this._.rotation);
var y = 10 * Math.sin(this._.rotation);

this._.x += x;
this._.y += y;
var newx = this._.x;
var newy = this._.y;

// Simple AABB collision detection
var shipKeys = Object.keys(scene.players);

for (var i=0, l=shipKeys.length;i<l;i++) {
var ship = scene.players[shipKeys[i]];
if (ship._.x < newx && ship.width + ship._.x > newx &&
ship._.y < newy && ship.height + ship._.y > newy)
{
// ship got hit.
ship._.health -= 10;
if (ship._.health <= 0) {
ship._.health = 0;
ship._.crashed = true;
this._.active = false;
}
}
}
},
update : function(vals) {
for (var key in vals) {
Expand All @@ -28,6 +52,10 @@
}
},
render : function(ctx, timeDiff) {
if (!this._.active) {
//return;
}

ctx.save();
ctx.translate(400, 300)
ctx.scale(window.scale, window.scale)
Expand Down
2 changes: 1 addition & 1 deletion shared/scene.js
Expand Up @@ -54,7 +54,7 @@
var that = this;
Object.keys(this.players).forEach(function(player_key) {
var player = that.players[player_key];
player.tick();
player.tick(that);
});
},

Expand Down
30 changes: 18 additions & 12 deletions shared/ship.js
Expand Up @@ -45,17 +45,21 @@
ctx.scale(window.scale, window.scale)
ctx.translate(-400, -300)
ctx.translate(this._.x + 25, this._.y + 25); //that._.x, that._.y);

ctx.rotate(Math.PI/2);
var R=Math.floor(255*(100-this._.health))/100;
var G=Math.floor((255*(this._.health/100)));
var B=0;
ctx.fillStyle = 'rgba(255,255,255,0.3)';
ctx.fillRect(-45, -25, 5, 50);
ctx.fillStyle = 'rgba(' + R + ',' + G + ',' + B + ', 1.0)';
var size = (this._.health/100);
ctx.fillRect(-45, 25-(50*size), 5, 50*size);
ctx.rotate(-Math.PI/2);

ctx.rotate(this._.rotation + (Math.PI * 0.5));

var R=(255*(this._.health))/100
var G=(255*(100-this._.health))/100;
var B=0

ctx.fillStyle = 'rgb(' + R + ',' + G + ',' + R + ')';
ctx.fillRect(-25, - 25, 5, 50);

ctx.translate(-(this._.x + 25), -(this._.y + 25));

if (this._.landed) {
ctx.drawImage(imageCache.ship.default.landing[4], this._.x, this._.y);
} else if (this._.crashed) {
Expand All @@ -68,7 +72,9 @@

for (var i = 0; i < 13; i ++) {
var part = this._.animation.crashing[i];
ctx.drawImage(imageCache.ship.default.crashing[i], this._.animation.crashing[i].x, this._.animation.crashing[i].y);
if (part) {
ctx.drawImage(imageCache.ship.default.crashing[i], this._.animation.crashing[i].x, this._.animation.crashing[i].y);
}
};
} else if (this.image) {
var imageIndex = Math.floor((this.planet_distance() - 100) / 4);
Expand Down Expand Up @@ -159,7 +165,7 @@
this.projectiles = [];
vals[key].forEach(function(projectileData) {
// TODO: wtf is going on?
var projectile = new window.Projectile(that, projectileData);
var projectile = new window.Projectile(this, projectileData);
that.projectiles.push(projectile);
});
} else if (vals.hasOwnProperty(key)) {
Expand Down Expand Up @@ -289,7 +295,7 @@
var data = JSON.parse(JSON.stringify(this._))
data.x += 25;
data.y += 25;
var projectile = new Projectile(data);
var projectile = new Projectile(this, data);
this.projectiles.unshift(projectile);
this.projectiles.length = 100;
this._lastFire = Date.now();
Expand Down

0 comments on commit 2e6326f

Please sign in to comment.