在Yuka.js库中,Active表示目标正在主动地采取措施来实现其目标。当目标处于主动模式时,它的行为将受到多个输入因素的影响,并受到指示来完成特定的动作。
active()方法是在目标处于主动模式时调用的方法。它通常与update()方法一起使用,update()方法负责执行目标的动作,而active()方法则确定何时执行这些动作。当目标处于被动模式时,active()方法不执行任何操作。
target.active();
当目标在建筑游戏中被玩家选中时,它会变得主动,它会打开建筑面板并允许玩家更改该建筑的属性。当玩家完成新属性的设置后,建筑将变得被动。
class Building extends YUKA.GameEntity {
constructor() {
super();
this.active = new YUKA.ActiveComponent(this);
this.active.threshold = 1.5; // init the threshold for activation
this.stateMachine = new YUKA.StateMachine(this);
this.add(buildState);
this.add(staticState);
this.add(activeState);
this.stateMachine.changeToState('build');
}
}
const buildState = new YUKA.State('build');
buildState.update = function() {
// the build state does nothing
};
buildState.onEnter = function() {
this.entity.active.setThreshold(1.5);
};
const staticState = new YUKA.State('static');
staticState.update = function() {
// the static state does nothing
};
staticState.onEnter = function() {
this.entity.active.setThreshold(10);
};
const activeState = new YUKA.State('active');
activeState.update = function() {
// the active state does nothing
};
const building = new Building();
YUKA.update(0.1);
building.active.threshold; // --> 1.5
building.stateMachine.changeToState('static');
YUKA.update(0.1);
building.active.threshold; // --> 10
building.stateMachine.changeToState('active');
YUKA.update(0.1);
building.active.threshold; // --> 1.5