From 290879dafec8059fcf99a53e4fe8892f57a470fa Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Mon, 28 Aug 2017 21:29:27 +0200 Subject: [PATCH] add entities --- ...is_door-sheet.png => stasis_doorSheet.png} | Bin src/assets/images/stasis_lightSwitch.gif | Bin 0 -> 87 bytes src/entity.js | 50 ++++++++++ src/index_dev.html | 1 + src/muri.js | 7 +- src/stasis.js | 87 ++++++++---------- 6 files changed, 93 insertions(+), 52 deletions(-) rename src/assets/images/{stasis_door-sheet.png => stasis_doorSheet.png} (100%) create mode 100644 src/assets/images/stasis_lightSwitch.gif create mode 100644 src/entity.js diff --git a/src/assets/images/stasis_door-sheet.png b/src/assets/images/stasis_doorSheet.png similarity index 100% rename from src/assets/images/stasis_door-sheet.png rename to src/assets/images/stasis_doorSheet.png diff --git a/src/assets/images/stasis_lightSwitch.gif b/src/assets/images/stasis_lightSwitch.gif new file mode 100644 index 0000000000000000000000000000000000000000..16c551eb1bd3e24a15074f82cbae32db2ad22f4c GIT binary patch literal 87 zcmZ?wbh9u|WMyDsC}&_${Lk&@8WQa67~pE8XTZ$J02KPk!pQ}sb%2-wq^t?VU|^7z e)8z273CUtuWaI}CVdj)jlyY{S>gLSEU=09w;teqX literal 0 HcmV?d00001 diff --git a/src/entity.js b/src/entity.js new file mode 100644 index 0000000..f3f7cb5 --- /dev/null +++ b/src/entity.js @@ -0,0 +1,50 @@ +(function() { + var entity = {}; + var allEntities = []; + + entity.get = function(name) { + for (var i in allEntities) + if (allEntities[i].name === name) + return allEntities[i]; + return false; + }; + + entity.create = function(name, sprite) { + allEntities.push({ + name: name, + sprite: sprite, + callbacks: [] + }); + return { + addCallback: function(callback) { + entity.get(name).callbacks.push(callback); + return this; + } + }; + }; + + entity.update = function() { + var clickedOnASprite = false; + allEntities.forEach(function(e) { + e.sprite.update(); + if (muri.get('mouse').clickedOn(e.sprite)) { + clickedOnASprite = true; + e.callbacks.forEach(function(c) { + c(); + }); + } + }); + + if (clickedOnASprite) + muri.get('mouse').releaseClick(); + }; + + entity.render = function() { + allEntities.forEach(function(e) { + e.sprite.render(); + }); + }; + + entity.name = 'entity'; + muri.modules.push(entity); +}()); diff --git a/src/index_dev.html b/src/index_dev.html index a7e18bb..2fa1ead 100644 --- a/src/index_dev.html +++ b/src/index_dev.html @@ -13,6 +13,7 @@ + diff --git a/src/muri.js b/src/muri.js index ef09e80..066fbd9 100644 --- a/src/muri.js +++ b/src/muri.js @@ -68,7 +68,8 @@ var muri = (function() { kontra.assets.load( 'room_stasis_dark.gif', 'room_stasis.gif', - 'stasis_door-sheet.png' + 'stasis_doorSheet.png', + 'stasis_lightSwitch.gif' ).then(function() { document.getElementById('loading').style.display = 'none'; muri.modules.forEach(function(m) { @@ -88,8 +89,10 @@ var muri = (function() { render: function() { muri.room(muri.currentRoom).render(); muri.modules.forEach(function(m) { - if (m.render !== undefined) m.render(); + if (m.render !== undefined && m.name !== 'entity') + m.render(); }); + muri.get('entity').render(); } }).start(); }); diff --git a/src/stasis.js b/src/stasis.js index 18ad462..5e88b1e 100644 --- a/src/stasis.js +++ b/src/stasis.js @@ -1,7 +1,6 @@ (function() { var stasis = {}; - var controlPanelSprite = kontra.sprite({x: 16, y: 13, width: 3, height: 2}); var background, backgroundDark = null; var doorAnimationSheet = null; var doorSprite = null; @@ -12,24 +11,13 @@ stasis.init = function() { doorAnimationSheet = kontra.spriteSheet({ - image: kontra.assets.images['stasis_door-sheet'], - frameWidth: 24, - frameHeight: 21, + image: kontra.assets.images.stasis_doorSheet, + frameWidth: 24, frameHeight: 21, animations: { - closed: { - frames: 0 - }, - opened: { - frames: 2 - }, - open: { - frames: '0..3', - frameRate: 6, - }, - close: { - frames: '3..0', - frameRate: 6, - } + closed: {frames: 0}, + opened: {frames: 2}, + open: {frames: '0..3', frameRate: 6}, + close: {frames: '3..0', frameRate: 6} } }); doorSprite = kontra.sprite({ @@ -40,6 +28,24 @@ background = muri.bg('stasis'); backgroundDark = muri.bg('stasis_dark'); + muri.get('entity') + .create('stasis.lightSwitch', + kontra.sprite({x: 16, y: 13, width: 3, height: 2, + image: kontra.assets.images.stasis_lightSwitch})) + .addCallback(function() { + if (!roomState.isLightOn) { + roomState.isLightOn = true; + muri.get('bubble') + .talk([ + 'Ah, much better.', + 'Looks like something happened to my stasis capsule.' + ]); + } else { + muri.get('bubble') + .talk(['No, I will not turn off the light again!']); + } + }); + if (!roomState.isLightOn) { muri.get('bubble') .story([ @@ -51,34 +57,19 @@ }; stasis.update = function() { - doorSprite.update(); - if (roomState.isLightOn) { - if (muri.get('mouse').clickedOn(doorSprite)) { - muri.get('mouse').releaseClick(); - if (!roomState.isDoorOpen) { - doorSprite.playAnimation('open'); - roomState.isDoorOpen = true; - } else { - doorSprite.playAnimation('close'); - roomState.isDoorOpen = false; - } - } - } - - if (muri.get('mouse').clickedOn(controlPanelSprite)) { - muri.get('mouse').releaseClick(); - - if (!roomState.isLightOn) { - roomState.isLightOn = true; - muri.get('bubble') - .talk([ - 'Ah, much better.', - 'Looks like something happened to my stasis capsule.' - ]); - } else { - muri.get('bubble') - .talk(['No, I will not turn off the light again!']); - } + if (roomState.isLightOn && + !muri.get('entity').get('stasis.door')) { + muri.get('entity') + .create('stasis.door', doorSprite) + .addCallback(function() { + if (!roomState.isDoorOpen) { + doorSprite.playAnimation('open'); + roomState.isDoorOpen = true; + } else { + doorSprite.playAnimation('close'); + roomState.isDoorOpen = false; + } + }); } }; @@ -88,10 +79,6 @@ } else { backgroundDark.render(); } - - if (roomState.isLightOn) { - doorSprite.render(); - } }; stasis.name = 'stasis';