humus-old/server/create-server-room.js
2019-04-22 18:20:27 -05:00

454 lines
11 KiB
JavaScript

import {buildInvoke, buildTraversal} from '@avocado/behavior';
import {World} from '@avocado/physics/matter/world';
import {Room} from '@avocado/topdown';
// Behaviors!
const move = buildInvoke(['entity', 'moveFor'], [
buildInvoke(['global', 'randomNumber'], [0.25, 2.5, false])
]);
const stopAnimating = buildTraversal(
['entity', 'isAnimating'],
false
);
const firstWait = buildInvoke(['global', 'wait'], [
buildInvoke(['global', 'randomNumber'], [1, 4, false])
]);
const turn = buildTraversal(
['entity', 'direction'],
buildInvoke(['global', 'randomNumber'], [0, 3])
);
const secondWait = buildInvoke(['global', 'wait'], [
buildInvoke(['global', 'randomNumber'], [0.5, 3, false])
]);
const startAnimating = buildTraversal(
['entity', 'isAnimating'],
true
);
// A fire.
function fireJSON(position) {
return {
traits: {
animated: {
params: {
animations: {
idle: {
jitter: 0.1,
offset: [0, 0],
uri: '/fire.animation.json',
},
}
},
},
audible: {
params: {
sounds: {
fire: {
uri: '/fire.sound.json',
},
}
}
},
collider: {
params: {
isSensor: true,
},
},
damaging: {
params: {
damagingSound: 'fire',
damageSpecs: [
{
affinity: 'fire',
lock: 0.15,
power: 5,
variance: 0.25,
},
],
},
},
darkened: {
params: {
isDarkened: false,
},
},
existent: {},
visible: {
params: {
filter: 'bloom',
},
},
physical: {},
positioned: {
state: {
x: position[0],
y: position[1],
},
},
shaped: {
params: {
shape: {
type: 'rectangle',
position: [0, 0],
size: [16, 16],
},
},
},
},
};
}
// A flower barrel.
function flowerBarrelJSON(position) {
return {
traits: {
alive: {
state: {
maxLife: 1000,
life: 1000,
},
},
collider: {},
emitter: {},
existent: {},
physical: {},
pictured: {
params: {
images: {
initial: {
offset: [0, -8],
size: [32, 32], // Derive?
uri: '/flower-barrel.png',
},
}
},
},
positioned: {
state: {
x: position[0],
y: position[1],
},
},
shaped: {
params: {
shape: {
type: 'rectangle',
position: [0, 0],
size: [20, 10],
},
},
},
visible: {},
vulnerable: {},
},
};
}
// A kitteh.
function kittyJSON(position) {
return {
traits: {
alive: {},
animated: {
params: {
animations: {
idle: {
offset: [0, -3],
uri: '/kitty.animation.json',
},
}
},
},
audible: {
params: {
sounds: {
deathSound: {
uri: '/ded.sound.json',
},
}
}
},
behaved: {
params: {
routines: {
type: 'routines',
routines: {
initial: {
type: 'routine',
routine: {
type: 'actions',
traversals: [
move,
stopAnimating,
firstWait,
turn,
secondWait,
startAnimating,
],
}
},
},
},
},
},
collider: {},
directional: {
params: {
directionCount: 4,
},
state: {
direction: 2,
},
},
emitter: {},
existent: {},
visible: {
state: {
visibleScale: [1, 1],
}
},
mobile: {
state: {
speed: 40,
},
},
physical: {},
positioned: {
state: {
x: position[0],
y: position[1],
},
},
shaped: {
params: {
shape: {
type: 'rectangle',
position: [0, 0],
size: [8, 4],
},
},
},
vulnerable: {},
},
};
}
// A MAMA kitteh.
function mamaKittyJSON(position) {
const storeJSON = buildTraversal(
['context', 'json'],
{
traits: {
positioned: {
state: {},
},
},
},
);
const setJSONX = buildTraversal(
['context', 'json', 'traits', 'positioned', 'state', 'x'],
buildInvoke(['global', 'multiply'], [
buildTraversal(['entity', 'x']), 4
]),
);
const setJSONY = buildTraversal(
['context', 'json', 'traits', 'positioned', 'state', 'y'],
buildInvoke(['global', 'multiply'], [
buildTraversal(['entity', 'y']), 4
]),
);
const spawn = buildInvoke(['entity', 'spawn'], [
'kitteh',
buildTraversal(['context', 'json']),
]);
const playDeathSound = buildInvoke(['entity', 'playSound'], [
buildTraversal(['entity', 'deathSound']),
]);
const squeeze = buildInvoke(['entity', 'transition'], [
{
opacity: 0,
visibleScaleX: .3,
visibleScaleY: 3,
},
0.2,
]);
const murderKitties = buildInvoke(['entity', 'killAllChildren']);
const json = JSON.parse(JSON.stringify(kittyJSON(position)));
const {traits} = json;
traits.alive.params = {
deathActions: {
type: 'actions',
traversals: [
playDeathSound,
murderKitties,
squeeze,
],
},
};
traits.alive.state = {
life: 500,
maxLife: 500,
};
traits.animated.params.animations.idle.offset = [0, -8];
traits.behaved.params.routines.routines.initial.routine.traversals.push(...[
storeJSON,
setJSONX,
setJSONY,
spawn,
]);
traits.visible.state.visibleScale = [2, 2];
traits.shaped.params.shape.size = [16, 8];
traits.spawner = {
params: {
spawns: {
kitteh: kittyJSON([100, 100]),
},
},
state: {
maxSpawns: 15,
},
};
return json;
}
// Room.
const roomJSON = {
size: [384, 384],
layers: {
everything: {
entities: [],
tiles: {
size: [24, 24],
data: [
1, 2, 3, 4, 1, 2, 3, 4, 1 , 2 , 3 , 4 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1 , 2 , 3 , 4 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1 , 2 , 3 , 4 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1 , 2 , 3 , 4 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1 , 2 , 3 , 4 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1 , 2 , 3 , 4 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1 , 2 , 3 , 4 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1 , 2 , 3 , 4 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1 , 2 , 3 , 4 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
// 1, 2, 3, 4, 1, 2, 3, 4, 5 , 6 , 7 , 8 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
// 1, 2, 3, 4, 1, 2, 3,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 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1 , 2 , 3 , 4 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1 , 2 , 3 , 4 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1 , 2 , 3 , 4 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 1 , 2 , 3 , 4 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
],
},
tilesetUri: '/tileset.json',
},
},
};
function mamaKittySpawnerJSON() {
const storeJSON = buildTraversal(
['context', 'json'],
{
traits: {
positioned: {
state: {},
},
},
},
);
const setJSONX = buildTraversal(
['context', 'json', 'traits', 'positioned', 'state', 'x'],
buildInvoke(['global', 'multiply'], [
buildInvoke(['global', 'randomNumber'], [50, 234]), 4
]),
);
const setJSONY = buildTraversal(
['context', 'json', 'traits', 'positioned', 'state', 'y'],
buildInvoke(['global', 'multiply'], [
buildInvoke(['global', 'randomNumber'], [50, 234]), 4
]),
);
const spawn = buildInvoke(['entity', 'spawn'], [
'mama',
buildTraversal(['context', 'json']),
]);
return {
traits: {
behaved: {
params: {
routines: {
type: 'routines',
routines: {
initial: {
type: 'routine',
routine: {
type: 'actions',
traversals: [
storeJSON,
setJSONX,
setJSONY,
spawn,
],
}
},
},
},
},
},
existent: {},
spawner: {
params: {
spawns: {
mama: mamaKittyJSON([0, 0]),
},
},
state: {
maxSpawns: 2,
},
},
},
};
}
for (let i = 0; i < 20; ++i) {
const x = Math.floor(Math.random() * 284) + 50;
const y = Math.floor(Math.random() * 284) + 50;
roomJSON.layers.everything.entities.push(flowerBarrelJSON([x * 4, y * 4]));
}
for (let i = 0; i < 1; ++i) {
const x = Math.floor(Math.random() * 284) + 50;
const y = Math.floor(Math.random() * 284) + 50;
roomJSON.layers.everything.entities.push(mamaKittySpawnerJSON());
}
for (let i = 0; i < 30; ++i) {
const x = Math.floor(Math.random() * 284) + 50;
const y = Math.floor(Math.random() * 284) + 50;
roomJSON.layers.everything.entities.push(fireJSON([x * 4, y * 4]));
}
export function createRoom() {
const room = (new Room()).fromJSON(roomJSON);
room.world = new World();
return room;
}