mirror of
https://gitee.com/sli97/behavior-eden-coco-plugin.git
synced 2024-12-26 03:39:12 +00:00
116 lines
4.2 KiB
JavaScript
116 lines
4.2 KiB
JavaScript
"use strict";
|
||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||
};
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.BehaviorTree = void 0;
|
||
const cc_1 = require("cc");
|
||
const BehaviorEditor_1 = require("./BehaviorEditor");
|
||
const BehaviorManager_1 = require("./BehaviorManager");
|
||
const BehaviorSource_1 = require("../core/BehaviorSource");
|
||
const { ccclass, property, requireComponent } = cc_1._decorator;
|
||
/***
|
||
* 用户真正使用的组件,用来设置单个行为树的运行参数
|
||
* 改成BehaviorTree名字,方便用户使用
|
||
*/
|
||
let BehaviorTree = class BehaviorTree extends cc_1.Component {
|
||
constructor() {
|
||
super(...arguments);
|
||
this.asset = null;
|
||
this.restartWhenComplete = false;
|
||
this.startWhenEnabled = true;
|
||
/***
|
||
* true:节点失活的时候,行为树会保留,仅暂停运行
|
||
* false:节点失活的时候,行为树直接删除
|
||
*/
|
||
this.pauseWhenDisabled = false;
|
||
this.logNodeChange = false;
|
||
// 是否被暂停运行了(未开始执行的行为树或者被删除的树都不属于暂停)
|
||
this.isPaused = false;
|
||
// 是否执行完start生成周期,防止start和onEnable重复执行enableBehavior
|
||
this.isInit = false;
|
||
this.behaviorSource = new BehaviorSource_1.BehaviorSource();
|
||
}
|
||
/***
|
||
* 仅enableBehavior和disableBehavior属于供用户在自定义脚本中管理单个行为树的方法
|
||
* 其他方法请勿使用
|
||
*/
|
||
enableBehavior() {
|
||
this.createBehaviorManager();
|
||
if (BehaviorManager_1.BehaviorManager.instance) {
|
||
BehaviorManager_1.BehaviorManager.instance.enableBehavior(this);
|
||
}
|
||
}
|
||
disableBehavior(pause = this.pauseWhenDisabled) {
|
||
if (BehaviorManager_1.BehaviorManager.instance) {
|
||
BehaviorManager_1.BehaviorManager.instance.disableBehavior(this, pause);
|
||
this.isPaused = pause;
|
||
}
|
||
}
|
||
parse() {
|
||
this.behaviorSource.parse(this.asset.json);
|
||
}
|
||
start() {
|
||
if (this.startWhenEnabled) {
|
||
this.enableBehavior();
|
||
}
|
||
this.isInit = true;
|
||
}
|
||
onEnable() {
|
||
if (!this.isInit) {
|
||
return;
|
||
}
|
||
// 运行被暂停或者startWhenEnabled的树
|
||
if (this.isPaused || this.startWhenEnabled) {
|
||
this.enableBehavior();
|
||
this.isPaused = false;
|
||
}
|
||
}
|
||
onDisable() {
|
||
this.disableBehavior();
|
||
}
|
||
createBehaviorManager() {
|
||
if (!BehaviorManager_1.BehaviorManager.instance) {
|
||
const node = new cc_1.Node("BehaviorManager");
|
||
BehaviorManager_1.BehaviorManager.instance = node.addComponent(BehaviorManager_1.BehaviorManager);
|
||
cc_1.director.getScene().addChild(node);
|
||
}
|
||
}
|
||
onBehaviorStarted() { }
|
||
/***
|
||
* 插件面板初始化时调用
|
||
*/
|
||
async getAssetUrl() {
|
||
if (!this.asset) {
|
||
return;
|
||
}
|
||
const uuid = this.asset._uuid;
|
||
// 获取当前json的url
|
||
const url = await Editor.Message.request("asset-db", "query-url", uuid);
|
||
return url;
|
||
}
|
||
};
|
||
__decorate([
|
||
property(cc_1.JsonAsset)
|
||
], BehaviorTree.prototype, "asset", void 0);
|
||
__decorate([
|
||
property(cc_1.CCBoolean)
|
||
], BehaviorTree.prototype, "restartWhenComplete", void 0);
|
||
__decorate([
|
||
property(cc_1.CCBoolean)
|
||
], BehaviorTree.prototype, "startWhenEnabled", void 0);
|
||
__decorate([
|
||
property(cc_1.CCBoolean)
|
||
], BehaviorTree.prototype, "pauseWhenDisabled", void 0);
|
||
__decorate([
|
||
property(cc_1.CCBoolean)
|
||
], BehaviorTree.prototype, "logNodeChange", void 0);
|
||
BehaviorTree = __decorate([
|
||
ccclass("BehaviorTree"),
|
||
requireComponent(BehaviorEditor_1.BehaviorEditor)
|
||
], BehaviorTree);
|
||
exports.BehaviorTree = BehaviorTree;
|