-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigParser.js
38 lines (30 loc) · 944 Bytes
/
configParser.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
import get from 'lodash/get'
import mapValues from 'lodash/mapValues'
import some from 'lodash/some'
import upperFirst from 'lodash/upperFirst'
import stampit from 'stampit'
import World from './world'
import things from './things'
function parseOrganism(config) {
const behaviors = get(config, 'actions') || ['go']
const mixins = behaviors
.concat(config.type)
.map(str => things[upperFirst(str)])
return stampit({
refs: config.properties,
methods: {
act(world, vector) {
return some(behaviors, action => this[action](world, vector))
},
},
}).compose(...mixins)
}
function parseConfig(config) {
const entities = mapValues(config.organisms, parseOrganism)
entities.wall = things.Wall
const legend = mapValues(config.world.legend, val => entities[val])
const world = World.fromLegend(legend, config.world.map)
world.randomize()
return world
}
export { parseConfig as default }