js13kgames2017-muri/src/muri.js

139 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

2017-09-13 22:59:35 +02:00
// 2017 Aaron Fischer <mail@aaron-fischer.net>
// a js13kgames entry
//
// Plot: You are in the dark, and you have no clue where you are
// and what happened. You wake up in completely dark and stumble
// around. You explore the area and you find out you are on a
// old space ship. You need to find out whats going on here and
// why you are here. After finding and combining some items, you
// find out that your stasis capsule had some malfunctioning and
// you are the only one on this mission. But what mission? And
// where are your crew members? Where are you? You are drifting
// along, lost in space with no hope of rescue. But you want to
// surrender.
// You find out that you are the only surrender on an ancient
// space ship, carring cargo from one place to another when
// space pirates killed the crew except you. You want to fly
// home, but you need to bring the ship back to life.
2017-08-18 16:02:53 +02:00
var muri = (function() {
kontra.init('js13k-2017');
kontra.assets.imagePath = 'assets/images';
var muri = {};
2017-09-12 23:52:00 +02:00
muri.ra = function(a) {
return a[Math.floor(Math.random()*a.length)];
};
2017-08-25 17:03:26 +02:00
muri.bg = function(room) {
2017-08-18 16:02:53 +02:00
return kontra.sprite({
x: 0, y: 0,
image: kontra.assets.images['room_'+room]
});
};
2017-08-24 17:06:46 +02:00
muri.currentRoom = 'stasis';
2017-08-18 16:02:53 +02:00
muri.modules = [];
2017-08-25 21:53:22 +02:00
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];
};
};
2017-08-25 21:53:22 +02:00
muri.get = fetchObject('modules');
muri.room = fetchObject('rooms');
2017-08-18 16:02:53 +02:00
2017-09-12 22:40:34 +02:00
muri.changeRoom = function(room) {
var fromRoom = muri.currentRoom;
muri.currentRoom = room;
if (muri.room(room).onEnter !== undefined)
muri.room(room).onEnter(fromRoom);
};
muri.door = function(room, position) {
var doorAnimationSheet = kontra.spriteSheet({
image: kontra.assets.images.door_sheet,
frameWidth: 13, frameHeight: 22,
animations: {
closed: {frames: 0},
opened: {frames: 5},
open: {frames: '0..5', frameRate: 6},
close: {frames: '5..0', frameRate: 6}
}
});
var doorSprite = kontra.sprite({
x: position[0], y: position[1],
animations: doorAnimationSheet.animations
});
return muri.get('entity')
.create(room+'.door', doorSprite)
.addCallback(function() {
doorSprite.playAnimation('open');
setTimeout(function() { muri.changeRoom('lift'); }, 1000);
});
};
muri.end = function(reason, again) {
2017-09-13 01:14:17 +02:00
muri.changeRoom('end');
muri.get('bubble')
.talk(reason, [20, 20])
.then(function() {
if (again)
document.getElementById('tryagain').style.display = 'block';
2017-09-13 01:14:17 +02:00
});
};
2017-08-22 15:03:49 +02:00
muri.setup = function() {
2017-08-17 19:05:06 +02:00
kontra.assets.load(
2017-08-31 15:06:10 +02:00
'room_stasis.png',
2017-09-12 17:06:21 +02:00
'room_engine.png',
'room_hydro.png',
2017-09-12 22:40:34 +02:00
'door_sheet.png',
'toggleSwitch_sheet.png',
'keycard.png',
2017-09-13 01:14:17 +02:00
'laser_sheet.png',
2017-09-12 00:33:44 +02:00
'stasis_lightSwitch.png',
'room_lift.png',
'lift_button.png'
2017-08-17 19:05:06 +02:00
).then(function() {
2017-08-18 16:02:53 +02:00
document.getElementById('loading').style.display = 'none';
2017-08-25 21:53:22 +02:00
muri.modules.forEach(function(m) {
if (m.init !== undefined) m.init();
});
muri.rooms.forEach(function(r) {
if (r.init !== undefined) r.init();
});
2017-08-18 16:02:53 +02:00
kontra.gameLoop({
update: function() {
var r = muri.room(muri.currentRoom);
if (r.update !== undefined) r.update();
muri.modules.forEach(function(m) {
if (m.update !== undefined) m.update();
});
2017-08-29 23:21:18 +02:00
if (!muri.get('mouse').isClickReleased()) {
muri.get('mouse').releaseClick();
muri.get('bubble').skip();
}
2017-08-18 16:02:53 +02:00
},
render: function() {
var r = muri.room(muri.currentRoom);
if (r.render !== undefined) r.render();
muri.modules.forEach(function(m) {
2017-08-28 21:29:27 +02:00
if (m.render !== undefined && m.name !== 'entity')
m.render();
});
2017-08-28 21:29:27 +02:00
muri.get('entity').render();
2017-08-18 16:02:53 +02:00
}
}).start();
});
2017-08-17 19:05:06 +02:00
};
2017-08-18 16:02:53 +02:00
return muri;
}());
2017-08-22 15:03:49 +02:00
window.onload = muri.setup;