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() { export function behaviorContextTypes() {
return { return {
bool: {}, bool: {
defaultLiteral: true,
},
context: { context: {
children: {
add: { add: {
type: 'void', type: 'void',
label: 'Add $2 as $1.', label: 'Add $2 as $1.',
@ -17,9 +20,14 @@ export function behaviorContextTypes() {
], ],
}, },
}, },
number: {}, },
number: {
defaultLiteral: 0,
},
stream: {}, stream: {},
string: {}, string: {
defaultLiteral: '',
},
void: {}, void: {},
}; };
} }

View File

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

View File

@ -27,6 +27,7 @@ export function behaviorContextGlobals() {
export function behaviorContextTypes() { export function behaviorContextTypes() {
return { return {
Flow: { Flow: {
children: {
conditional: { conditional: {
type: 'bool', type: 'bool',
label: 'If $1 then run $2.', label: 'If $1 then run $2.',
@ -58,6 +59,7 @@ export function behaviorContextTypes() {
], ],
}, },
}, },
},
}; };
} }

View File

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

View File

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

View File

@ -5,14 +5,8 @@ export function behaviorContextTypes() {
const Traits = entity const Traits = entity
? Object.values(entity.allTraitInstances()).map((instance) => instance.constructor) ? Object.values(entity.allTraitInstances()).map((instance) => instance.constructor)
: allTraits(); : allTraits();
const traitTypes = Traits const core = {
.reduce((r, {behaviorContextTypes, describeState}) => ({ children: {
...r,
...behaviorContextTypes(),
...describeState(),
}), {});
return {
...traitTypes,
invokeHook: { invokeHook: {
type: 'object', type: 'object',
label: 'Invoke hook.', 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() { export function behaviorContextTypes() {
return { return {
Math: (Math) => ({ Math: (Math) => ({
children: {
floor: { floor: {
type: 'number', type: 'number',
label: 'Floor $1.', label: 'Floor $1.',
@ -33,6 +34,7 @@ export function behaviorContextTypes() {
Vector: { Vector: {
type: 'Vector', type: 'Vector',
}, },
}
}), }),
}; };
} }

View File

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