add entities

This commit is contained in:
Aaron Fischer 2017-08-28 21:29:27 +02:00
parent 3ee8048bc9
commit 290879dafe
6 changed files with 93 additions and 52 deletions

View file

Before

Width:  |  Height:  |  Size: 361 B

After

Width:  |  Height:  |  Size: 361 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 B

50
src/entity.js Normal file
View file

@ -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);
}());

View file

@ -13,6 +13,7 @@
<script src="vendor/kontra.js"></script> <script src="vendor/kontra.js"></script>
<script src="muri.js"></script> <script src="muri.js"></script>
<script src="entity.js"></script>
<script src="bubble.js"></script> <script src="bubble.js"></script>
<script src="mouse.js"></script> <script src="mouse.js"></script>
<script src="stasis.js"></script> <script src="stasis.js"></script>

View file

@ -68,7 +68,8 @@ var muri = (function() {
kontra.assets.load( kontra.assets.load(
'room_stasis_dark.gif', 'room_stasis_dark.gif',
'room_stasis.gif', 'room_stasis.gif',
'stasis_door-sheet.png' 'stasis_doorSheet.png',
'stasis_lightSwitch.gif'
).then(function() { ).then(function() {
document.getElementById('loading').style.display = 'none'; document.getElementById('loading').style.display = 'none';
muri.modules.forEach(function(m) { muri.modules.forEach(function(m) {
@ -88,8 +89,10 @@ var muri = (function() {
render: function() { render: function() {
muri.room(muri.currentRoom).render(); muri.room(muri.currentRoom).render();
muri.modules.forEach(function(m) { 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(); }).start();
}); });

View file

@ -1,7 +1,6 @@
(function() { (function() {
var stasis = {}; var stasis = {};
var controlPanelSprite = kontra.sprite({x: 16, y: 13, width: 3, height: 2});
var background, backgroundDark = null; var background, backgroundDark = null;
var doorAnimationSheet = null; var doorAnimationSheet = null;
var doorSprite = null; var doorSprite = null;
@ -12,24 +11,13 @@
stasis.init = function() { stasis.init = function() {
doorAnimationSheet = kontra.spriteSheet({ doorAnimationSheet = kontra.spriteSheet({
image: kontra.assets.images['stasis_door-sheet'], image: kontra.assets.images.stasis_doorSheet,
frameWidth: 24, frameWidth: 24, frameHeight: 21,
frameHeight: 21,
animations: { animations: {
closed: { closed: {frames: 0},
frames: 0 opened: {frames: 2},
}, open: {frames: '0..3', frameRate: 6},
opened: { close: {frames: '3..0', frameRate: 6}
frames: 2
},
open: {
frames: '0..3',
frameRate: 6,
},
close: {
frames: '3..0',
frameRate: 6,
}
} }
}); });
doorSprite = kontra.sprite({ doorSprite = kontra.sprite({
@ -40,6 +28,24 @@
background = muri.bg('stasis'); background = muri.bg('stasis');
backgroundDark = muri.bg('stasis_dark'); 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) { if (!roomState.isLightOn) {
muri.get('bubble') muri.get('bubble')
.story([ .story([
@ -51,34 +57,19 @@
}; };
stasis.update = function() { stasis.update = function() {
doorSprite.update(); if (roomState.isLightOn &&
if (roomState.isLightOn) { !muri.get('entity').get('stasis.door')) {
if (muri.get('mouse').clickedOn(doorSprite)) { muri.get('entity')
muri.get('mouse').releaseClick(); .create('stasis.door', doorSprite)
if (!roomState.isDoorOpen) { .addCallback(function() {
doorSprite.playAnimation('open'); if (!roomState.isDoorOpen) {
roomState.isDoorOpen = true; doorSprite.playAnimation('open');
} else { roomState.isDoorOpen = true;
doorSprite.playAnimation('close'); } else {
roomState.isDoorOpen = false; 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!']);
}
} }
}; };
@ -88,10 +79,6 @@
} else { } else {
backgroundDark.render(); backgroundDark.render();
} }
if (roomState.isLightOn) {
doorSprite.render();
}
}; };
stasis.name = 'stasis'; stasis.name = 'stasis';