From 3ee8048bc95eb8ee808e9a0b65efd1b826eeac40 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Fri, 25 Aug 2017 21:53:22 +0200 Subject: [PATCH] Fix up the room refactoring --- src/muri.js | 33 ++++++++++++++++++--------------- src/stasis.js | 3 ++- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/muri.js b/src/muri.js index 1cb0605..ef09e80 100644 --- a/src/muri.js +++ b/src/muri.js @@ -53,12 +53,16 @@ var muri = (function() { }; muri.currentRoom = 'stasis'; muri.modules = []; - muri.rooms = {}; - muri.get = function(moduleName) { - for (var i in muri.modules) - if (muri.modules[i].name === moduleName) - return muri.modules[i]; + muri.rooms = []; + var fetchObject = function(type) { + return function(name) { + for (var i in muri[type]) + if (muri[type][i].name === name) + return muri[type][i]; + }; }; + muri.get = fetchObject('modules'); + muri.room = fetchObject('rooms'); muri.setup = function() { kontra.assets.load( @@ -67,28 +71,27 @@ var muri = (function() { 'stasis_door-sheet.png' ).then(function() { document.getElementById('loading').style.display = 'none'; + muri.modules.forEach(function(m) { + if (m.init !== undefined) m.init(); + }); + muri.rooms.forEach(function(r) { + if (r.init !== undefined) r.init(); + }); + kontra.gameLoop({ update: function() { - muri.rooms[muri.currentRoom].update(); + muri.room(muri.currentRoom).update(); muri.modules.forEach(function(m) { if (m.update !== undefined) m.update(); }); }, render: function() { - muri.rooms[muri.currentRoom].render(); + muri.room(muri.currentRoom).render(); muri.modules.forEach(function(m) { if (m.render !== undefined) m.render(); }); } }).start(); - - muri.modules.forEach(function(m) { - if (m.init !== undefined) m.init(); - }); - muri.rooms.forEach(function(r) { - .... - if (r.init !== undefined) r.init(); - }); }); }; diff --git a/src/stasis.js b/src/stasis.js index 4e3fe92..18ad462 100644 --- a/src/stasis.js +++ b/src/stasis.js @@ -94,5 +94,6 @@ } }; - muri.rooms['stasis'] = stasis; + stasis.name = 'stasis'; + muri.rooms.push(stasis); }());