123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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;
- }
- }
- }
- }
- };
|