agentscript
Version:
AgentScript Model in Model/View architecture
66 lines (58 loc) • 2.1 kB
HTML
<html>
<head>
<title>ants</title>
</head>
<body>
<script type="module">
import * as util from 'https://code.agentscript.org/src/utils.js'
import TwoDraw from 'https://code.agentscript.org/src/TwoDraw.js'
import Animator from 'https://code.agentscript.org/src/Animator.js'
import Color from 'https://code.agentscript.org/src/Color.js'
import ColorMap from 'https://code.agentscript.org/src/ColorMap.js'
import Model from 'https://code.agentscript.org/models/AntsModel.js'
// Define colors and colormaps
const nestColor = Color.typedColor('yellow')
const foodColor = Color.typedColor('blue')
const nestColorMap = ColorMap.gradientColorMap(20, [
'black',
nestColor,
])
const foodColorMap = ColorMap.gradientColorMap(20, [
'black',
foodColor,
])
const drawOptions = {
patchesColor: p => {
if (p.isNest) return nestColor
if (p.isFood) return foodColor
return p.foodPheromone > p.nestPheromone
? foodColorMap.scaleColor(p.foodPheromone, 0, 1)
: nestColorMap.scaleColor(p.nestPheromone, 0, 1)
},
turtlesShape: 'bug',
turtlesSize: 3,
turtlesColor: t => (t.carryingFood ? nestColor : foodColor),
}
// ==============================
const model = new Model()
await model.startup()
model.setup()
const view = new TwoDraw(model, {
div: 'modelDiv',
useSprites: true, // ant shape difficult to draw
width: 700, // sets patchSize, will round to integer if useSprites
drawOptions,
})
const anim = new Animator(
() => {
model.step()
view.draw()
},
500, // run 500 steps
30 // 30 fps
)
util.toWindow({ util, model, view, anim })
</script>
<div id="modelDiv"></div>
</body>
</html>