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="muri.js"></script>
<script src="entity.js"></script>
<script src="bubble.js"></script>
<script src="mouse.js"></script>
<script src="stasis.js"></script>

View file

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

View file

@ -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';