diff --git a/src/async.js b/src/async.js new file mode 100644 index 0000000..61dc206 --- /dev/null +++ b/src/async.js @@ -0,0 +1,55 @@ +function Async() { + return { + bind: bind, + run: run, + sequence: sequence, + wait: wait, + wrap: wrap + }; + + function bind(m, f) { + return function(g) { + m(function(x) { + f(x)(g); + }); + } + } + + function run() { + var m; + if(arguments.length == 1) { + m = arguments[0]; + } else { + m = sequence.apply(null, arguments); + } + m(function() {}); + } + + function sequence() { + var steps = arguments; + var i = 0; + return function(f) { + var step = function() { + if(i < steps.length) { + steps[i++](step); + } else { + f(); + } + } + step(); + }; + } + + function wait(delay) { + return function(f) { + setTimeout(f, delay); + }; + } + + function wrap(x) { + return function(f) { + f(x); + }; + } + +} diff --git a/src/automaton.js b/src/automaton.js index 62d0426..eb63869 100644 --- a/src/automaton.js +++ b/src/automaton.js @@ -1,12 +1,125 @@ -function Automaton(messaging, screen, session) { - var startMenu = ['New Game', 'Settings']; - if(session.hasSavedGame()) { - startMenu.unshift('Continue'); +function Automaton(async, buttons, dom, messaging, screen, session) { + var menus = { + start: { + entries: [ + {label: 'New Game', action: newGame}, + {label: 'Settings', action: function() { console.log("Not implemented yet"); }} + ], + cancel: intro + }, + name: { + }, + skin: { + } + }; + var game = session.getGame(); + if(game != undefined) { + menus.start.entries.unshift( + {label: 'Continue', action: function() { messaging.send({tag: 'Resume', game: game}); }} + ); } - screen.menu(startMenu); - messaging.addEventListener(['Init'], function(game) { - console.log(game); - }); - //messaging.send({tag: 'NewGame'}); + return { + run: run + } + + //messaging.addEventListener(['Init'], function(game) { + // console.log(game); + //}); + + function animation(frames) { + var frameCounter = 0; + var scheduled; + var step = function() { + if(frameCounter < frames.length) { + scheduled = null; + frames[frameCounter].action(); + if(frames[frameCounter].delay != undefined) { + scheduled = setTimeout(step, frames[frameCounter].delay); + } + frameCounter++ + } + }; + return { + run: step, + skip: function() { if(scheduled != undefined) { clearTimeout(scheduled); } } + }; + } + + function text(message) { + var characters = message.split(''); + var frames = []; + var ready = false; + var append = function(c) { return function() { screen.appendText(c); }; }; + for(var i = 0; i < characters.length; i++) { + frames.push({action: append(characters[i]), delay: 50}); + } + frames.push({action: function() { ready = true; screen.markAsRead(); }, delay: null}); + var remote = animation(frames); + return function(f) { + buttons.push({ + 'A': function() { + if(ready) { + f(); + } else { + remote.skip(); + screen.text(message); + ready = true; + } + } + }); + screen.text(''); + remote.run(); + }; + } + + function menu(config) { + var cursor = 0; + var m = screen.menu(config.entries); + var sync = function() { screen.select(cursor); }; + var mapping = { + 'Up': function() { cursor = Math.max(0, cursor - 1); sync(); }, + 'Down': function() { cursor = Math.min(config.entries.length - 1, cursor + 1); sync(); }, + 'A': function() { config.entries[cursor].action(); } + }; + if(config.cancel != undefined) { + mapping['B'] = function() { buttons.pop(); screen.clear(m); config.cancel(); } + } + buttons.push(mapping); + } + + function run() { + intro(); + } + + function intro() { + var title = dom.make('p', {textContent: "P O K E M O N", class: 'title'}); + var subtitle = dom.make('p', {textContent: "< press start >", class: 'subtitle'}); + var remote = animation([ + {action: function() {}, delay: 500}, + {action: function() { screen.show(title); }, delay: 1000}, + {action: function() { screen.show(subtitle); }, delay: null} + ]); + buttons.map({ + 'B': function() { remote.skip(); intro() }, + 'Start': function() { + remote.skip(); + screen.frame(2); + screen.clear(); + menu(menus.start); + } + }); + screen.frame(1); + screen.clear(); + remote.run(); + } + + function newGame() { + screen.frame(3); + screen.clear(); + async.run( + text("Bonjour ! Bienvenue dans le monde merveilleux des pokémons !"), + text("Pour certains, les pokemons sont des amis. Pour d'autres, ils sont une ressource. "), + ); + } } diff --git a/src/buttons.js b/src/buttons.js new file mode 100644 index 0000000..1545c75 --- /dev/null +++ b/src/buttons.js @@ -0,0 +1,31 @@ +function Buttons(session) { + var mapping; + var mappingsStack = []; + var layout = session.getLayout(); + + document.addEventListener('keydown', function(event) { + var button = layout[event.key]; + if(button != undefined && mapping[button] != undefined) { + mapping[button](); + } + }); + + return { + map: map, + pop: pop, + push: push + }; + + function map(newMapping) { + mapping = newMapping; + } + + function pop() { + mappingsStack.pop(); + } + + function push(newMapping) { + mappingsStack.push(newMapping); + map(newMapping); + } +} diff --git a/src/index.html b/src/index.html index 6ac2f1f..9809b39 100644 --- a/src/index.html +++ b/src/index.html @@ -5,8 +5,10 @@