import {TickingPromise} from '@avocado/core'; import {register as registerType} from './types/registry'; import {Traversal} from '../item/traversal'; export class Globals { multiply(x, y) { return x * y; } sub(x, y) { return x - y; } conditional(condition, actions, context) { if (condition.get(context)) { return this.serial(actions, context); } } makeArray(...args) { return args; } log(any) { console.log(any); } parallel(actions, context) { return actions.parallel(context); } randomNumber(min, max, floor = true) { let mag = Math.random() * ((max - min) + 1); return min + (floor ? Math.floor(mag) : mag); } serial(actions, context) { return actions.serial(context); } testing() { return null; } wait(duration) { let resolve; const promise = new TickingPromise(_resolve => resolve = _resolve); promise.ticker = (elapsed) => { duration -= elapsed; if (duration <= 0) { resolve(); } } return promise; } } // Globals. registerType('globals', { label: 'Globals', children: { log: { type: { key: 'function', args: [ { type: 'any', label: 'Thing to log', }, ], return: { type: 'void', }, }, label: 'Log to console', }, randomNumber: { type: { key: 'function', args: [ { type: { key: 'number', }, label: 'Minimum value', advanced: true, }, { type: 'number', label: 'Maximum value', }, { type: 'boolean', label: 'Convert to an integer?', default: true, }, ], return: { type: 'number', }, }, label: 'Random number', }, test: { type: { key: 'function', args: [ { type: 'number', label: 'Milliseconds to wait', }, ], return: { type: { key: 'entity', traits: ['mobile'], }, }, }, label: 'Entityyyyy', }, waitMs: { type: { key: 'function', args: [ { type: 'number', label: 'Milliseconds to wait', }, ], return: { type: 'void', }, }, label: 'Wait for a specified time', }, }, });