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,8 +2,11 @@ import {Context} from './context';
export function behaviorContextTypes() {
return {
bool: {},
bool: {
defaultLiteral: true,
},
context: {
children: {
add: {
type: 'void',
label: 'Add $2 as $1.',
@ -17,9 +20,14 @@ export function behaviorContextTypes() {
],
},
},
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,6 +27,7 @@ export function behaviorContextGlobals() {
export function behaviorContextTypes() {
return {
Flow: {
children: {
conditional: {
type: 'bool',
label: 'If $1 then run $2.',
@ -58,6 +59,7 @@ export function behaviorContextTypes() {
],
},
},
},
};
}

View File

@ -19,6 +19,7 @@ class Timing {
export function behaviorContextTypes() {
return {
Timing: {
children: {
wait: {
type: 'void',
label: 'Wait for $1 seconds.',
@ -29,6 +30,7 @@ export function behaviorContextTypes() {
],
},
},
},
};
}

View File

@ -35,6 +35,7 @@ class Utility {
export function behaviorContextTypes() {
return {
Utility: {
children: {
makeArray: {
type: 'array',
label: 'Make array.',
@ -56,6 +57,7 @@ export function behaviorContextTypes() {
args: [],
},
},
},
};
}

View File

@ -5,14 +5,8 @@ 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,
const core = {
children: {
invokeHook: {
type: 'object',
label: 'Invoke hook.',
@ -22,7 +16,12 @@ export function behaviorContextTypes() {
}],
],
},
}
};
return Traits
.reduce((r, T) => ({
...r, children: {...r.children, ...T.behaviorContextTypes(), ...T.describeState()},
}), core);
},
};
}

View File

@ -9,6 +9,7 @@ export function behaviorContextGlobals() {
export function behaviorContextTypes() {
return {
Math: (Math) => ({
children: {
floor: {
type: 'number',
label: 'Floor $1.',
@ -33,6 +34,7 @@ export function behaviorContextTypes() {
Vector: {
type: 'Vector',
},
}
}),
};
}

View File

@ -1,6 +1,20 @@
export function behaviorContextTypes() {
return {
vector: {
defaultLiteral: [0, 0],
children: {
x: {
type: 'number',
label: 'X',
},
y: {
type: 'number',
label: 'Y',
},
},
},
Vector: (Math) => ({
children: {
floor: {
type: 'vector',
label: 'Floor $1.',
@ -31,6 +45,7 @@ export function behaviorContextTypes() {
}],
],
},
},
}),
};
}