SysEnter.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /** @format */
  2. import {ECSSystem} from '../lib/ECSSystem'
  3. import {GenFilterKey} from '../lib/ECSComponent'
  4. import {FightWorld} from '../worlds/FightWorld'
  5. import {ComDie} from '../components/ComDie'
  6. import {ComNodeConfig} from '../components/ComNodeConfig'
  7. import {ComCocosNode} from '../components/ComCocosNode'
  8. import {ComTransform} from '../components/ComTransform'
  9. import {ComRole} from '../components/ComRole'
  10. import {ComRoleSpine} from '../components/ComRoleSpine'
  11. import {ComEnter} from '../components/ComEnter'
  12. import {ComBehaviorTree} from '../components/ComBehaviorTree'
  13. import {BT} from '../../behaviorTree/BehaviorTree'
  14. import {EventEnter} from '../core/NodeEvent'
  15. const FILTER_ENTER = GenFilterKey([ComCocosNode, ComEnter, ComRole])
  16. export class SysEnter extends ECSSystem {
  17. /** 连接 */
  18. public onAdd(world: FightWorld): void {}
  19. /** 断开连接 */
  20. public onRemove(world: FightWorld): void {}
  21. /** 添加实体 */
  22. public onEntityEnter(world: FightWorld, entity: number): void {}
  23. /** */
  24. public onEntityLeave(world: FightWorld, entity: number): void {}
  25. /** 更新 */
  26. public onUpdate(world: FightWorld, dt: number): void {
  27. let filter = world.getFilter(FILTER_ENTER)
  28. filter.walk((entity: number) => {
  29. let comEnter = world.getComponent(entity, ComEnter)
  30. let comCocosNode = world.getComponent(entity, ComCocosNode)
  31. if (!comCocosNode.loaded) return
  32. let comRole = world.getComponent(entity, ComRole)
  33. comEnter.countDown -= dt
  34. if (comEnter.countDown <= 0) {
  35. world.removeComponent(entity, ComEnter)
  36. // 添加behavior tree
  37. let comBehavior = world.addComponent(entity, ComBehaviorTree)
  38. comBehavior.bb = new BT.BlackBoard()
  39. world.initRoleBehavior(comBehavior, comRole.roleCfg)
  40. comCocosNode.events.push(new EventEnter())
  41. }
  42. return false
  43. })
  44. }
  45. }