function Planet(game, depth) { this.id = 0; this.position = {x: 800, y: 0}; this.velocity = {x: 0, y: 0}; this.angleRadians = 0; this.fireRate = 5; this.lifetime = 100; this.timeToDie = 0; this.isDead = false; this.planetColor = "#AAAAAA"; this.radius = 100; this.orbitRadius = 2000; this.angleRadians = 0; this.orbitSpeed = 1; this.rotationRadians = 0; this.rotationSpeed = 0.001; this.system = null; this.mass = 1000; this.teamId = 0; this.teamColor = "#DDDDDD"; this.type = "barren"; this.resources = 50; this.resourceRate = 0; this.init = function(solar) { this.system = solar; this.orbitRadius = 2000 * (depth + 1); this.angleRadians = (2 * Math.PI) * Math.random(); this.position.x = solar.position.x + (this.orbitRadius * Math.cos(this.angleRadians)); this.position.y = solar.position.y + (this.orbitRadius * Math.sin(this.angleRadians)); this.radius = 100; this.mass = 1000; this.type = "barren"; if(depth > 2 && depth < 6) { this.planetColor = "#33DD33"; this.radius = 140; this.mass = 2800; this.type = "habitable"; this.resourceRate = 1; } this.rotationRadians = (2 * Math.PI) * Math.random(); //this.orbitSpeed = 0.00001 * Math.pow(1/1000, depth); this.orbitSpeed = 0.00001;// * Math.pow(1/10, depth); } this.tick = function() { this.resources += this.resourceRate; }; this.update = function(delta) { this.angleRadians += this.orbitSpeed; this.rotationRadians += this.rotationSpeed; this.position.x = this.system.position.x + (this.orbitRadius * Math.cos(this.angleRadians)); this.position.y = this.system.position.y + (this.orbitRadius * Math.sin(this.angleRadians)); } this.draw = function(context) { var solar = {x: 0, y: 0}; solar.x = this.system.position.x; solar.y = this.system.position.y; //context.save(); //context.translate(solar.x, solar.y); //context.rotate(this.angleRadians); context.save(); context.fillStyle = "#000000"; context.translate(this.position.x, this.position.y); context.beginPath(); context.arc(0, 0, this.radius, 0, 2 * Math.PI); context.fill(); context.restore(); context.strokeStyle = "#3333DD"; context.lineWidth = 1; context.save(); context.translate(this.system.position.x, this.system.position.y); context.beginPath(); context.arc(0, 0, this.orbitRadius, 0, 2 * Math.PI); context.stroke(); context.restore(); context.save(); context.strokeStyle = this.planetColor; context.lineWidth = 2; context.translate(this.position.x, this.position.y); context.rotate(this.rotationRadians); context.beginPath(); context.arc(0, 0, this.radius, 0, 2 * Math.PI); context.stroke(); //for(var index in this.structures) { // var building = this.structures[index]; // building.draw(context); //} /*context.beginPath(); context.fillStyle = "#33BBBB"; context.strokeStyle = "#33DDDD"; context.rect(10, 10, 50, 30); context.fill(); context.stroke(); context.restore();*/ context.restore(); context.save(); context.translate(this.position.x, this.position.y); //ownership flag context.beginPath(); context.fillStyle = this.teamColor; context.rect(-8, -6, 16, 12); context.fill(); context.font = "16px sans-serif"; context.textAlign = "center"; context.fillStyle = "#DDDDDD"; context.strokeStyle = "#333333"; context.lineWidth = 2; var textMetrics = context.measureText("Planet " + this.id); var textWidth = textMetrics.width; context.fillText("Planet " + this.id, 0, 0); context.restore(); } this.addGravity = function(ship) { var nearestPlanetAngle = game.calculateAngleTo(this, ship); var nearestPlanetDistance = game.calculateDistanceTo(this, ship); ship.acceleration.x += system.formatFloat((this.mass * (1 / Math.pow(nearestPlanetDistance, 2))) * Math.cos(nearestPlanetAngle)); ship.acceleration.y += system.formatFloat((this.mass * (1 / Math.pow(nearestPlanetDistance, 2))) * Math.sin(nearestPlanetAngle)); }; this.claim = function(ship) { this.teamId = ship.teamId; this.teamColor = ship.shipColor; ship.setSpawn(this); }; this.canSpawnShip = function() { return this.resources > 10; }; this.spawnShip = function() { this.resources -= 10; } };