humus-old/server/create-server-room.js
2019-04-19 19:39:24 -05:00

328 lines
8.9 KiB
JavaScript

import {World} from '@avocado/physics/matter/world';
import {Room} from '@avocado/topdown';
// A fire.
function fireJSON(position) {
return {
traits: {
collider: {
params: {
isSensor: true,
},
},
damaging: {
params: {
damageSpecs: [
{
affinity: 'fire',
lock: 0.15,
power: 5,
variance: 0.25,
},
],
},
},
existent: {},
visible: {
state: {
zIndex: -1,
},
},
physical: {},
pictured: {
params: {
images: {
initial: {
offset: [0, 0],
size: [16, 16], // Derive?
uri: '/fire.png',
},
}
},
},
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: {
collider: {},
existent: {},
visible: {},
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],
},
},
},
},
};
}
// A kitteh.
function kittyJSON(position) {
const buildRandom = (min, max, floor = true) => {
return {
type: 'traversal',
hash: 1,
steps: [
{
type: 'key',
key: 'global',
},
{
type: 'key',
key: 'randomNumber',
},
{
type: 'invoke',
args: [
{
type: 'literal',
value: min,
},
{
type: 'literal',
value: max,
},
{
type: 'literal',
value: floor,
},
],
},
],
};
}
return {
traits: {
alive: {},
animated: {
params: {
animations: {
idle: {
offset: [0, -3],
uri: '/kitty.animation.json',
},
}
},
},
behaved: {
params: {
routines: {
type: 'routines',
routines: {
initial: {
type: 'routine',
routine: {
type: 'actions',
traversals: [
{
type: 'action',
hash: 2,
steps: [
{
type: 'key',
key: 'entity',
},
{
type: 'key',
key: 'moveFor',
},
{
type: 'invoke',
args: [
buildRandom(0.25, 1, false),
],
},
],
},
{
type: 'action',
hash: 3,
steps: [
{
type: 'key',
key: 'global',
},
{
type: 'key',
key: 'wait',
},
{
type: 'invoke',
args: [
buildRandom(1, 4),
],
},
],
},
{
type: 'action',
hash: 4,
steps: [
{
type: 'key',
key: 'entity',
},
{
type: 'key',
key: 'direction',
},
],
value: buildRandom(0, 3),
},
{
type: 'action',
hash: 5,
steps: [
{
type: 'key',
key: 'global',
},
{
type: 'key',
key: 'wait',
},
{
type: 'invoke',
args: [
buildRandom(0.5, 3),
],
},
],
},
],
}
},
},
},
},
},
collider: {},
directional: {
params: {
directionCount: 4,
},
state: {
direction: 2,
},
},
existent: {},
visible: {},
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: {},
},
};
}
// 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, 5 , 6 , 7 , 8 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2, 3, 4, 21 , 22 , 23 , 24 , 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,
1, 2, 3, 4, 1, 2,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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,
],
},
tilesetUri: '/tileset.json',
},
},
};
roomJSON.layers.everything.entities.push(fireJSON([200, 200]));
for (let i = 0; i < 4; ++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 < 40; ++i) {
const x = Math.floor(Math.random() * 284) + 50;
const y = Math.floor(Math.random() * 284) + 50;
roomJSON.layers.everything.entities.push(kittyJSON([x * 4, y * 4]));
}
export function createRoom() {
const room = (new Room()).fromJSON(roomJSON);
room.world = new World();
return room;
}