-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (60 loc) · 2.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
const actions = require('./game/actions');
const gameReducer = require('./game/reducers');
const createStore = require('./game/store');
const render = require('./game/render');
const GameState = require('./game/state');
const moveAi = require('./ai/prediction');
const initialGameState = GameState([
{entity: 'hero', name: 'position', x: 4, y: 6},
{entity: 'hero', name: 'lastMove', move: 'up'},
{entity: 'hero', name: 'score', score: 0},
{entity: 'hero', name: 'player', playerName: 'pole'},
{entity: 'hero', name: 'target', target: 'reward'},
{entity: 'hero', name: 'active', active: false},
{entity: 'hero', name: 'render', avatar: 'hero'},
{entity: 'ghost', name: 'position', x: 6, y: 5},
{entity: 'ghost', name: 'lastMove', move: 'up'},
{entity: 'ghost', name: 'score', score: 0},
{entity: 'ghost', name: 'ai', aiName: 'terminator'},
{entity: 'ghost', name: 'target', target: 'hero'},
{entity: 'ghost', name: 'active', active: true},
{entity: 'ghost', name: 'render', avatar: 'antihero'},
{entity: 'reward', name: 'position', x: 6, y: 6},
{entity: 'reward', name: 'render', avatar: 'reward'}
]);
// TODO:
//
// - player turn order
// - graph:
// - collapsible graph http://bl.ocks.org/mbostock/4339083
// - incremental updates
// - don't move back, please
const store = createStore(gameReducer, initialGameState);
document.querySelector('body').innerHTML = [
'<div class="wrapper">',
'<div class="overlay"></div>',
'<div class="container"></div>',
'</div>'
].join('');
const container = document.querySelector('.container');
store.subscribe(render(container));
render(container)(initialGameState);
document.addEventListener('keydown', (e) => {
if (e.keyCode > 40 || e.keyCode < 37) return;
e.preventDefault();
const gameState = store.getState();
const currentPlayer = gameState.query(['active']).filter((entityName) => {
return !gameState.get(entityName, 'active');
}).first();
store.dispatch(actions.entityTurn(currentPlayer));
store.dispatch(actions.moveEntity(currentPlayer, e.keyCode));
store.dispatch(actions.updateWinners());
setTimeout(() => {
const move = moveAi(store.getState()).get('ghost', 'move');
console.log('move: ', move);
store.dispatch(actions.entityTurn('ghost'));
store.dispatch(actions.moveEntity('ghost', move));
store.dispatch(actions.updateWinners());
}, 10);
});