From 27e95d401170442d9bb4e8900942045c3855f2f8 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Wed, 18 May 2016 14:35:29 -0600 Subject: [PATCH] Fixed loading issues by forcing rm_Ocean to wait until all prefabs are loaded before running. Also added debug messages for all scripts loading. --- gui/inventoryGUI.js | 4 +++- gui/mapGUI.js | 4 +++- gui/titleScreen.js | 4 +++- gui/tradeGUI.js | 4 +++- loadAudio.js | 4 +++- loadControls.js | 4 +++- loadGUIs.js | 2 ++ loadGameManager.js | 2 ++ loadPrefabs.js | 33 ++++++++++++++++++++++++++++++--- loadRooms.js | 25 ++++++++++++++----------- prefabs/islandPrefab.js | 5 ++++- prefabs/oceanTilePrefab.js | 5 ++++- prefabs/shipPrefab.js | 21 ++++++++++++--------- prefabs/wavePrefab.js | 5 ++++- rooms/oceanRoom.js | 15 ++++++--------- start.js | 4 +++- 16 files changed, 99 insertions(+), 42 deletions(-) diff --git a/gui/inventoryGUI.js b/gui/inventoryGUI.js index 23c17e4..92a0014 100644 --- a/gui/inventoryGUI.js +++ b/gui/inventoryGUI.js @@ -1,4 +1,6 @@ -function inventoryGUI() {} +function inventoryGUI() { + if (Oversimplified.DEBUG.showMessages) console.log("Ran inventoryGUI()"); +} guiControl.inventory = { screen: "main", diff --git a/gui/mapGUI.js b/gui/mapGUI.js index 3dc0878..a5370e4 100644 --- a/gui/mapGUI.js +++ b/gui/mapGUI.js @@ -1,4 +1,6 @@ -function mapGUI() {} +function mapGUI() { + if (Oversimplified.DEBUG.showMessages) console.log("Ran mapGUI()"); +} guiControl.map = { show: false, diff --git a/gui/titleScreen.js b/gui/titleScreen.js index dbe78e7..19589c8 100644 --- a/gui/titleScreen.js +++ b/gui/titleScreen.js @@ -1,4 +1,6 @@ -function titleScreen () {} +function titleScreen () { + if (Oversimplified.DEBUG.showMessages) console.log("Ran titleScreen()"); +} guiControl.title = { screen: "main", diff --git a/gui/tradeGUI.js b/gui/tradeGUI.js index 87f9841..8586b51 100644 --- a/gui/tradeGUI.js +++ b/gui/tradeGUI.js @@ -1,4 +1,6 @@ -function tradeGUI() {} +function tradeGUI() { + if (Oversimplified.DEBUG.showMessages) console.log("Ran tradeGUI()"); +} guiControl.trade = { screen: "main", // "main", "buy", "sell", "gossip" diff --git a/loadAudio.js b/loadAudio.js index 50aa589..3f9ec77 100644 --- a/loadAudio.js +++ b/loadAudio.js @@ -14,4 +14,6 @@ var snd_select = new OS.E.AddSound("Select", {wav: "audio/sounds/Select.wav", mp var snd_sell = new OS.E.AddSound("Sell", {wav: "audio/sounds/Sell.wav", mp3: "audio/sounds/Sell.mp3"}); var snd_wave = new OS.E.AddSound("Wave Crash", {wav: "audio/sounds/Wave_Crash.wav", mp3: "audio/sounds/Wave_Crash.mp3"}); -function loadAudio() {} +function loadAudio() { + if (Oversimplified.DEBUG.showMessages) console.log("Ran loadAudio()"); +} diff --git a/loadControls.js b/loadControls.js index 0aa90f3..1fa809c 100644 --- a/loadControls.js +++ b/loadControls.js @@ -14,7 +14,9 @@ var ct_x = OS.C.Add("X", OS.Keycode.x); var ct_m = OS.C.Add("M", OS.Keycode.m); var ct_esc = OS.C.Add("Cancel", OS.Keycode.escape); -function loadControls () {} +function loadControls () { + if (Oversimplified.DEBUG.showMessages) console.log("Ran loadControls()"); +} function ct_up () { return { diff --git a/loadGUIs.js b/loadGUIs.js index 5da5113..8312339 100644 --- a/loadGUIs.js +++ b/loadGUIs.js @@ -3,6 +3,8 @@ function loadGUIs() { OS.AddScript("gui/inventoryGUI.js"); OS.AddScript("gui/mapGUI.js"); OS.AddScript("gui/tradeGUI.js"); + + if (Oversimplified.DEBUG.showMessages) console.log("Ran loadGUIs()"); } var guiControl = { diff --git a/loadGameManager.js b/loadGameManager.js index bcc25d4..97a0490 100644 --- a/loadGameManager.js +++ b/loadGameManager.js @@ -217,4 +217,6 @@ function loadGameManager () { for (var i = 0; i < G.economy.cargoItemWorth.length; i++) { G.economy.cargoItemWorth[i] += Math.round(Math.randomRange(-5, 5)); } + + if (Oversimplified.DEBUG.showMessages) console.log("Ran loadGameManager()"); } diff --git a/loadPrefabs.js b/loadPrefabs.js index f65a0c7..eaf5c83 100644 --- a/loadPrefabs.js +++ b/loadPrefabs.js @@ -1,6 +1,33 @@ +var prefabsLoaded = 0; +var prefabsToLoad = 4; function loadPrefabs() { - OS.AddScript("prefabs/shipPrefab.js"); - OS.AddScript("prefabs/islandPrefab.js"); - OS.AddScript("prefabs/oceanTilePrefab.js"); + OS.AddScript("prefabs/shipPrefab.js"); + OS.AddScript("prefabs/islandPrefab.js"); + OS.AddScript("prefabs/oceanTilePrefab.js"); OS.AddScript("prefabs/wavePrefab.js"); + + // Delay switching to Ocean room until everything is loaded. + WaitForPrefabsToLoad(); + + if (Oversimplified.DEBUG.showMessages) console.log("Ran loadPrefabs()"); +} + +// Callback function that prevents Ocean room from loading before everything it references is loaded. +WaitForPrefabsToLoad = function () { + console.log("waiting for " + (prefabsToLoad - prefabsLoaded).toString() + " prefabs to load"); + if (prefabsLoaded < prefabsToLoad || !window["oceanRoom"]) + { + setTimeout(function(){WaitForPrefabsToLoad()}, 0.1); + } else { + // Create player and ocean objects in rm_Ocean. + G.player = rm_Ocean.AddObject(OS.P["Ship"]); + G.oceanParticle = rm_Ocean.AddObject(OS.P["Ocean Particle"]); + + if (G.player.xBound) { // Force rm_Ocean to wait until G.player is completely loaded! + // Load the rooms only after the prefabs are loaded. + rm_Ocean.SetAsCurrentRoom(); + } else { + setTimeout(function(){WaitForPrefabsToLoad()}, 0.1); + } + } } diff --git a/loadRooms.js b/loadRooms.js index bf987c4..d2958b7 100644 --- a/loadRooms.js +++ b/loadRooms.js @@ -1,17 +1,20 @@ -var rm_Ocean = OS.R.Add("Default", { - // Putting my room "constants" here. - width: pixel(64) * 50, //50x45 map of 64x64 squares. This will allow a single pixel on the map to represent a 64x square and fit comfortably on screen. - height: pixel(64) * 44, - backgroundColor: "#1b2632", - squaresX: 50, - squaresY: 44, - squareSize: pixel(64), - numberOfIslands: 10, - clockTimerCutoff: ((1 / OS.S.defaultStep) * 60) * 5 // 5 minute day. +var rm_Load = OS.R.Add("Default"); +var rm_Ocean = OS.R.Add("Ocean Room", { + // Putting my room "constants" here. + width: pixel(64) * 50, //50x45 map of 64x64 squares. This will allow a single pixel on the map to represent a 64x square and fit comfortably on screen. + height: pixel(64) * 44, + backgroundColor: "#1b2632", + squaresX: 50, + squaresY: 44, + squareSize: pixel(64), + numberOfIslands: 10, + clockTimerCutoff: ((1 / OS.S.defaultStep) * 60) * 5 // 5 minute day. }); function loadRooms() { OS.AddScript("rooms/oceanRoom.js"); - OS.SetRoom(rm_Ocean); + rm_Load.SetAsCurrentRoom(); + + if (Oversimplified.DEBUG.showMessages) console.log("Ran loadRooms()"); } diff --git a/prefabs/islandPrefab.js b/prefabs/islandPrefab.js index 038a8b2..6e20ab1 100644 --- a/prefabs/islandPrefab.js +++ b/prefabs/islandPrefab.js @@ -1,6 +1,9 @@ var ani_island_1 = OS.A.Add("Island 1", 256, 256, {}); -function islandPrefab() {} +function islandPrefab() { + if (Oversimplified.DEBUG.showMessages) console.log("Ran islandPrefab()"); + prefabsLoaded++; +} var pr_island = OS.P.Add("Island", { solid: true, diff --git a/prefabs/oceanTilePrefab.js b/prefabs/oceanTilePrefab.js index b7c1ee9..31bb292 100644 --- a/prefabs/oceanTilePrefab.js +++ b/prefabs/oceanTilePrefab.js @@ -1,6 +1,9 @@ var ani_ocean = OS.A.Add("Ocean", 256, 256, {columns: 10, speed: 1/60}); -function oceanTilePrefab() {} +function oceanTilePrefab() { + if (Oversimplified.DEBUG.showMessages) console.log("Ran oceanTilePrefab()"); + prefabsLoaded++; +} var pr_ocean = OS.P.Add("Ocean Particle", { imageSrc: "images/ocean_sheet.png", diff --git a/prefabs/shipPrefab.js b/prefabs/shipPrefab.js index 3828a46..13e043f 100644 --- a/prefabs/shipPrefab.js +++ b/prefabs/shipPrefab.js @@ -7,7 +7,10 @@ var ani_ship_dl = OS.A.Add("Ship Down-Left", 64, 64, {columns: 2, speed: 1/60, y var ani_ship_d = OS.A.Add("Ship Down", 64, 64, {columns: 2, speed: 1/60, yOffset: 64 * 0}); var ani_ship_dr = OS.A.Add("Ship Down-Right", 64, 64, {columns: 2, speed: 1/60, yOffset: 64 * 2}); -function shipPrefab() {} +function shipPrefab() { + if (Oversimplified.DEBUG.showMessages) console.log("Ran shipPrefab()"); + prefabsLoaded++; +} var pr_ship = OS.P.Add("Ship", { solid: true, @@ -118,49 +121,49 @@ pr_ship.CheckMovement = function () { var movedSuccessfully = false; switch (this.direction) { case 0: - if (this.image.currentAnimation != "Ship Right") this.SetAnimation("Ship Right"); + if (this.sprite.currentAnimation != "Ship Right") this.SetAnimation("Ship Right"); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(moveAmount, 0, true, pixel(4)); this.pointInFront.x = this.x + this.xBound + pixel(2) + moveAmount; this.pointInFront.y = this.y; break; case 45: - if (this.image.currentAnimation != "Ship Up-Right") this.SetAnimation("Ship Up-Right"); + if (this.sprite.currentAnimation != "Ship Up-Right") this.SetAnimation("Ship Up-Right"); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(moveAmount, -moveAmount, true, pixel(4)); this.pointInFront.x = this.x + this.xBound + pixel(2) + moveAmount; this.pointInFront.y = this.y - this.yBound - pixel(2) - moveAmount; break; case 90: - if (this.image.currentAnimation != "Ship Up") this.SetAnimation("Ship Up"); + if (this.sprite.currentAnimation != "Ship Up") this.SetAnimation("Ship Up"); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(0, -moveAmount, true, pixel(4)); this.pointInFront.x = this.x; this.pointInFront.y = this.y - this.yBound - pixel(2) - moveAmount; break; case 135: - if (this.image.currentAnimation != "Ship Up-Left") this.SetAnimation("Ship Up-Left"); + if (this.sprite.currentAnimation != "Ship Up-Left") this.SetAnimation("Ship Up-Left"); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(-moveAmount, -moveAmount, true, pixel(4)); this.pointInFront.x = this.x - this.xBound - pixel(2) - moveAmount; this.pointInFront.y = this.y - this.yBound - pixel(2) - moveAmount; break; case 180: - if (this.image.currentAnimation != "Ship Left") this.SetAnimation("Ship Left"); + if (this.sprite.currentAnimation != "Ship Left") this.SetAnimation("Ship Left"); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(-moveAmount, 0, true, pixel(4)); this.pointInFront.x = this.x - this.xBound - pixel(2) - moveAmount; this.pointInFront.y = this.y; break; case 225: - if (this.image.currentAnimation != "Ship Down-Left") this.SetAnimation("Ship Down-Left"); + if (this.sprite.currentAnimation != "Ship Down-Left") this.SetAnimation("Ship Down-Left"); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(-moveAmount, moveAmount, true, pixel(4)); this.pointInFront.x = this.x - this.xBound - pixel(2) - moveAmount; this.pointInFront.y = this.y + this.yBound + pixel(2) + moveAmount; break; case 270: - if (this.image.currentAnimation != "Ship Down") this.SetAnimation("Ship Down"); + if (this.sprite.currentAnimation != "Ship Down") this.SetAnimation("Ship Down"); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(0, moveAmount, true, pixel(4)); this.pointInFront.x = this.x; this.pointInFront.y = this.y + this.yBound + pixel(2) + moveAmount; break; case 315: - if (this.image.currentAnimation != "Ship Down-Right") this.SetAnimation("Ship Down-Right"); + if (this.sprite.currentAnimation != "Ship Down-Right") this.SetAnimation("Ship Down-Right"); if (this.doTakeStep) movedSuccessfully = this.SimpleMove(moveAmount, moveAmount, true, pixel(4)); this.pointInFront.x = this.x + this.xBound + pixel(2) + moveAmount; this.pointInFront.y = this.y + this.yBound + pixel(2) + moveAmount; diff --git a/prefabs/wavePrefab.js b/prefabs/wavePrefab.js index 712dae6..7ef69d7 100644 --- a/prefabs/wavePrefab.js +++ b/prefabs/wavePrefab.js @@ -1,6 +1,9 @@ var ani_wave = OS.A.Add("Wave", 64, 64, {columns: 10, speed: 1/10}); -function wavePrefab() {} +function wavePrefab() { + if (Oversimplified.DEBUG.showMessages) console.log("Ran wavePrefab()"); + prefabsLoaded++; +} var pr_wave = OS.P.Add("Wave Particle", { imageSrc: "images/wave_sheet.png", diff --git a/rooms/oceanRoom.js b/rooms/oceanRoom.js index c0741cb..f9cdd0b 100644 --- a/rooms/oceanRoom.js +++ b/rooms/oceanRoom.js @@ -1,13 +1,5 @@ function oceanRoom () { - // Create objects on room creation for persistence. - G.player = rm_Ocean.AddObject(OS.P["Ship"]); - G.oceanParticle = rm_Ocean.AddObject(OS.P["Ocean Particle"]); - - rm_Ocean.GenerateMap(); - - // When room is loaded, explicitly set room to rm_Ocean, just in case "Default" doesn't work/is loaded too slowly - // to make sure DoFirst runs. - OS.SetRoom(rm_Ocean); + if (Oversimplified.DEBUG.showMessages) console.log("Ran oceanRoom()"); } rm_Ocean.waveTimer = Math.round(Math.randomRange(30, 150)); @@ -16,6 +8,11 @@ rm_Ocean.clockTimerCount = 1; // Set it to 1 so it doesn't check for player il rm_Ocean.DoFirst = function () { mus_title.Play(); + // G.player and G.oceanParticle are created in loadPrefabs.js + + rm_Ocean.GenerateMap(); + + console.log("player xBound: " + G.player.xBound); G.player.x = (this.squareSize * (this.squaresX / 2)) - (this.squareSize / 2) - G.player.xBound; G.player.y = (this.squareSize * (this.squaresY / 2)); // console.log(G.player.name + " created at " + G.player.x + ", " + G.player.y); diff --git a/start.js b/start.js index 228b392..c7de84e 100644 --- a/start.js +++ b/start.js @@ -13,9 +13,11 @@ function start() OS.AddScript("loadControls.js"); OS.AddScript("loadAudio.js"); OS.AddScript("loadGameManager.js"); - OS.AddScript("loadPrefabs.js"); OS.AddScript("loadGUIs.js"); + OS.AddScript("loadPrefabs.js"); OS.AddScript("loadRooms.js"); + + if (Oversimplified.DEBUG.showMessages) console.log("Ran start()"); } function pixel(number) {