- commit
- 9c85a5a50a6c826ee51d0fabb621345a7ad2505c
- parent
- 9aec41599002d25b8b981f18c132d6a36f4701fe
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2019-10-20 12:36
add scenaio UI
Diffstat
| M | src/index.js | 15 | +++++++++++++++ |
| M | src/template.js | 11 | +++++++++-- |
2 files changed, 24 insertions, 2 deletions
diff --git a/src/index.js b/src/index.js
@@ -1,4 +1,5 @@ 1 1 import template from './template.js'; -1 2 import scenarios from './scenarios.js'; 2 3 import * as logic from './logic.js'; 3 4 import * as constants from './constants.js'; 4 5 @@ -27,6 +28,10 @@ const on = function(eventType, selector, fn) { 27 28 }); 28 29 }; 29 30 -1 31 const clone = function(obj) { -1 32 return JSON.parse(JSON.stringify(obj)); -1 33 }; -1 34 30 35 const play = function() { 31 36 if (!state.game.playing && !state.game.steps) { 32 37 return; @@ -102,4 +107,14 @@ on('click', '.js-menu-sandbox', function(state) { 102 107 } 103 108 }); 104 109 -1 110 on('click', '.js-menu-scenario', function(state) { -1 111 const i = parseInt(this.dataset.scenario, 10); -1 112 state.game = { -1 113 board: clone(scenarios[i].board), -1 114 currentPlayer: 1, -1 115 playing: false, -1 116 steps: 0, -1 117 } -1 118 }); -1 119 105 120 init(document.body);
diff --git a/src/template.js b/src/template.js
@@ -1,3 +1,5 @@ -1 1 import scenarios from './scenarios.js'; -1 2 1 3 const h = petitDom.h; 2 4 3 5 const renderBoard = function(state) { @@ -28,6 +30,7 @@ const renderControls = function(state) { 28 30 ]); 29 31 } else { 30 32 return h('div', {'class': 'board-controls'}, [ -1 33 h('input', {type: 'range', value: 50, name: 'speed'}), 31 34 h('input', {type: 'hidden', value: 50, name: 'speed'}), 32 35 h('input', {type: 'hidden', value: 1, name: 'steps'}), 33 36 h('button', {'class': 'js-play'}, state.game.playing ? 'Pause' : 'Play'), @@ -38,8 +41,12 @@ const renderControls = function(state) { 38 41 39 42 const renderMenu = function(state) { 40 43 return h('div', {'class': 'menu'}, [41 -1 h('button', {'class': 'js-menu-sandbox'}, 'Start sandbox game'),42 -1 ]);-1 44 h('button', {'class': 'js-menu-sandbox'}, 'Start sandbox game') -1 45 ].concat(scenarios.map((scenario, i) => h( -1 46 'button', -1 47 {'class': 'js-menu-scenario', 'data-scenario': i}, -1 48 'Start scenario: ' + scenario.title, -1 49 )))); 43 50 }; 44 51 45 52 export default function(state) {