123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- require('SpriteAtlas');
- require('Background');
- require('Ship');
- require('Sector');
- require('Inventory');
- require('Crafting');
- require('PlayerHud');
- require('PlayerProgress');
- class SpaceRPGGame {
- constructor() {
- this.atlas = new SpriteAtlas(`./assets/sprites.xml`);
- this.center = { x: 0, y: 0 };
- this.playerHud = new PlayerHud();
- this.background = new Background();
- this.ship = new Ship();
- this.inventory = new Inventory();
- this.crafting = new Crafting();
- this.sector = new Sector();
- this.progress = new PlayerProgress();
- }
- init(canvas) {
- this.canvas = canvas;
- this.canvas.addEventListener("click", (event) => this.playerHud.dialog.advance(event));
- this.center = { x: Math.floor(this.canvas.width / 2), y: Math.floor(this.canvas.height / 2) };
- this.sector.init();
- this.progress.init();
- this.isLoading = true;
- this.atlas.load().then(() => {
- this.isLoading = false;
- this.playerHud.attach(canvas).then(() => this.playerHud.init());
- this.background.init();
- this.ship.init();
- this.inventory.init();
- this.crafting.init();
- });
- setTimeout(() => {
- this.playerHud.dialog.leftSay("COMM: Can you read me? You seem to be having a bit of trouble.");
- if (this.progress.getProgress("starting").name == "init") {
- setTimeout(() => {
- this.playerHud.dialog.leftSay("COMM: You'll want to repair your ship and head to the nearest station.");
- }, 5000);
- } else {
- setTimeout(() => {
- this.playerHud.dialog.leftSay("COMM: When you're up and running, head to the nearest station.");
- }, 5000);
- }
- }, 3000);
- }
- draw(context, delta) {
- if (this.isLoading) {
- return;
- }
- this.background.draw(context, delta);
- this.sector.draw(context, delta);
- this.ship.draw(context, delta);
- this.playerHud.draw(context, delta);
- }
- handleResize(event) {
- this.center = { x: Math.floor(this.canvas.width / 2), y: Math.floor(this.canvas.height / 2) };
- this.background.handleResize();
- this.playerHud.handleResize(event);
- this.sector.handleResize(event);
- }
- handleOrientation(event) {
- //this.ship.handleOrientation(event);
- }
- repairShip() {
- if (!game.playerHud.main.isProgressFinished()) {
- return;
- }
- if (this.ship.damage == 0) {
- //You do not need to repair
- return false;
- }
- if (!this.inventory.consumeShipRepairMaterials()) {
- //You do not have the necessary materials
- game.playerHud.dialog.rightSay('Insufficient Materials', 'error-message');
- if (this.progress.getProgress("starting").name == "repair") {
- game.playerHud.dialog.advance();
- this.progress.advanceProgress("starting");
- game.playerHud.crafting.show();
- setTimeout(() => {
- this.playerHud.dialog.leftSay("COMM: If you're out of spare parts, use the replicator to make more.");
- }, 2000);
- }
- return false;
- }
- game.playerHud.main.startProgress(1000, () => {
- if (this.progress.getProgress("starting").name == "init") {
- this.progress.advanceProgress("starting");
- game.playerHud.inventory.show();
- }
- if (this.ship.repair()) {
- game.playerHud.ship.disableRepair();
- this.background.move({ x: 0, y: 0 });
- this.ship.animateRotation(0);
- this.playerHud.ship.updateShipStatus();
- if (this.progress.getProgress("starting").name == "craft") {
- game.playerHud.dialog.advance();
- this.progress.advanceProgress("starting");
- game.playerHud.navigation.show();
- setTimeout(() => {
- this.playerHud.dialog.leftSay("COMM: Nice. You should be able to pick a destination on the System Nav.");
- }, 2000);
- }
- }
- });
- return false;
- }
- craftRecipe(recipeId) {
- if (!game.playerHud.main.isProgressFinished()) {
- return;
- }
- let recipe = this.crafting.getRecipe(recipeId);
- if (!recipe) {
- game.playerHud.dialog.rightSay('Invalid Recipe', 'error-message');
- }
- if (!this.inventory.consumeCraftingMaterials(recipe.ingredients)) {
- //not enough ingredients
- game.playerHud.dialog.rightSay('Insufficient Materials', 'error-message');
- return false;
- }
- game.playerHud.main.startProgress(recipe.duration, () => {
- this.inventory.addItems(recipe.results);
- if (this.progress.getProgress("starting").name == "repairfail") {
- game.playerHud.dialog.advance();
- this.progress.advanceProgress("starting");
- }
- });
- return true;
- }
- startShipTravel() {
- if (!game.playerHud.main.isProgressFinished()) {
- return;
- }
- if (!this.inventory.consumeShipTravelMaterials()) {
- game.playerHud.dialog.rightSay('Insufficient Fuel', 'error-message');
- if (this.progress.getProgress("starting").name == "repair2") {
- game.playerHud.dialog.advance();
- this.progress.advanceProgress("starting");
- this.crafting.enableRecipe("fuel");
- setTimeout(() => {
- this.playerHud.dialog.leftSay("COMM: You can also make ship fuel out of Hydrogen using the replicator.");
- }, 2000);
- return false;
- }
- return false;
- }
- let duration = 5000;
- this.ship.move();
- this.background.move({ x: 0, y: -0.8 });
- setTimeout(() => {
- this.playerHud.dialog.leftSay("COMM: Excellent! Glad to see you up and running again.");
- setTimeout(() => {
- this.playerHud.dialog.leftSay("COMM: Dock at the station and I'll buy you a drink at the bar.");
- }, 2000);
- }, 2000);
- game.playerHud.navigation.startTravelAnimation(duration);
- game.playerHud.main.startProgress(duration, () => {
- this.playerHud.navigation.finishShipTravel();
- this.ship.stop();
- this.background.move({ x: 0, y: -0.01 });
- this.ship.setLocation("");
- this.sector.dock();
- this.playerHud.dialog.advance();
- setTimeout(() => {
- this.playerHud.dialog.leftSay("STATION: Contact, \"" + this.ship.name + "\" you are cleared to dock.");
- }, 2000);
- });
- return true;
- }
- dockingFinished() {
- this.ship.dock();
- this.playerHud.ship.enableDocking();
- setTimeout(() => {
- this.playerHud.dialog.leftSay("STATION: Welcome aboard, \"" + this.ship.name + "\".");
- }, 2000);
- }
- }
|