UNPKG

agentscript

Version:

AgentScript Model in Model/View architecture

160 lines (139 loc) 5.18 kB
<html> <head> <title>ants</title> </head> <body> <script type="module"> import * as util from 'https://code.agentscript.org/src/utils.js' import Model from 'https://code.agentscript.org/models/AntsModel.js' import TwoDraw from 'https://code.agentscript.org/src/TwoDraw.js' import Color from 'https://code.agentscript.org/src/Color.js' import ColorMap from 'https://code.agentscript.org/src/ColorMap.js' import Animator from 'https://code.agentscript.org/src/Animator.js' import Mouse from 'https://code.agentscript.org/src/Mouse.js' import Keyboard from 'https://code.agentscript.org/src/Keyboard.js' import GUI from 'https://code.agentscript.org/src/GUI.js' import Plot from 'https://code.agentscript.org/src/Plot.js' // ==== Define draw options, 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), } // ==== Define model, view, anim ==== const model = new Model() await model.startup() model.setup() const view = new TwoDraw(model, { div: 'modelDiv', useSprites: true, // ant shape difficult to draw width: 500, drawOptions, }) const anim = new Animator( () => { model.step() view.draw() updatePlot() }, -1, // run forever 30 // fps ).startStats() anim.setIdle(() => view.draw()) // ==== Define keyboard commands ==== const keyCommands = [ { key: 't', cmd: () => anim.toggle() }, { key: 'o', cmd: () => anim.once() }, { key: 'd', cmd: () => view.downloadCanvas() }, ] const keyboard = new Keyboard(keyCommands).start() // ============ Mouse Handling ================== let selectedTurtle const mouse = new Mouse(model, view, mouse => { const { x, y, action } = mouse switch (action) { case 'mousedown': selectedTurtle = model.turtles.minOneOf(t => t.distanceXY(x, y)) break case 'mousedrag': if (selectedTurtle) selectedTurtle.setxy(x, y) break case 'mouseup': selectedTurtle = null break } view.draw() // Draw whenever mouse has an event }).start() // ==== Define gui commands ==== let template = { fps: { slider: [20, [5, 60, 5]], cmd: val => anim.setFps(val), }, stop: { switch: !anim.isRunning, // bool cmd: val => { if (val) { anim.stop() } else { anim.start() } }, }, once: { button: () => anim.once() }, modelWidth: { slider: [500, [100, 1000, 25]], cmd: val => view.width = val, }, turtlesShape: { chooser: ['bug', ['bug', 'dart', 'person', 'circle', 'arrow']], cmd: val => view.drawOptions.turtlesShape = val, }, turtlesSize: { slider: [6, [1, 10, 1]], cmd: val => view.drawOptions.turtlesSize = val, }, modelTicks: { monitor: [model, 'ticks'], }, downloadCanvas: { button: () => view.downloadCanvas() }, } const gui = new GUI(template) // ==== Setup Plot and provide an updater called by anim ==== const pens = { foodSeekers: ['blue', 'thickLines'], nestSeekers: ['#ffeb40', 'thickLines'], } const options = { title: 'Data', width: 800, height: 200, } const plot = new Plot('plotDiv', pens, options) function updatePlot() { pens.foodSeekers = model.foodSeekers pens.nestSeekers = model.nestSeekers plot.linePlot(pens) } // ==== End, debug if needed util.toWindow({ util, model, view, anim, gui, keyboard }) </script> <div id="modelDiv"></div> <div id="plotDiv"></div> </body> </html>