humus-old/server/create-server-room.js

455 lines
11 KiB
JavaScript
Raw Normal View History

2019-04-21 21:13:56 -05:00
import {buildInvoke, buildTraversal} from '@avocado/behavior';
2019-03-27 15:51:58 -05:00
import {World} from '@avocado/physics/matter/world';
import {Room} from '@avocado/topdown';
2019-04-21 01:31:07 -05:00
// Behaviors!
2019-04-21 21:13:56 -05:00
const move = buildInvoke(['entity', 'moveFor'], [
buildInvoke(['global', 'randomNumber'], [0.25, 2.5, false])
2019-04-21 01:31:07 -05:00
]);
2019-04-21 21:13:56 -05:00
const stopAnimating = buildTraversal(
2019-04-21 03:43:20 -05:00
['entity', 'isAnimating'],
false
);
2019-04-21 21:13:56 -05:00
const firstWait = buildInvoke(['global', 'wait'], [
buildInvoke(['global', 'randomNumber'], [1, 4, false])
2019-04-21 01:31:07 -05:00
]);
2019-04-21 21:13:56 -05:00
const turn = buildTraversal(
2019-04-21 01:31:07 -05:00
['entity', 'direction'],
2019-04-21 21:13:56 -05:00
buildInvoke(['global', 'randomNumber'], [0, 3])
2019-04-21 01:31:07 -05:00
);
2019-04-21 21:13:56 -05:00
const secondWait = buildInvoke(['global', 'wait'], [
buildInvoke(['global', 'randomNumber'], [0.5, 3, false])
2019-04-21 01:31:07 -05:00
]);
2019-04-21 21:13:56 -05:00
const startAnimating = buildTraversal(
2019-04-21 03:43:20 -05:00
['entity', 'isAnimating'],
true
);
2019-04-21 01:31:07 -05:00
// A fire.
function fireJSON(position) {
return {
traits: {
2019-04-21 02:28:58 -05:00
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',
2019-04-19 12:16:28 -05:00
damageSpecs: [
{
affinity: 'fire',
2019-04-21 16:31:53 -05:00
lock: 0.075,
2019-04-19 12:16:28 -05:00
power: 5,
variance: 0.25,
},
],
},
},
2019-04-21 05:07:46 -05:00
darkened: {
params: {
isDarkened: false,
},
},
existent: {},
2019-04-21 05:07:46 -05:00
visible: {
params: {
filter: 'bloom',
},
},
physical: {},
positioned: {
state: {
x: position[0],
y: position[1],
},
},
shaped: {
params: {
shape: {
type: 'rectangle',
position: [0, 0],
size: [16, 16],
},
},
},
},
};
}
2019-03-27 15:51:58 -05:00
// A flower barrel.
function flowerBarrelJSON(position) {
return {
traits: {
2019-04-20 22:37:59 -05:00
alive: {
state: {
maxLife: 1000,
life: 1000,
},
},
2019-04-08 15:20:43 -05:00
collider: {},
2019-04-20 22:37:59 -05:00
emitter: {},
2019-04-08 15:20:43 -05:00
existent: {},
physical: {},
2019-03-27 15:51:58 -05:00
pictured: {
params: {
images: {
initial: {
offset: [0, -8],
size: [32, 32], // Derive?
uri: '/flower-barrel.png',
},
}
},
},
2019-04-08 13:32:52 -05:00
positioned: {
state: {
x: position[0],
y: position[1],
},
},
shaped: {
2019-03-27 15:51:58 -05:00
params: {
shape: {
type: 'rectangle',
position: [0, 0],
size: [20, 10],
2019-04-08 13:32:52 -05:00
},
2019-03-27 15:51:58 -05:00
},
},
2019-04-20 22:37:59 -05:00
visible: {},
vulnerable: {},
2019-03-27 15:51:58 -05:00
},
};
}
2019-04-08 21:23:00 -05:00
// A kitteh.
function kittyJSON(position) {
return {
traits: {
2019-04-19 14:51:05 -05:00
alive: {},
2019-04-08 21:23:00 -05:00
animated: {
params: {
animations: {
idle: {
2019-04-21 01:31:07 -05:00
offset: [0, -3],
2019-04-08 21:23:00 -05:00
uri: '/kitty.animation.json',
},
}
},
},
audible: {
params: {
sounds: {
deathSound: {
src: '/ded.wav',
volume: 0.1,
},
}
}
},
2019-04-09 09:43:51 -05:00
behaved: {
params: {
routines: {
2019-04-19 15:40:14 -05:00
type: 'routines',
routines: {
initial: {
type: 'routine',
routine: {
type: 'actions',
traversals: [
2019-04-21 01:31:07 -05:00
move,
2019-04-21 03:43:20 -05:00
stopAnimating,
2019-04-21 01:31:07 -05:00
firstWait,
turn,
secondWait,
2019-04-21 03:43:20 -05:00
startAnimating,
2019-04-09 09:43:51 -05:00
],
2019-04-19 15:40:14 -05:00
}
},
2019-04-09 09:43:51 -05:00
},
},
},
},
2019-04-08 21:23:00 -05:00
collider: {},
directional: {
params: {
directionCount: 4,
},
state: {
direction: 2,
},
},
emitter: {},
2019-04-08 21:23:00 -05:00
existent: {},
2019-04-20 22:37:59 -05:00
visible: {
state: {
2019-04-21 01:31:07 -05:00
visibleScale: [1, 1],
2019-04-20 22:37:59 -05:00
}
},
2019-04-08 21:23:00 -05:00
mobile: {
state: {
2019-04-09 09:43:51 -05:00
speed: 40,
2019-04-08 21:23:00 -05:00
},
},
physical: {},
positioned: {
state: {
x: position[0],
y: position[1],
},
},
shaped: {
params: {
shape: {
type: 'rectangle',
position: [0, 0],
2019-04-21 01:31:07 -05:00
size: [8, 4],
2019-04-08 21:23:00 -05:00
},
},
},
vulnerable: {},
2019-04-08 21:23:00 -05:00
},
};
}
2019-04-21 01:31:07 -05:00
// A MAMA kitteh.
function mamaKittyJSON(position) {
2019-04-21 21:13:56 -05:00
const storeJSON = buildTraversal(
2019-04-21 01:31:07 -05:00
['context', 'json'],
{
traits: {
positioned: {
state: {},
},
},
},
);
2019-04-21 21:13:56 -05:00
const setJSONX = buildTraversal(
2019-04-21 01:31:07 -05:00
['context', 'json', 'traits', 'positioned', 'state', 'x'],
2019-04-21 21:13:56 -05:00
buildInvoke(['global', 'multiply'], [
buildTraversal(['entity', 'x']), 4
2019-04-21 01:31:07 -05:00
]),
);
2019-04-21 21:13:56 -05:00
const setJSONY = buildTraversal(
2019-04-21 01:31:07 -05:00
['context', 'json', 'traits', 'positioned', 'state', 'y'],
2019-04-21 21:13:56 -05:00
buildInvoke(['global', 'multiply'], [
buildTraversal(['entity', 'y']), 4
2019-04-21 01:31:07 -05:00
]),
);
2019-04-21 21:13:56 -05:00
const spawn = buildInvoke(['entity', 'spawn'], [
2019-04-21 01:31:07 -05:00
'kitteh',
2019-04-21 21:13:56 -05:00
buildTraversal(['context', 'json']),
2019-04-21 01:31:07 -05:00
]);
2019-04-21 21:44:45 -05:00
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']);
2019-04-21 01:31:07 -05:00
const json = JSON.parse(JSON.stringify(kittyJSON(position)));
const {traits} = json;
2019-04-21 21:44:45 -05:00
traits.alive.params = {
deathActions: {
type: 'actions',
traversals: [
playDeathSound,
murderKitties,
squeeze,
],
},
};
traits.alive.state = {
2019-04-21 15:02:18 -05:00
life: 500,
maxLife: 500,
2019-04-21 01:31:07 -05:00
};
traits.animated.params.animations.idle.offset = [0, -8];
2019-04-21 03:43:20 -05:00
traits.behaved.params.routines.routines.initial.routine.traversals.push(...[
2019-04-21 01:31:07 -05:00
storeJSON,
setJSONX,
setJSONY,
spawn,
2019-04-21 03:43:20 -05:00
]);
2019-04-21 01:31:07 -05:00
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;
}
2019-03-27 15:51:58 -05:00
// Room.
const roomJSON = {
2019-04-08 21:23:00 -05:00
size: [384, 384],
2019-03-27 17:16:09 -05:00
layers: {
everything: {
2019-03-27 15:51:58 -05:00
entities: [],
tiles: {
2019-04-08 21:23:00 -05:00
size: [24, 24],
2019-03-27 15:51:58 -05:00
data: [
2019-04-12 20:16:58 -05:00
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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27 15:51:58 -05:00
},
2019-03-27 17:16:09 -05:00
},
2019-03-27 15:51:58 -05:00
};
2019-04-21 15:02:18 -05:00
function mamaKittySpawnerJSON() {
2019-04-21 21:13:56 -05:00
const storeJSON = buildTraversal(
2019-04-21 15:02:18 -05:00
['context', 'json'],
{
traits: {
positioned: {
state: {},
},
},
},
);
2019-04-21 21:13:56 -05:00
const setJSONX = buildTraversal(
2019-04-21 15:02:18 -05:00
['context', 'json', 'traits', 'positioned', 'state', 'x'],
2019-04-21 21:13:56 -05:00
buildInvoke(['global', 'multiply'], [
buildInvoke(['global', 'randomNumber'], [50, 234]), 4
2019-04-21 15:02:18 -05:00
]),
);
2019-04-21 21:13:56 -05:00
const setJSONY = buildTraversal(
2019-04-21 15:02:18 -05:00
['context', 'json', 'traits', 'positioned', 'state', 'y'],
2019-04-21 21:13:56 -05:00
buildInvoke(['global', 'multiply'], [
buildInvoke(['global', 'randomNumber'], [50, 234]), 4
2019-04-21 15:02:18 -05:00
]),
);
2019-04-21 21:13:56 -05:00
const spawn = buildInvoke(['entity', 'spawn'], [
2019-04-21 15:02:18 -05:00
'mama',
2019-04-21 21:13:56 -05:00
buildTraversal(['context', 'json']),
2019-04-21 15:02:18 -05:00
]);
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,
},
},
},
};
}
2019-04-20 22:37:59 -05:00
for (let i = 0; i < 20; ++i) {
2019-04-09 15:59:26 -05:00
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]));
2019-03-27 15:51:58 -05:00
}
2019-04-21 15:02:18 -05:00
for (let i = 0; i < 1; ++i) {
2019-04-09 15:59:26 -05:00
const x = Math.floor(Math.random() * 284) + 50;
const y = Math.floor(Math.random() * 284) + 50;
2019-04-21 15:02:18 -05:00
roomJSON.layers.everything.entities.push(mamaKittySpawnerJSON());
2019-04-08 21:23:00 -05:00
}
2019-04-20 22:37:59 -05:00
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]));
}
2019-03-27 15:51:58 -05:00
export function createRoom() {
2019-03-27 16:11:49 -05:00
const room = (new Room()).fromJSON(roomJSON);
2019-03-27 15:51:58 -05:00
room.world = new World();
2019-03-27 16:11:49 -05:00
return room;
2019-03-27 15:51:58 -05:00
}