js13kgames2017-muri/src/bubble.js

70 lines
2 KiB
JavaScript
Raw Normal View History

(function() {
var bubble = {};
2017-08-28 23:51:21 +02:00
var activeTalk = false;
var skip = false;
var dom = document.getElementById('bubble');
2017-08-28 23:51:21 +02:00
var show = function(text, position) {
return new Promise(function(resolve, reject) {
2017-08-22 15:03:49 +02:00
dom.style.display = '';
dom.style.left = position[0]*8;
dom.style.top = position[1]*8;
dom.innerHTML = '';
2017-08-22 15:03:49 +02:00
parts = text.split(' ');
2017-08-28 23:51:21 +02:00
var showFragment = function() {
if (skip) {
dom.innerHTML = '';
dom.style.display = 'none';
skip = false;
throw new Error('dfad');
}
2017-08-22 15:03:49 +02:00
if (parts.length === 0) {
setTimeout(function() {
dom.style.display = 'none';
return resolve();
2017-08-28 23:51:21 +02:00
}, 1500);
2017-08-22 15:03:49 +02:00
return;
}
dom.innerHTML += parts.shift() + ' ';
2017-08-28 23:51:21 +02:00
setTimeout(showFragment, 150);
2017-08-22 15:03:49 +02:00
};
2017-08-28 23:51:21 +02:00
showFragment();
2017-08-22 15:03:49 +02:00
});
};
2017-08-28 23:51:21 +02:00
bubble.skip = function() {
dom.innerHTML = '';
skip = true;
};
bubble.stop = function() {
bubble.skip();
if (!activeTalk) return;
activeTalk.reject();
activeTalk = false;
};
2017-08-22 15:03:49 +02:00
bubble.talk = function(texts, position) {
2017-08-22 23:44:46 +02:00
if (texts.length === 0) return;
var text = texts.shift();
2017-08-28 23:51:21 +02:00
return show(text, position || [5, 44])
.then(function() {return bubble.talk(texts, position);})
.catch(function() { return; });
2017-08-22 15:03:49 +02:00
};
bubble.story = function(talkList) {
2017-08-22 23:44:46 +02:00
if (talkList.length === 0) return;
var params = talkList.shift();
2017-08-28 23:51:21 +02:00
var activeTalk = bubble.talk(params[0], params[1])
.then(function() {return bubble.story(talkList);})
.catch(function() { return; });
return activeTalk;
};
bubble.name = 'bubble';
muri.modules.push(bubble);
}());