@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
132 lines (104 loc) • 3.31 kB
JavaScript
import { assert } from "../../../../core/assert.js";
import { compileReactiveExpression } from "../../../../core/lang/reactive/compileReactiveExpression.js";
import {
inferReactiveExpressionTypes
} from "../../../../core/model/reactive/transform/inferReactiveExpressionTypes.js";
import DataType from "../../../../core/parser/simple/DataType.js";
import { UUID } from "../../guid/UUID.js";
import { deserializeActionFromJSON } from "../actions/definition/deserializeActionFromJSON.js";
import { DynamicRuleCooldownDescription } from "./DynamicRuleCooldownDescription.js";
export class DynamicRuleDescription {
/**
*
* @type {string}
*/
id = UUID.string();
/**
*
* @type {ReactiveExpression}
*/
condition = null;
/**
*
* @type {AbstractActionDescription}
*/
action = null;
/**
*
* @type {ReactiveReference[]}
*/
references = [];
/**
* Rules with higher priority have the right to interrupt lower priority rules
* @type {number}
*/
priority = 0;
/**
* @private
* @type {number}
*/
predicate_complexity = 0;
/**
* Specified which global cooldowns will be triggered and for how long
* @type {DynamicRuleCooldownDescription[]}
*/
cooldowns_global = [];
/**
*
* @param {DynamicRuleDescription} other
* @returns {boolean}
*/
equals(other) {
return this.id.equals(other.id);
}
/**
*
* @returns {number}
*/
getPredicateComplexity() {
return this.predicate_complexity;
}
__increment_predicate_complexity() {
this.predicate_complexity++;
}
build() {
//infer types
inferReactiveExpressionTypes(this.condition);
if (this.condition.dataType === DataType.Any) {
//enforce top level type
this.condition.dataType = DataType.Boolean;
}
/**
*
* @type {ReactiveReference[]}
*/
this.references.splice(0, this.references.length);
const included = {};
this.condition.traverse((node) => {
if (node.isReference && included[node.name] === undefined) {
this.references.push(node);
included[node.name] = true;
}
});
this.condition.traverse(this.__increment_predicate_complexity, this);
}
fromJSON({
id = UUID.string(),
condition,
action,
global_cooldowns = [],
priority = 0
}) {
assert.isString(condition, 'condition');
assert.defined(action, 'action');
assert.notNull(action, 'action');
assert.isNumber(priority, 'priority');
assert.isArray(global_cooldowns, 'global_cooldowns');
this.condition = compileReactiveExpression(condition);
this.action = deserializeActionFromJSON(action);
this.id = id;
this.priority = priority;
this.cooldowns_global = global_cooldowns.map(DynamicRuleCooldownDescription.fromJSON);
this.build();
}
}