123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- 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);
- }
- };
|