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

474 lines
12 KiB
JavaScript
Raw Normal View History

2019-04-21 21:13:56 -05:00
import {buildInvoke, buildTraversal} from '@avocado/behavior';
2019-04-23 15:28:17 -05:00
import {Vector} from '@avocado/math';
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
2019-04-28 22:35:20 -05:00
import {AFFINITY_FIRE} from '../common/combat/constants';
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: {
2019-04-21 21:55:03 -05:00
jitter: 0.1,
2019-04-21 02:28:58 -05:00
offset: [0, 0],
uri: '/fire.animation.json',
},
}
},
},
audible: {
params: {
sounds: {
fire: {
2019-04-21 23:01:47 -05:00
uri: '/fire.sound.json',
},
}
}
},
collider: {
params: {
isSensor: true,
},
},
damaging: {
params: {
damagingSound: 'fire',
2019-04-19 12:16:28 -05:00
damageSpecs: [
{
2019-04-28 22:35:20 -05:00
affinity: AFFINITY_FIRE,
2019-04-28 22:48:39 -05:00
lock: 0.45,
power: 15,
2019-04-19 12:16:28 -05:00
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-04-22 21:03:39 -05:00
// Healing fire!
function blueFireJSON(position) {
const json = fireJSON(position);
json.traits.animated.params.animations.idle.uri = '/blue-fire.animation.json';
2019-04-28 22:48:39 -05:00
json.traits.damaging.params.damageSpecs[0].power = -15;
2019-04-22 21:03:39 -05:00
json.traits.audible.params.sounds.fire.uri = '/blue-fire.sound.json';
return json;
}
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: {
2019-04-21 23:01:47 -05:00
uri: '/ded.sound.json',
},
}
}
},
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: {
2019-04-26 01:40:41 -05:00
maxSpawns: 30,
2019-04-21 01:31:07 -05:00
},
};
return json;
}
2019-03-27 15:51:58 -05:00
// Room.
2019-04-26 01:40:41 -05:00
const roomTileSize = [24, 24];
2019-04-23 15:28:17 -05:00
const roomSize = Vector.mul([16, 16], roomTileSize);
2019-03-27 15:51:58 -05:00
const roomJSON = {
2019-04-23 15:28:17 -05:00
size: roomSize,
2019-03-27 17:16:09 -05:00
layers: {
everything: {
2019-03-27 15:51:58 -05:00
entities: [],
tiles: {
2019-04-23 15:28:17 -05:00
size: roomTileSize,
2019-03-27 15:51:58 -05:00
data: [
2019-04-26 01:40:41 -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, 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, 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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, 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,
2019-03-27 15:51:58 -05:00
],
},
tilesetUri: '/tileset.json',
2019-03-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'], [
2019-04-26 01:40:41 -05:00
buildInvoke(['global', 'randomNumber'], [100, roomSize[0] - 100]), 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'], [
2019-04-26 01:40:41 -05:00
buildInvoke(['global', 'randomNumber'], [100, roomSize[1] - 100]), 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: {
2019-04-22 18:20:27 -05:00
maxSpawns: 2,
2019-04-21 15:02:18 -05:00
},
},
},
};
}
2019-04-20 22:37:59 -05:00
2019-04-28 22:48:27 -05:00
for (let i = 0; i < 50; ++i) {
2019-04-23 15:28:17 -05:00
const x = Math.floor(Math.random() * (roomSize[0] - 100)) + 50;
const y = Math.floor(Math.random() * (roomSize[1] - 100)) + 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-23 15:28:17 -05:00
const x = Math.floor(Math.random() * (roomSize[0] - 100)) + 50;
const y = Math.floor(Math.random() * (roomSize[1] - 100)) + 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-26 01:40:41 -05:00
for (let i = 0; i < 60; ++i) {
2019-04-23 15:28:17 -05:00
const x = Math.floor(Math.random() * (roomSize[0] - 100)) + 50;
const y = Math.floor(Math.random() * (roomSize[1] - 100)) + 50;
2019-04-20 22:37:59 -05:00
roomJSON.layers.everything.entities.push(fireJSON([x * 4, y * 4]));
}
2019-04-28 22:46:16 -05:00
for (let i = 0; i < 5; ++i) {
2019-04-23 15:28:17 -05:00
const x = Math.floor(Math.random() * (roomSize[0] - 100)) + 50;
const y = Math.floor(Math.random() * (roomSize[1] - 100)) + 50;
2019-04-22 21:03:39 -05:00
roomJSON.layers.everything.entities.push(blueFireJSON([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
}