From 78d655fe02e61ee3d4ad370e075544a750c88d81 Mon Sep 17 00:00:00 2001 From: cha0s Date: Sun, 21 Apr 2019 01:31:07 -0500 Subject: [PATCH] fun: mama kitteh! --- server/create-server-room.js | 243 +++++++++++++++++++---------------- yarn.lock | 9 +- 2 files changed, 142 insertions(+), 110 deletions(-) diff --git a/server/create-server-room.js b/server/create-server-room.js index 9b834bb..3057da4 100644 --- a/server/create-server-room.js +++ b/server/create-server-room.js @@ -1,5 +1,67 @@ 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, 1, 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]) +]); + + // A fire. function fireJSON(position) { return { @@ -115,38 +177,6 @@ function flowerBarrelJSON(position) { } // A kitteh. function kittyJSON(position) { - const buildRandom = (min, max, floor = true) => { - return { - type: 'traversal', - 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: {}, @@ -154,7 +184,7 @@ function kittyJSON(position) { params: { animations: { idle: { - offset: [0, -6], + offset: [0, -3], uri: '/kitty.animation.json', }, } @@ -180,77 +210,10 @@ function kittyJSON(position) { routine: { type: 'actions', traversals: [ - { - type: 'action', - steps: [ - { - type: 'key', - key: 'entity', - }, - { - type: 'key', - key: 'moveFor', - }, - { - type: 'invoke', - args: [ - buildRandom(0.25, 1, false), - ], - }, - ], - }, - { - type: 'action', - steps: [ - { - type: 'key', - key: 'global', - }, - { - type: 'key', - key: 'wait', - }, - { - type: 'invoke', - args: [ - buildRandom(1, 4), - ], - }, - ], - }, - { - type: 'action', - steps: [ - { - type: 'key', - key: 'entity', - }, - { - type: 'key', - key: 'direction', - }, - ], - value: buildRandom(0, 3), - }, - { - type: 'action', - steps: [ - { - type: 'key', - key: 'global', - }, - { - type: 'key', - key: 'wait', - }, - { - type: 'invoke', - args: [ - buildRandom(0.5, 3), - ], - }, - ], - }, + move, + firstWait, + turn, + secondWait, ], } }, @@ -271,7 +234,7 @@ function kittyJSON(position) { existent: {}, visible: { state: { - visibleScale: [1.5, 1.5], + visibleScale: [1, 1], } }, mobile: { @@ -291,7 +254,7 @@ function kittyJSON(position) { shape: { type: 'rectangle', position: [0, 0], - size: [12, 6], + size: [8, 4], }, }, }, @@ -299,6 +262,70 @@ function kittyJSON(position) { }, }; } +// 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: 5000, + maxLife: 5000, + }; + traits.animated.params.animations.idle.offset = [0, -8]; + traits.behaved.params.routines.routines.initial.routine.traversals = [ + move, + firstWait, + turn, + storeJSON, + setJSONX, + setJSONY, + spawn, + secondWait, + ]; + 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], @@ -348,10 +375,10 @@ for (let i = 0; i < 20; ++i) { const y = Math.floor(Math.random() * 284) + 50; roomJSON.layers.everything.entities.push(flowerBarrelJSON([x * 4, y * 4])); } -for (let i = 0; i < 50; ++i) { +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(kittyJSON([x * 4, y * 4])); + roomJSON.layers.everything.entities.push(mamaKittyJSON([x * 4, y * 4])); } for (let i = 0; i < 15; ++i) { const x = Math.floor(Math.random() * 284) + 50; diff --git a/yarn.lock b/yarn.lock index 58bdaa2..0b1741a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -21,8 +21,8 @@ resolved "https://npm.i12e.cha0s.io/@avocado%2fcore/-/core-1.0.4.tgz#ca0c9c91f91b51d02655808f3f85cf7f9b845b29" "@avocado/entity@1.x": - version "1.0.9" - resolved "https://npm.i12e.cha0s.io/@avocado%2fentity/-/entity-1.0.9.tgz#aea08882c343b2a534d6cfc86d42263686c481cc" + version "1.0.10" + resolved "https://npm.i12e.cha0s.io/@avocado%2fentity/-/entity-1.0.10.tgz#ff39fcbe41bb20b75d7defc4eb069d535dd9543a" dependencies: "@avocado/core" "1.x" "@avocado/graphics" "1.x" @@ -34,6 +34,7 @@ debug "^3.1.0" immutable "4.0.0-rc.12" lodash.mapvalues "4.6.0" + lodash.merge "4.6.1" lodash.without "4.4.0" "@avocado/graphics@1.x": @@ -2874,6 +2875,10 @@ lodash.mapvalues@4.6.0: version "4.6.0" resolved "https://npm.i12e.cha0s.io/lodash.mapvalues/-/lodash.mapvalues-4.6.0.tgz#1bafa5005de9dd6f4f26668c30ca37230cc9689c" +lodash.merge@4.6.1: + version "4.6.1" + resolved "https://npm.i12e.cha0s.io/lodash.merge/-/lodash.merge-4.6.1.tgz#adc25d9cb99b9391c59624f379fbba60d7111d54" + lodash.without@4.4.0: version "4.4.0" resolved "https://npm.i12e.cha0s.io/lodash.without/-/lodash.without-4.4.0.tgz#3cd4574a00b67bae373a94b748772640507b7aac"