Skip to content

Commit 61d4580

Browse files
committed
patches.oneOf
- patches.oneOf: const patch = this.patches.oneOf() t.setxy(patch.x, patch.y) => t.moveTo(this.patches.oneOf()) - Hello3ZModel HelloModel HelloPlusModel KelpForestModel LinkTravelModel PheromoneModel
1 parent e2bcb02 commit 61d4580

9 files changed

+29
-37
lines changed

models/CountiesModel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as util from 'https://code.agentscript.org/src/utils.js'
2-
import HelloModel from './HelloModel.js'
2+
import HelloModel from 'https://code.agentscript.org/models/HelloModel.js'
33
import { booleanPointInPolygon } from '../vendor/turfImports.js'
44

55
const url = import.meta.resolve('./data/nmcounties.json')

models/Hello3ZModel.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export default class HelloModel extends Model {
2020
this.turtles.setDefault('atEdge', 'bounce')
2121

2222
this.turtles.create(this.population, t => {
23-
const patch = this.patches.oneOf()
24-
t.setxy(patch.x, patch.y)
23+
t.moveTo(this.patches.oneOf())
2524
this.moveToSphere(t)
2625
})
2726

models/HelloModel.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ class HelloModel extends Model {
1919
this.turtles.setDefault('atEdge', 'bounce')
2020

2121
// create "population" turtles placed on random patches
22-
this.turtles.create(this.population, t => {
23-
const patch = this.patches.oneOf()
24-
t.setxy(patch.x, patch.y)
25-
})
22+
this.turtles.create(this.population, t =>
23+
t.moveTo(this.patches.oneOf())
24+
)
2625

2726
// If links.too is true, create a link from every turtle to another turtle
2827
if (this.linksToo) {

models/HelloPlusModel.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ export default class HelloPlusModel extends HelloModel {
3838
if (delta < 0) {
3939
util.repeat(-delta, () => this.turtles.oneOf().die())
4040
} else {
41-
this.turtles.create(delta, t => {
42-
const patch = this.patches.oneOf()
43-
t.setxy(patch.x, patch.y)
44-
})
41+
this.turtles.create(delta, t => t.moveTo(this.patches.oneOf()))
4542
}
4643
// make sure all turtles have at least one link
4744
this.turtles.ask(t => {

models/KelpForestModel.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,16 @@ class KelpForestModel extends Model {
2121
this.turtleBreeds('kelp urchin seaStar')
2222

2323
this.kelp.create(this.numKelp, t => {
24-
const patch = this.patches.oneOf()
25-
t.setxy(patch.x, patch.y)
24+
t.moveTo(this.patches.oneOf())
2625
})
2726

2827
this.seaStar.create(this.numSeastar, t => {
29-
const patch = this.patches.oneOf()
30-
t.setxy(patch.x, patch.y)
28+
t.moveTo(this.patches.oneOf())
3129
})
3230
this.seaStar.setDefault('atEdge', 'bounce')
3331

3432
this.urchin.create(this.numUrchin, t => {
35-
const patch = this.patches.oneOf()
36-
t.setxy(patch.x, patch.y)
33+
t.moveTo(this.patches.oneOf())
3734
})
3835
}
3936

@@ -52,17 +49,15 @@ class KelpForestModel extends Model {
5249
spawnUrchin() {
5350
if (this.day() === 0 && this.year() > 1) {
5451
this.urchin.create(this.urchin.length * 2, t => {
55-
const patch = this.patches.oneOf()
56-
t.setxy(patch.x, patch.y)
52+
t.moveTo(this.patches.oneOf())
5753
})
5854
}
5955
}
6056

6157
reseedKelp() {
6258
if (this.day() === 0 && this.year() > 1) {
6359
this.kelp.create(this.kelp.length * 3, t => {
64-
const patch = this.patches.oneOf()
65-
t.setxy(patch.x, patch.y)
60+
t.moveTo(this.patches.oneOf())
6661
})
6762
}
6863
}
@@ -71,8 +66,7 @@ class KelpForestModel extends Model {
7166
if (this.day() === 0 && this.year() > 1) {
7267
if (this.seaStar.length > 0) {
7368
this.seaStar.create(2, t => {
74-
const patch = this.patches.oneOf()
75-
t.setxy(patch.x, patch.y)
69+
t.moveTo(this.patches.oneOf())
7670
})
7771
}
7872
}

models/LinkTravelModel.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ export default class LinkTravelModel extends Model {
1919
setup() {
2020
this.turtleBreeds('nodes drivers')
2121

22-
// Create the graph node turtles
23-
this.patches.nOf(this.numNodes).ask(p => {
24-
p.sprout(1, this.nodes, t => {
25-
if (this.nodes.length > 2) {
26-
this.links.create(t, this.turtles.otherNOf(2, t))
27-
}
28-
})
29-
})
22+
this.nodes.create(this.numNodes)
23+
24+
// create two links per node to random other nodes.
25+
// two so that there is always an outgoing link other than the incomming one.
26+
this.nodes.ask(n => this.links.create(n, this.nodes.otherNOf(2, n)))
3027

3128
if (this.layoutCircle) {
3229
this.nodes.layoutCircle(this.world.maxX - 1)
30+
} else {
31+
this.nodes.ask(n => n.moveTo(this.patches.oneOf()))
3332
}
3433

3534
util.repeat(this.numDrivers, () => {

models/PheromoneModel.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export default class PheromoneModel extends Model {
2020
setup() {
2121
// create population turtles, placing them randomly on the patches
2222
this.turtles.create(this.population, turtle => {
23-
const patch = this.patches.oneOf()
24-
turtle.setxy(patch.x, patch.y)
23+
turtle.moveTo(this.patches.oneOf())
2524
})
2625

2726
// initialize the patches with the pheromone value of 0

mvc/helloElements.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
anim.stop() // let uielements start/stop animator
4747
anim.setSteps(-1) // in case you want to run forever
4848

49-
await UI.setAppState(model, view, anim) // connect model to uielements
49+
UI.setAppState(model, view, anim) // connect model to uielements
5050

5151
UI.createElements() // use minElements, editable
5252
// UI.createElements(elements, true) // use elements from file, editable

uielements/uielements.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,6 @@ function loadElementsFromJSON() {
568568
let minJsonString = `[{"id":1729270887157,"type":"output","name":"ticks","position":{"x":342,"y":21},"monitor":"model.ticks","fps":"10","command":null},{"id":1729463191305,"type":"range","name":"patchesSize","command":"view.setValue('patchesSize', value)","position":{"x":172,"y":21},"min":"1","max":"20","step":"1","value":"12"},{"id":1730141024864,"type":"checkbox","name":"Run","command":"checked ? anim.start() : anim.stop()","position":{"x":20,"y":21},"checked":false},{"id":1733442807622,"type":"dropdown","name":"fps","command":"anim.setFps(value)","position":{"x":100,"y":21},"options":["2","5","10","20","30","60"],"selected":"30"},{"id":1729535684833,"type":"button","name":"Save","command":"downloadJson()","position":{"x":405,"y":21}}]`
569569
const minJson = JSON.parse(minJsonString)
570570

571-
// userName, modelName set in setAppState()
572571
const persistentStorage = {
573572
// autoDownload: false,
574573
// userName: null,
@@ -597,7 +596,7 @@ function jsonToStorage() {
597596
// persistentStorage.autoDownload = bool
598597
// }
599598

600-
export async function setAppState(model, view, anim) {
599+
export function setAppState(model, view, anim) {
601600
const modelName = model.constructor.name
602601
const elementsName =
603602
modelName.replace('Model', '').toLowerCase() + 'Elements.js'
@@ -615,7 +614,8 @@ export async function setAppState(model, view, anim) {
615614
}
616615

617616
let initialJson, isStaticMode
618-
export async function createElements(json = null, isEditable = json == null) {
617+
// Three modes: new editable, static file, editable file. Any need for new static?
618+
export function createElements(json = null, isEditable = json == null) {
619619
isStaticMode = !isEditable
620620
if (isEditable) {
621621
// Show control panel
@@ -624,7 +624,12 @@ export async function createElements(json = null, isEditable = json == null) {
624624

625625
params.json = json == null ? minJson : json
626626
initialJson = json == null ? '[]' : JSON.stringify(params.json)
627+
627628
console.log('hasUnsavedChanges:', hasUnsavedChanges())
629+
// if (json == null) downloadJson() // need user geesture
630+
if (json == null && isEditable) {
631+
alert('You can use "Save" at any time to download your new Elements.')
632+
}
628633

629634
jsonToStorage()
630635
loadElementsFromJSON()

0 commit comments

Comments
 (0)