-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrunModel.js
73 lines (63 loc) · 2.32 KB
/
runModel.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
72
73
import util from 'https://backspaces.github.io/agentscript/src/util.js'
// You only need one of these but this is simpler than dynamic import()
import TwoDraw from 'https://backspaces.github.io/agentscript/tests/TwoDraw.js'
import TwoView from 'https://backspaces.github.io/agentscript/src/TwoView.js'
// Only needed by TwoView, built into TwoDraw
import ColorMap from 'https://backspaces.github.io/agentscript/src/ColorMap.js'
// These are the default parameters for TwoDraw:
// patchColor: undefined, // random gray
// turtleColor: undefined, // random color
// linkColor: 'white',
// linkWidth: 1,
// shape: 'dart',
// shapeSize: 1,
// See overrides in draw parameters below
const turtleColors = {
infected: 'red',
susceptible: 'blue',
resistant: 'gray',
}
// init and run the model 500 steps, 30 fps, async (don't block browser)
export default async function run(Model, viewOptions = {}, useTwoDraw = true) {
const model = new Model()
await model.startup()
model.setup()
const view = useTwoDraw
? new TwoDraw(model, viewOptions)
: new TwoView(model, viewOptions)
// Debug: Push several properties to window from inside this module.
util.toWindow({ model, view, util })
if (!useTwoDraw)
// setup static patches colors. Done by default in TwoDraw
view.createPatchPixels(i => ColorMap.DarkGray.randomColor().pixel)
await util.timeoutLoop(
() => {
model.step()
// model.tick()
if (useTwoDraw) {
view.draw({
turtleColor: t => turtleColors[t.state],
shape: 'circle',
shapeSize: 1.5,
linkColor: 'rgba(255, 255, 255, 0.50',
linkWidth: 1,
})
} else {
view.drawPatches() // redraw cached patches colors
view.drawLinks(model.links, {
color: 'rgba(255, 255, 255, 0.50',
width: 1,
})
view.drawTurtles(model.turtles, t => ({
shape: 'circle',
color: turtleColors[t.state],
size: 1.5,
}))
}
},
500,
33
)
// Debug: return a sample of the model.
return util.sampleModel(model)
}