Handle text speeds and store it along with keyboard layout

This commit is contained in:
Tissevert 2018-11-23 17:03:09 +01:00
parent 54ff5b0875
commit 78aa3f6281
4 changed files with 22 additions and 17 deletions

View file

@ -1,10 +1,9 @@
function Buttons(session) { function Buttons(session) {
var mapping; var mapping;
var mappingsStack = []; var mappingsStack = [];
var layout = session.getLayout();
document.addEventListener('keydown', function(event) { document.addEventListener('keydown', function(event) {
var button = layout[event.key]; var button = session.getOptions().layout[event.key];
if(button != undefined && mapping[button] != undefined) { if(button != undefined && mapping[button] != undefined) {
mapping[button](); mapping[button]();
} }

View file

@ -5,7 +5,7 @@ window.addEventListener('load', function() {
screen = Screen(dom); screen = Screen(dom);
session = Session(); session = Session();
buttons = Buttons(session); buttons = Buttons(session);
ui = UI(async, buttons, screen); ui = UI(async, buttons, screen, session);
automaton = Automaton(async, dom, messaging, screen, session, ui); automaton = Automaton(async, dom, messaging, screen, session, ui);
messaging.start(); messaging.start();

View file

@ -1,9 +1,9 @@
function Session() { function Session() {
var game = JSON.parse(localStorage.getItem('game')); var game = JSON.parse(localStorage.getItem('game'));
var layout = loadLayout(); var options = JSON.parse(localStorage.getItem('options')) || defaultOptions();
return { return {
getLayout: getLayout, getOptions: getOptions,
getGame: getGame, getGame: getGame,
save : save save : save
}; };
@ -20,10 +20,9 @@ function Session() {
localStorage.setItem('game', JSON.stringify(game)); localStorage.setItem('game', JSON.stringify(game));
} }
function loadLayout() { function defaultOptions() {
var saved = JSON.parse(localStorage.getItem('layout')); var o = {
if(saved == undefined) { layout: {
saved = {
'a': 'A', 'a': 'A',
'b': 'B', 'b': 'B',
'ArrowLeft': 'Left', 'ArrowLeft': 'Left',
@ -32,13 +31,14 @@ function Session() {
'ArrowDown': 'Down', 'ArrowDown': 'Down',
'Enter': 'Start', 'Enter': 'Start',
' ': 'Select' ' ': 'Select'
}; },
localStorage.setItem('layout', JSON.stringify(saved)); textSpeed: 'medium'
} };
return saved; localStorage.setItem('options', JSON.stringify(o));
return o;
} }
function getLayout() { function getOptions() {
return layout; return options;
} }
} }

View file

@ -1,5 +1,11 @@
function UI(async, buttons, screen) { function UI(async, buttons, screen, session) {
var lineWidth = 36; var lineWidth = 36;
var textSpeeds = {
slow: 100,
medium: 50,
fast: 20
};
var textDelay = textSpeeds[session.getOptions().textSpeed];
return { return {
animation: animation, animation: animation,
@ -74,7 +80,7 @@ function UI(async, buttons, screen) {
var ready = false; var ready = false;
var append = function(c) { return function() { screen.appendText(c); }; }; var append = function(c) { return function() { screen.appendText(c); }; };
for(var i = 0; i < characters.length; i++) { for(var i = 0; i < characters.length; i++) {
frames.push({action: append(characters[i]), delay: 50}); frames.push({action: append(characters[i]), delay: textDelay});
} }
frames.push({action: function() { ready = true; screen.markAsRead(); }, delay: null}); frames.push({action: function() { ready = true; screen.markAsRead(); }, delay: null});
var remote = animation(frames); var remote = animation(frames);