From b46fcf8d0f3b69e8ea6106a3467fa49a321025dc Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Wed, 6 Apr 2016 14:36:51 -0600 Subject: [PATCH] Restructured Oversimplified.Settings for better readability in code, added preventRightClick option defaulted to true, moved Oversimplified.SetCamera() out of the Oversimplified.Settings namespace because that just makes more sense, fixed SetCamera() references accordingly. --- Oversimplified.js | 64 ++++++++++++++++++++++++---------------------- rooms/oceanRoom.js | 26 +++++++++++-------- start.js | 7 ++++- 3 files changed, 55 insertions(+), 42 deletions(-) diff --git a/Oversimplified.js b/Oversimplified.js index 25a7bc2..949d81e 100644 --- a/Oversimplified.js +++ b/Oversimplified.js @@ -16,36 +16,11 @@ Oversimplified.emptyImage.width = 1; Oversimplified.emptyImage.height = 1; // Settings Namespace -Oversimplified.Settings = {}; -Oversimplified.Settings.defaultStep = 1/30; -Oversimplified.Settings.soundVolume = 0.75; -Oversimplified.Settings.musicVolume = 0.75; - -/* Set up the camera. - -It is important that this is done first at the time the game is loaded because this determines the size of the HTML5 canvas. -Be sure that the objectToFollow has already been created in the current room. Can be referenced with a variable. -objectToFollow, hBorder, and vBorder are optional arguments, but if you want to set hBorder and vBorder, there must be an objectToFollow. -*/ -Oversimplified.Settings.SetCamera = function (options) { - Oversimplified.camera.width = typeof options.width !== 'undefined' ? options.width : Oversimplified.camera.width; - Oversimplified.camera.height = typeof options.height !== 'undefined' ? options.height : Oversimplified.camera.height; - Oversimplified.SetCanvasToCameraSize(); - - Oversimplified.camera.x = typeof options.x !== 'undefined' ? options.x : Oversimplified.camera.x; - Oversimplified.camera.y = typeof options.y !== 'undefined' ? options.y : Oversimplified.camera.y; - - if (typeof options.objectToFollow !== 'undefined') { - if (options.objectToFollow.name) { - Oversimplified.camera.Follow(options.objectToFollow); - } else { - if (Oversimplified.DEBUG.showMessages) console.log("Oversimplified.Settings.SetCamera()'s objectToFollow argument must be a Oversimplified.GameObject."); - } - } - - Oversimplified.camera.hBorder = (typeof options.hBorder !== 'undefined') ? options.hBorder : Oversimplified.camera.hBorder; - Oversimplified.camera.vBorder = (typeof options.vBorder !== 'undefined') ? options.vBorder : Oversimplified.camera.vBorder; - +Oversimplified.Settings = { + defaultStep : 1/30, + soundVolume : 0.75, + musicVolume : 0.75, + preventRightClick : true } // Convenient alias for Settings. Oversimplified.S = Oversimplified.Settings; @@ -74,6 +49,33 @@ Oversimplified.camera = { } } +/* Set up the camera. + +It is important that this is done first at the time the game is loaded because this determines the size of the HTML5 canvas. +Be sure that the objectToFollow has already been created in the current room. Can be referenced with a variable. +objectToFollow, hBorder, and vBorder are optional arguments, but if you want to set hBorder and vBorder, there must be an objectToFollow. +*/ +Oversimplified.SetCamera = function (options) { + Oversimplified.camera.width = typeof options.width !== 'undefined' ? options.width : Oversimplified.camera.width; + Oversimplified.camera.height = typeof options.height !== 'undefined' ? options.height : Oversimplified.camera.height; + Oversimplified.SetCanvasToCameraSize(); + + Oversimplified.camera.x = typeof options.x !== 'undefined' ? options.x : Oversimplified.camera.x; + Oversimplified.camera.y = typeof options.y !== 'undefined' ? options.y : Oversimplified.camera.y; + + if (typeof options.objectToFollow !== 'undefined') { + if (options.objectToFollow.name) { + Oversimplified.camera.Follow(options.objectToFollow); + } else { + if (Oversimplified.DEBUG.showMessages) console.log("Oversimplified.Settings.SetCamera()'s objectToFollow argument must be a Oversimplified.GameObject."); + } + } + + Oversimplified.camera.hBorder = (typeof options.hBorder !== 'undefined') ? options.hBorder : Oversimplified.camera.hBorder; + Oversimplified.camera.vBorder = (typeof options.vBorder !== 'undefined') ? options.vBorder : Oversimplified.camera.vBorder; + +} + // Mouse Object Oversimplified.mouse = { x: 0, @@ -1346,7 +1348,7 @@ Oversimplified.SetupCanvas = function () { } //Disable right click menu on canvas - Oversimplified.canvas.oncontextmenu = function() {return false;}; + if (Oversimplified.Settings.preventRightClick) Oversimplified.canvas.oncontextmenu = function() {return false;}; } Oversimplified.SetupControls = function () { diff --git a/rooms/oceanRoom.js b/rooms/oceanRoom.js index a82020e..06fcd20 100644 --- a/rooms/oceanRoom.js +++ b/rooms/oceanRoom.js @@ -1,5 +1,15 @@ function oceanRoom () { + // Create objects on room creation for persistence. + G.player = rm_Ocean.AddObject(OS.P["Ship"]); + G.player.x = ((rm_Ocean.width / OS.S.pixelScale) / 2) * OS.S.pixelScale; + G.player.y = ((rm_Ocean.height / OS.S.pixelScale) / 2) * OS.S.pixelScale; + console.log(G.player.name + " created at " + G.player.x + ", " + G.player.y); + G.oceanParticle = rm_Ocean.AddObject(OS.P["Ocean Particle"]); + G.oceanParticle.x = G.player.x + randomSmidge(); + G.oceanParticle.y = G.player.y + randomSmidge(); + // 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); } @@ -11,16 +21,12 @@ rm_Ocean.DoFirst = function () { //Hide cursor when playing (only use if masking the cursor with another object) //OS.canvas.style.cursor = "none"; - // Create objects on room start. This is best practice unless you need persistent objects. - G.player = this.AddObject(OS.P["Ship"]); - G.player.x = ((rm_Ocean.width / OS.S.pixelScale) / 2) * OS.S.pixelScale; - G.player.y = ((rm_Ocean.height / OS.S.pixelScale) / 2) * OS.S.pixelScale; - - G.oceanParticle = this.AddObject(OS.P["Ocean Particle"]); - G.oceanParticle.x = G.player.x + randomSmidge(); - G.oceanParticle.y = G.player.y + randomSmidge(); - - OS.S.SetCamera(64 * OS.S.pixelScale, 64 * OS.S.pixelScale, G.player, 24 * OS.S.pixelScale, 24 * OS.S.pixelScale); + // Reset camera whenever room starts + OS.SetCamera({ + x: G.player.x - (OS.camera.width / 2), + y: G.player.y - (OS.camera.height / 2), + objectToFollow: G.player + }); } rm_Ocean.Do = function () { // Move G.oceanParticle around based on player's movement. diff --git a/start.js b/start.js index bc9dc9d..aa87bd9 100644 --- a/start.js +++ b/start.js @@ -1,6 +1,11 @@ OS.S.defaultStep = 1 / 120; OS.S.pixelScale = 4; -OS.S.SetCamera(64 * OS.S.pixelScale, 64 * OS.S.pixelScale); +OS.SetCamera({ + width: 64 * OS.S.pixelScale, + height: 64 * OS.S.pixelScale, + hBorder: 24 * OS.S.pixelScale, + vBorder: 24 * OS.S.pixelScale +}); function start() {