refactor: children

This commit is contained in:
cha0s 2020-06-22 16:53:49 -05:00
parent a85a984374
commit 1650ccddcc
8 changed files with 179 additions and 146 deletions

View File

@ -2,24 +2,32 @@ import {Context} from './context';
export function behaviorContextTypes() {
return {
bool: {},
bool: {
defaultLiteral: true,
},
context: {
add: {
type: 'void',
label: 'Add $2 as $1.',
args: [
['key', {
type: 'string',
}],
['value', {
type: 'any',
}],
],
children: {
add: {
type: 'void',
label: 'Add $2 as $1.',
args: [
['key', {
type: 'string',
}],
['value', {
type: 'any',
}],
],
},
},
},
number: {},
number: {
defaultLiteral: 0,
},
stream: {},
string: {},
string: {
defaultLiteral: '',
},
void: {},
};
}

View File

@ -60,7 +60,7 @@ export class Context {
}
const description = this.typeDescription(type, undefined);
const subtypes = Object.keys(
Object.values(description)
Object.values(description.children)
.reduce((r, spec) => ({...r, [spec.type]: true}), {})
);
subtypes.forEach((type) => marked[type] = true);
@ -78,15 +78,18 @@ export class Context {
if (!types[type]) {
return {};
}
if ('function' === typeof types[type]) {
return types[type](variable);
}
return types[type];
return {
children: {},
...('function' === typeof types[type] ? types[type](variable) : types[type])
};
}
static types() {
return invokeHookFlat('behaviorContextTypes')
.reduce((r, results) => ({...r, ...results}), {});
.reduce((r, results) => ({
...r,
...results,
}), {});
}
add(key, value, type) {

View File

@ -27,35 +27,37 @@ export function behaviorContextGlobals() {
export function behaviorContextTypes() {
return {
Flow: {
conditional: {
type: 'bool',
label: 'If $1 then run $2.',
args: [
['condition', {
type: 'condition',
}],
['actions', {
type: 'actions',
}],
],
},
parallel: {
type: 'void',
label: 'Run $1 in parallel.',
args: [
['actions', {
type: 'actions',
}],
],
},
serial: {
type: 'void',
label: 'Run $1 serially.',
args: [
['actions', {
type: 'actions',
}],
],
children: {
conditional: {
type: 'bool',
label: 'If $1 then run $2.',
args: [
['condition', {
type: 'condition',
}],
['actions', {
type: 'actions',
}],
],
},
parallel: {
type: 'void',
label: 'Run $1 in parallel.',
args: [
['actions', {
type: 'actions',
}],
],
},
serial: {
type: 'void',
label: 'Run $1 serially.',
args: [
['actions', {
type: 'actions',
}],
],
},
},
},
};

View File

@ -19,14 +19,16 @@ class Timing {
export function behaviorContextTypes() {
return {
Timing: {
wait: {
type: 'void',
label: 'Wait for $1 seconds.',
args: [
['duration', {
type: 'number',
}],
],
children: {
wait: {
type: 'void',
label: 'Wait for $1 seconds.',
args: [
['duration', {
type: 'number',
}],
],
},
},
},
};

View File

@ -35,25 +35,27 @@ class Utility {
export function behaviorContextTypes() {
return {
Utility: {
makeArray: {
type: 'array',
label: 'Make array.',
args: [],
},
makeObject: {
type: 'object',
label: 'Make object.',
args: [],
},
log: {
type: 'void',
label: 'Log.',
args: [],
},
merge: {
type: 'object',
label: 'Merge objects.',
args: [],
children: {
makeArray: {
type: 'array',
label: 'Make array.',
args: [],
},
makeObject: {
type: 'object',
label: 'Make object.',
args: [],
},
log: {
type: 'void',
label: 'Log.',
args: [],
},
merge: {
type: 'object',
label: 'Merge objects.',
args: [],
},
},
},
};

View File

@ -5,24 +5,23 @@ export function behaviorContextTypes() {
const Traits = entity
? Object.values(entity.allTraitInstances()).map((instance) => instance.constructor)
: allTraits();
const traitTypes = Traits
.reduce((r, {behaviorContextTypes, describeState}) => ({
...r,
...behaviorContextTypes(),
...describeState(),
}), {});
return {
...traitTypes,
invokeHook: {
type: 'object',
label: 'Invoke hook.',
args: [
['hook', {
type: 'string',
}],
],
},
const core = {
children: {
invokeHook: {
type: 'object',
label: 'Invoke hook.',
args: [
['hook', {
type: 'string',
}],
],
},
}
};
return Traits
.reduce((r, T) => ({
...r, children: {...r.children, ...T.behaviorContextTypes(), ...T.describeState()},
}), core);
},
};
}

View File

@ -9,30 +9,32 @@ export function behaviorContextGlobals() {
export function behaviorContextTypes() {
return {
Math: (Math) => ({
floor: {
type: 'number',
label: 'Floor $1.',
args: [
['operand', {
type: 'number',
}],
],
},
randomNumber: {
type: 'number',
label: 'Random number between $1 and $2.',
args: [
['min', {
type: 'number',
}],
['max', {
type: 'number',
}],
],
},
Vector: {
type: 'Vector',
},
children: {
floor: {
type: 'number',
label: 'Floor $1.',
args: [
['operand', {
type: 'number',
}],
],
},
randomNumber: {
type: 'number',
label: 'Random number between $1 and $2.',
args: [
['min', {
type: 'number',
}],
['max', {
type: 'number',
}],
],
},
Vector: {
type: 'Vector',
},
}
}),
};
}

View File

@ -1,35 +1,50 @@
export function behaviorContextTypes() {
return {
vector: {
defaultLiteral: [0, 0],
children: {
x: {
type: 'number',
label: 'X',
},
y: {
type: 'number',
label: 'Y',
},
},
},
Vector: (Math) => ({
floor: {
type: 'vector',
label: 'Floor $1.',
args: [
['vector', {
type: 'vector',
}],
],
},
fromDirection: {
type: 'vector',
label: '$1 as a movement vector.',
args: [
['direction', {
type: 'number',
}],
],
},
sub: {
type: 'vector',
label: 'Subtract $2 from $1.',
args: [
['l', {
type: 'vector',
}],
['r', {
type: 'vector',
}],
],
children: {
floor: {
type: 'vector',
label: 'Floor $1.',
args: [
['vector', {
type: 'vector',
}],
],
},
fromDirection: {
type: 'vector',
label: '$1 as a movement vector.',
args: [
['direction', {
type: 'number',
}],
],
},
sub: {
type: 'vector',
label: 'Subtract $2 from $1.',
args: [
['l', {
type: 'vector',
}],
['r', {
type: 'vector',
}],
],
},
},
}),
};