function Bullet(game, ownerId) { this.position = {x: 0, y: 0}; this.velocity = {x: 0, y: 0}; this.acceleration = {x: 0, y: 0}; this.angleRadians = 0; this.fireRate = 5; this.lifetime = 100; this.timeToDie = 0; this.isDead = false; this.owner = ownerId; this.damage = 1; this.fire = function(startPosition, startVelocity, directionRadians) { this.position.x = startPosition.x + 8 * Math.cos(directionRadians); this.position.y = startPosition.y + 8 * Math.sin(directionRadians); this.velocity.x = startVelocity.x; this.velocity.y = startVelocity.y; this.angleRadians = directionRadians; this.velocity.x += this.fireRate * Math.cos(directionRadians); this.velocity.y += this.fireRate * Math.sin(directionRadians); this.timeToDie = this.lifetime; } this.update = function(delta) { this.timeToDie -= delta; if(this.timeToDie <= 0) { game.expireBullet(this); return; } //bullets affected by gravity /*this.acceleration.x = 0; this.acceleration.y = 0; var nearestSystem = game.getNearestSystem(game, this); nearestSystem.addGravity(this); for(var index in nearestSystem.planets) { var planet = nearestSystem.planets[index]; planet.addGravity(this); } this.velocity.x += this.acceleration.x; this.velocity.y += this.acceleration.y; */ this.position.x += this.velocity.x * delta; this.position.y += this.velocity.y * delta; } this.draw = function(context) { if(this.isDead) { return; } //bullet context.fillStyle = "#DDDDDD"; context.beginPath(); context.arc(this.position.x, this.position.y, 2, 0, 2 * Math.PI); context.fill(); //missile /* context.strokeStyle = "#DDDDDD"; context.beginPath(); context.moveTo(truePos.x, truePos.y); context.lineTo(truePos.x + 15* Math.cos(this.angleRadians), truePos.y + 15* Math.sin(this.angleRadians)); context.stroke(); */ } this.checkHit = function(ship) { var distance = Math.sqrt(Math.pow(ship.position.x - this.position.x, 2) + Math.pow(ship.position.y - this.position.y, 2)); if(ship.id != ownerId && ship.width / 2 >= distance) { ship.takeDamage(this.damage, ownerId); this.isDead = true; } } };