vis = {
const sim = new AA.Simulation({
width: 920,
height: 600,
gridStep: 40,
beforeTick: () => sim.state.workTime = sim.tickIndex % 1800 < 900,
});
invalidation.then(() => sim.end());
const xHouse = new Set([1, 3, 5, 9, 11, 13, 17, 19, 21]);
const yHouse = new Set([1, 3, 5, 7]);
const xRoad = new Set([7, 15]);
const yRoad = new Set([2, 6]);
for (let x = 0; x <= 22; x++) {
for (let y = 0; y <= 8; y++) {
sim.squareAt(x, y).label('land',
xRoad.has(x) || yRoad.has(y)
? 'road'
: xHouse.has(x) && yHouse.has(y)
? 'house'
: 'grass'
);
}
}
const houses = sim.withLabel('land', 'house');
const grass = sim.withLabel('land', 'grass');
const works = [
[0, 4, 11, 14],
[6, 7, 12, 14],
[9, 17, 14, 14],
[21, 22, 9, 11],
[20, 22, 13, 14],
].map(indexLimits => {
return new AA.Zone({indexLimits})
.label('land', 'work')
.vis({image: 'bricks-gray.png', tile: true})
.addTo(sim);
});
for (let sq of sim.squaresInRect([0, 22, 9, 14])) {
sq.label('land', 'stone');
}
for (let house of houses) {
house.populate({
n: AA.random.int(1, 10)(),
padding: 4,
radius: 3.5,
setup: p => {
p.maxSpeed = AA.random.uniform(1.2, 1.7)();
p.state = {
house,
work: works[AA.random.int(works.length)()],
}
p.label('person', true);
p.vis({image: 'boid.png'})
}
});
}
const people = sim.withLabel('person');
const paths = sim.paths(
[
...works.map(w => [w, w]),
...[...houses].map(h => [h, h])
],
[
[grass, Infinity]
]
);
for (let p of people) {
p.steer.set('go-work', {
disabled: () => !sim.state.workTime,
behavior: 'go',
paths: paths.get(p.state.work)
});
p.steer.set('go-home', {
disabled: () => sim.state.workTime,
behavior: 'go',
paths: paths.get(p.state.house)
});
}
sim.interaction.set('person-person-repel', {
group1: people,
behavior: 'repel',
off: 3,
strength: 1
});
sim.interaction.set('grass-person-repel', {
group1: grass,
group2: sim.actors,
behavior: 'repel',
off: 10,
strength: 2
});
sim.interaction.set('stay-at-home', {
disabled: () => sim.state.workTime,
group1: houses,
group2: sim.actors,
behavior: 'repel',
inward: true,
off: 5,
strength: 1
});
sim.interaction.set('stay-at-work', {
disabled: () => !sim.state.workTime,
group1: works,
group2: sim.actors,
behavior: 'repel',
inward: true,
off: 5,
strength: 1
});
const squareImageLookup = {
grass: 'nature-grass.png',
road: 'road.png',
stone: 'stones.png',
house: 'bricks-yellow.png'
};
for (let sq of sim.squares) {
sq.zIndex = -Infinity;
sq.vis({image: squareImageLookup[sq.label('land')]});
}
return AV.visObs(sim, {
images: [
'https://cdn.jsdelivr.net/gh/gjmcn/sprites/sprite-sheets/outside.json',
'https://cdn.jsdelivr.net/gh/gjmcn/sprites/sprite-sheets/shapes.json'
],
});
}