UNPKG

agentscript

Version:

AgentScript Model in Model/View architecture

119 lines (103 loc) 3.71 kB
<html> <head> <title>ants</title> <link rel="icon" type="image/x-icon" href="../favicon.ico" /> </head> <body> <script type="module"> import * as util from '../src/utils.js' import TwoDraw from '../src/TwoDraw.js' import Animator from '../src/Animator.js' import Color from '../src/Color.js' import ColorMap from '../src/ColorMap.js' import Model from '../models/AntsModel.js' import GUI from '../src/GUI.js' import Keyboard from '../src/Keyboard.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() }, -1, // run forever 30 // fps ).startStats() // ==== Define gui commands ==== let template = { fps: { // slider val: [40, [5, 60, 5]], cmd: val => anim.setFps(val), }, stop: { // switch/toggle val: !anim.isRunning, // bool cmd: val => { if (val) { anim.stop() } else { anim.start() } }, // cmd: val => anim.toggle(), }, once: { // button cmd: () => anim.once() //gui.update(stop, false) }, width: { // slider val: [500, [100, 1000, 25]], cmd: val => view.width = val, }, turtlesShape: { // chooser val: ['bug', ['bug', 'dart', 'person', 'circle', 'arrow']], cmd: val => view.drawOptions.turtlesShape = val, }, turtlesSize: { // chooser val: [1, [1, 10, 1]], cmd: val => view.drawOptions.turtlesSize = val, }, downloadCanvas: { // button cmd: () => view.downloadCanvas() }, } const gui = new GUI(template) // ==== Define keyboard commands ==== const keyModCmds = [ { key: 's', cmd: () => anim.toggle() }, { key: 'o', cmd: () => anim.once() }, { key: 'd', cmd: () => view.downloadCanvas() }, ] const keyboard = new Keyboard(keyModCmds) // const keyboard = new Keyboard(keyModCmds, 'alt') keyboard.start() // ==== End, debug if needed util.toWindow({ util, model, view, anim, gui, keyboard }) </script> <div id="modelDiv"></div> </body> </html>