function SolarSystem(game, startPos) { this.id = 0; this.position = startPos; this.planets = []; this.ships = []; this.systemColor = null; this.radius = 400; this.mass = 150000; this.teamId = 0; this.init = function() { this.teamId = this.id; this.systemColor = this.getColor(this.teamId); for(var index in this.planets) { var planet = this.planets[index]; planet.init(this); } var randomPlanet = this.planets[parseInt(this.planets.length * Math.random())]; for(var index in this.ships) { var ship = this.ships[index]; ship.init(this.systemColor, this); } } this.getColor = function(teamId) { switch(teamId) { case 1: return "crimson"; case 2: return "orangered"; case 3: return "gold"; case 4: return "limegreen"; case 5: return "dodgerblue"; case 6: return "aqua"; case 7: return "darkmagenta"; case 8: return "ghostwhite"; case 9: return "orchid"; } } this.spawnPlanets = function(state){ for(var i = 1; i <= 7; i++) { var isPlanet = parseInt(8 * Math.random()) > 4 || i == 4; if(isPlanet) { var planet = new Planet(game, i); planet.id = state.planetIndex++; if(i == 4) { planet.teamId = this.id; planet.teamColor = this.getColor(this.id); } this.planets.push(planet); } } return this.planets; } this.spawnShips = function(state){ for(var i = 0; i < 4; i++) { var ship = new Ship(game, new BotController(game)); ship.id = state.shipIndex++; this.ships.push(ship); } return this.ships; } this.tick = function() { for(var index in this.planets) { var planet = this.planets[index]; planet.tick(); } }; this.update = function(delta) { for(var index in this.planets) { var planet = this.planets[index]; planet.update(delta); } } this.draw = function(context) { //star context.fillStyle = "#000000"; context.strokeStyle = "#DDDD33"; context.lineWidth = 3; context.save(); context.translate(this.position.x, this.position.y); context.beginPath(); context.arc(0, 0, this.radius, 0, 2 * Math.PI); context.fill(); context.stroke(); context.restore(); for(var index in this.planets) { var planet = this.planets[index]; planet.draw(context); } context.save(); context.translate(this.position.x, this.position.y); context.font = "16px sans-serif"; context.textAlign = "center"; context.fillStyle = "#DDDDDD"; context.strokeStyle = "#333333"; context.lineWidth = 2; var textMetrics = context.measureText("System " + this.id); var textWidth = textMetrics.width; context.fillText("System " + this.id, 0, 0); context.restore(); } this.addGravity = function(ship) { var nearestSystemAngle = game.calculateAngleTo(this, ship); var nearestSystemDistance = game.calculateDistanceTo(this, ship); ship.acceleration.x += system.formatFloat((this.mass * (1 / Math.pow(nearestSystemDistance, 2))) * Math.cos(nearestSystemAngle)); ship.acceleration.y += system.formatFloat((this.mass * (1 / Math.pow(nearestSystemDistance, 2))) * Math.sin(nearestSystemAngle)); }; this.checkCrash = function(ship) { if(ship.isDead) { return; } var distance = game.calculateDistanceTo(ship, this); if(this.radius + (ship.width / 2) > distance) { ship.touching = this; ship.die(); return; } for(var planetIndex in this.planets) { var otherPlanet = this.planets[planetIndex]; if(ship.touching == otherPlanet) { ship.touching = null; } var distance = game.calculateDistanceTo(ship, otherPlanet); if(otherPlanet.radius + (ship.width / 2) > distance) { ship.touching = otherPlanet; if(Math.abs(ship.velocity.x) + Math.abs(ship.velocity.y) > 3) { ship.die(); return; } } } } };