function GravityWellGame() { this.game = null; this.system = null; this.player = null; this.ships = []; this.bullets = []; this.systems = []; this.planets = []; this.camera = null; this.background = null; this.farBackground = null; this.state = {shipIndex: 1, planetIndex: 1, solarIndex: 1}; this.shipIndex = 1; this.planetIndex = 1; this.solarIndex = 1; this.settings = {}; this.settings.enableMouseControl = true; this.tickDelay = 60; this.init = function(systemObject, canvas) { this.game = this; this.system = systemObject; this.camera = new Camera(); this.camera.init(canvas); this.background = new Scenery(this.camera, 0.1); this.farBackground = new Scenery(this.camera, 0.05); this.systems.push(new SolarSystem(this.game, {x: -40000, y:-40000})); this.systems.push(new SolarSystem(this.game, {x: 0, y:-40000})); this.systems.push(new SolarSystem(this.game, {x: 40000, y:-40000})); this.systems.push(new SolarSystem(this.game, {x: -40000, y:0})); this.systems.push(new SolarSystem(this.game, {x: 0, y:0})); this.systems.push(new SolarSystem(this.game, {x: 40000, y:0})); this.systems.push(new SolarSystem(this.game, {x: -40000, y:40000})); this.systems.push(new SolarSystem(this.game, {x: 0, y:40000})); this.systems.push(new SolarSystem(this.game, {x: 40000, y:40000})); this.player = new Ship(this.game, new HumanController()); this.player.id = this.state.shipIndex++; for(var index in this.systems) { var solar = this.systems[index]; solar.id = this.state.solarIndex++; var solarPlanets = solar.spawnPlanets(this.state); var solarShips = solar.spawnShips(this.state); this.planets = this.planets.concat(solarPlanets); this.ships = this.ships.concat(solarShips); } var startingSystem = Math.min(Math.max(0, this.systems.length - 1), 4); var replaceShip = this.systems[startingSystem].ships[0]; this.ships.splice(this.ships.indexOf(replaceShip), 1); this.ships.push(this.player); this.systems[startingSystem].ships[0] = this.player; for(var index in this.systems) { var solar = this.systems[index]; solar.init(); } this.ui = new HeadsUpDisplay(this.game, this.camera); this.ui.init(this.player, canvas); }; this.tick = function() { for(var index in this.systems) { var solar = this.systems[index]; solar.tick(); } }; this.updateDelta = function(canvas, delta){ this.tickDelay -= delta; if(this.tickDelay <= 0) { this.tick(); this.tickDelay = 60; } this.update(canvas); //this.player.update(delta); for(var index in this.ships) { var ship = this.ships[index]; ship.update(delta); } for(var index in this.systems) { var solar = this.systems[index]; solar.update(delta); } for(var index in this.bullets) { var bullet = this.bullets[index]; bullet.update(delta); } this.camera.update(canvas, this.player, delta); this.farBackground.update(canvas, delta); this.background.update(canvas, delta); this.ui.update(delta); }; this.update = function(canvas) { //this.player.control(); for(var index in this.ships) { var ship = this.ships[index]; ship.control(); } //bullet-to-ship collision for(var index in this.bullets) { var bullet = this.bullets[index]; for(var shipIndex in this.ships) { var ship = this.ships[shipIndex]; if(!ship.isDead) { bullet.checkHit(ship); } } //todo: also check planets } for(var index in this.ships) { var ship = this.ships[index]; if(ship.isDead) { continue; } //ship-to-ship collision for(var shipIndex in this.ships) { var otherShip = this.ships[shipIndex]; if(ship == otherShip) { continue; } ship.checkHit(otherShip); } //ship-to-planet contact for(var systemIndex in this.systems) { var otherSystem = this.systems[systemIndex]; otherSystem.checkCrash(ship); } } }; this.draw = function(context) { this.farBackground.draw(context); this.background.draw(context); context.save(); context.translate(this.camera.x, this.camera.y); for(var index in this.systems) { var solar = this.systems[index]; solar.draw(context); } for(var index in this.ships) { var ship = this.ships[index]; ship.draw(context); } for(var index in this.bullets) { var bullet = this.bullets[index]; bullet.draw(context); } context.restore(); this.ui.draw(context); }; this.spawnBullet = function(ship) { var newBullet = new Bullet(this.game, ship.id); newBullet.fire(ship.position, ship.velocity, ship.targetAngle); this.bullets.push(newBullet); }; this.expireBullet = function(bullet) { this.bullets.splice(this.bullets.indexOf(bullet), 1); } this.handleResize = function(canvas) { this.camera.handleResize(canvas); } this.mouseMove = function(canvas, x, y) { if(this.settings.enableMouseControl) { this.player.controller.mouseSteer(this.player, this.camera, x, y); } }; this.mouseDown = function(canvas, button, x, y) { if(this.settings.enableMouseControl) { switch(button) { case 0: this.player.shoot(); break; case 2: this.player.controller.mouseSteer(this.player, this.camera, x, y); this.player.accelerate(); break; } } }; this.mouseUp = function(canvas, button, x, y) { if(this.settings.enableMouseControl) { switch(button) { case 2: this.player.idle(); break; } } }; this.touchStart = function(canvas, x, y) { if(this.settings.enableMouseControl) { this.player.controller.mouseSteer(this.player, this.camera, x, y); this.player.accelerate(); } }; this.touchMove = function(canvas, x, y) { if(this.settings.enableMouseControl) { this.player.controller.mouseSteer(this.player, this.camera, x, y); this.player.accelerate(); } }; this.touchEnd = function(canvas, x, y) { if(this.settings.enableMouseControl) { this.player.idle(); } }; this.contextMenu = function(canvas, event) { }; this.getNearestPlanet = function(solar, ship) { var closestDistance = Number.MAX_SAFE_INTEGER; var closestPlanet = null; for(var index in solar.planets) { var planet = solar.planets[index]; var distance = this.calculateDistanceTo(ship, planet); if(distance < closestDistance) { closestPlanet = planet; closestDistance = distance; } } return closestPlanet; } this.getNearestSystem = function(game, ship) { var closestDistance = Number.MAX_SAFE_INTEGER; var closestSystem = null; for(var index in game.systems) { var solar = game.systems[index]; var distance = this.calculateDistanceTo(ship, solar); if(distance < closestDistance) { closestSystem = solar; closestDistance = distance; } } return closestSystem; } this.getNearestShip = function(game, ship) { var closestDistance = Number.MAX_SAFE_INTEGER; var closestShip = null; for(var index in game.ships) { var other = game.ships[index]; if(other == ship) { continue; } var distance = this.calculateDistanceTo(ship, other); if(distance < closestDistance) { closestShip = other; closestDistance = distance; } } return closestShip; } this.calculateDistanceTo = function(ship, other) { return Math.sqrt(Math.pow(ship.position.x - other.position.x, 2) + Math.pow(ship.position.y - other.position.y, 2)); } this.calculateAngleTo = function(ship, other) { return Math.atan2(ship.position.y - other.position.y , ship.position.x - other.position.x); } this.calculateAngleToPlanet = function(ship, solar, planet) { return Math.atan2((solar.position.y + planet.position.y) - ship.position.y , (solar.position.x + planet.position.x) - ship.position.x); } };