humus-old/server/create-server-room.js
2019-04-21 16:31:53 -05:00

473 lines
12 KiB
JavaScript

import {World} from '@avocado/physics/matter/world';
import {Room} from '@avocado/topdown';
// Helpers to make scripts for now.
const behaviorTraversal = (path, value) => {
const traversal = {
type: 'traversal',
steps: path.map((key) => {
return {
type: 'key',
key: key,
};
}),
};
if ('undefined' !== typeof value) {
if ('object' === typeof value && 'traversal' === value.type) {
traversal.value = value;
}
else {
traversal.value = {
type: 'literal',
value,
};
}
}
return traversal;
}
const behaviorInvoke = (path, args) => {
const traversal = behaviorTraversal(path);
traversal.steps.push({
type: 'invoke',
args: args.map((arg) => {
if ('object' === typeof arg && 'traversal' === arg.type) {
return arg;
}
return {
type: 'literal',
value: arg,
};
}),
});
return traversal;
}
// Behaviors!
const move = behaviorInvoke(['entity', 'moveFor'], [
behaviorInvoke(['global', 'randomNumber'], [0.25, 2.5, false])
]);
const stopAnimating = behaviorTraversal(
['entity', 'isAnimating'],
false
);
const firstWait = behaviorInvoke(['global', 'wait'], [
behaviorInvoke(['global', 'randomNumber'], [1, 4, false])
]);
const turn = behaviorTraversal(
['entity', 'direction'],
behaviorInvoke(['global', 'randomNumber'], [0, 3])
);
const secondWait = behaviorInvoke(['global', 'wait'], [
behaviorInvoke(['global', 'randomNumber'], [0.5, 3, false])
]);
const startAnimating = behaviorTraversal(
['entity', 'isAnimating'],
true
);
// A fire.
function fireJSON(position) {
return {
traits: {
animated: {
params: {
animations: {
idle: {
offset: [0, 0],
uri: '/fire.animation.json',
},
}
},
},
audible: {
params: {
sounds: {
fire: {
src: '/fire.wav',
volume: 0.05,
},
}
}
},
collider: {
params: {
isSensor: true,
},
},
damaging: {
params: {
damagingSound: 'fire',
damageSpecs: [
{
affinity: 'fire',
lock: 0.075,
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: {
src: '/ded.wav',
volume: 0.1,
},
}
}
},
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 = behaviorTraversal(
['context', 'json'],
{
traits: {
positioned: {
state: {},
},
},
},
);
const setJSONX = behaviorTraversal(
['context', 'json', 'traits', 'positioned', 'state', 'x'],
behaviorInvoke(['global', 'multiply'], [
behaviorTraversal(['entity', 'x']), 4
]),
);
const setJSONY = behaviorTraversal(
['context', 'json', 'traits', 'positioned', 'state', 'y'],
behaviorInvoke(['global', 'multiply'], [
behaviorTraversal(['entity', 'y']), 4
]),
);
const spawn = behaviorInvoke(['entity', 'spawn'], [
'kitteh',
behaviorTraversal(['context', 'json']),
]);
const json = JSON.parse(JSON.stringify(kittyJSON(position)));
const {traits} = json;
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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 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,
],
},
tilesetUri: '/tileset.json',
},
},
};
function mamaKittySpawnerJSON() {
const storeJSON = behaviorTraversal(
['context', 'json'],
{
traits: {
positioned: {
state: {},
},
},
},
);
const setJSONX = behaviorTraversal(
['context', 'json', 'traits', 'positioned', 'state', 'x'],
behaviorInvoke(['global', 'multiply'], [
behaviorInvoke(['global', 'randomNumber'], [50, 234]), 4
]),
);
const setJSONY = behaviorTraversal(
['context', 'json', 'traits', 'positioned', 'state', 'y'],
behaviorInvoke(['global', 'multiply'], [
behaviorInvoke(['global', 'randomNumber'], [50, 234]), 4
]),
);
const spawn = behaviorInvoke(['entity', 'spawn'], [
'mama',
behaviorTraversal(['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: 4,
},
},
},
};
}
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 < 15; ++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;
}