DynamicPlan.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import BasicObject from "../../../Basic/BasicObject";
  2. import { _IBehaviorTreeConfig } from "../../../Data/CommonDataType";
  3. /**
  4. * 动态规划 - 逻辑层
  5. */
  6. export default class DynamicPlan extends BasicObject {
  7. /**动态规划配置 */
  8. protected readonly dynamicPlanConfig: _IBehaviorTreeConfig;
  9. /**当前规划列表 */
  10. protected _currentDynamicPlans: Array<any>;
  11. constructor (unitID: any, dynamicPlanConfig: _IBehaviorTreeConfig) {
  12. super();
  13. this._id = unitID;
  14. this.dynamicPlanConfig = dynamicPlanConfig;
  15. this._currentDynamicPlans = new Array<any>();
  16. }
  17. /**获得当前规划列表 */
  18. public get currentDynamicPlans (): Array<any> {
  19. return this._currentDynamicPlans;
  20. }
  21. /**是否可以动态规划 */
  22. public isDynamicPlan (type: any): boolean {
  23. let priority: number = this.dynamicPlanConfig.priority[type];
  24. // 判断优先级
  25. for (let index in this._currentDynamicPlans) {
  26. if (this.dynamicPlanConfig.priority[index] > priority) {
  27. return false;
  28. }
  29. }
  30. return true;
  31. }
  32. /**添加动态规划 */
  33. public addDynamicPlan (type: any): void {
  34. this.removeMutexDynamicPlan(type);
  35. this._currentDynamicPlans.push(type);
  36. }
  37. /**删除动态规划*/
  38. public removeDynamicPlan (type: any): void {
  39. if (this._currentDynamicPlans.includes(type) == true) {
  40. this._currentDynamicPlans.splice(this._currentDynamicPlans.indexOf(type), 1);
  41. }
  42. }
  43. /**删除互斥动态规划 */
  44. private removeMutexDynamicPlan (type: any): void {
  45. // 判断兼容或排斥
  46. let mutexChild: Array<string> = this.dynamicPlanConfig.mutexChild[type];
  47. let compatibilityChild: Array<string> = this.dynamicPlanConfig.compatibilityChild[type];
  48. // 若两者为空,排斥其他所有子节点
  49. if (mutexChild == null && compatibilityChild == null) {
  50. this._currentDynamicPlans = [];
  51. }
  52. else if (mutexChild != null) {
  53. for (let index: number = 0; index < this._currentDynamicPlans.length; index++) {
  54. // 若是互斥行为
  55. if (this._currentDynamicPlans[index] != type && mutexChild.includes(this._currentDynamicPlans[index]) == true) {
  56. this._currentDynamicPlans.splice(index, 1);
  57. index--;
  58. }
  59. }
  60. }
  61. else if (compatibilityChild != null) {
  62. for (let index: number = 0; index < this._currentDynamicPlans.length; index++) {
  63. // 若不是兼容行为
  64. if (this._currentDynamicPlans[index] != type && compatibilityChild.includes(this._currentDynamicPlans[index]) == false) {
  65. this._currentDynamicPlans.splice(index, 1);
  66. index--;
  67. }
  68. }
  69. }
  70. }
  71. }