agentscript
Version:
AgentScript Model in Model/View architecture
61 lines (52 loc) • 1.89 kB
HTML
<html>
<head>
<title>ants</title>
</head>
<body>
<script type="module">
import * as util from 'https://code.agentscript.org/src/utils.js'
import Animator from 'https://code.agentscript.org/src/Animator.js'
import ThreeDraw from 'https://code.agentscript.org/src/ThreeDraw.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 ThreeDraw(
model,
{ div: 'modelDiv' },
drawOptions
)
util.toWindow({ util, model, view })
await new Animator(
() => {
model.step()
view.draw()
},
500,
30
)
view.idle()
</script>
<div id="modelDiv"></div>
</body>
</html>