agentscript
Version:
AgentScript Model in Model/View architecture
81 lines (68 loc) • 2.46 kB
HTML
<html>
<head>
<title>shapes</title>
</head>
<body>
<!-- HTML emojis -->
<!-- <p>I am copyright © I am a lion 🦁 Me too 🦁 </p> -->
<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 Model from 'https://code.agentscript.org/models/HelloModel.js'
import World from 'https://code.agentscript.org/src/World.js'
import Shapes from 'https://code.agentscript.org/src/Shapes.js'
import * as emoji from 'https://code.agentscript.org/src/emoji.js'
// JS emojies
// console.log("At: \x40, Sum: \u2211, Tree: \u{1F332}")
const shapes = new Shapes()
function createEmojiPath(name, codePoint) {
const can = emoji.emoji2can(codePoint)
shapes.createImagePath(name, can)
}
async function addImages() {
await shapes.imagePathPromise(
'twitter',
'https://code.agentscript.org/models/data/twitter.png'
)
await shapes.imagePathPromise(
'redfish',
'https://code.agentscript.org/models/data/redfish.png'
)
createEmojiPath('lion', 0x1F981)
createEmojiPath('smiley', 0x1F600)
createEmojiPath('tree', 0x1F332)
}
function turtleName(t) {
return shapes.nameAtIndex(t.id)
}
await addImages()
const model = new Model(World.defaultOptions(25, 16))
model.population = 100
await model.startup()
model.setup()
const view = new TwoDraw(
model,
{
div: 'modelDiv',
patchSize: 20,
},
{
turtlesShape: t => turtleName(t),
turtlesSize: t => (turtleName(t) === 'redfish' ? 5 : 3),
turtlesRotate: t => (!["lion", "smiley", "tree"].includes(turtleName(t))),
}
)
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>