SysLifeTime.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 {ComLifeTime} from '../components/ComLifeTime'
  7. import {ComRole} from '../components/ComRole'
  8. import {ComBehaviorTree} from '../components/ComBehaviorTree'
  9. const FILTER_LIFE_TIME = GenFilterKey([ComLifeTime, ComRole, ComBehaviorTree], [ComDie])
  10. export class SysLifeTime extends ECSSystem {
  11. /** 连接 */
  12. public onAdd(world: FightWorld): void {}
  13. /** 断开连接 */
  14. public onRemove(world: FightWorld): void {}
  15. /** 添加实体 */
  16. public onEntityEnter(world: FightWorld, entity: number): void {}
  17. /** */
  18. public onEntityLeave(world: FightWorld, entity: number): void {}
  19. /** 更新 */
  20. public onUpdate(world: FightWorld, dt: number): void {
  21. let filter = world.getFilter(FILTER_LIFE_TIME)
  22. filter.walk((entity: number) => {
  23. let comLifeTime = world.getComponent(entity, ComLifeTime)
  24. if (!comLifeTime.dirty) return
  25. comLifeTime.countDown -= dt
  26. if (comLifeTime.countDown <= 0) {
  27. let comRole = world.getComponent(entity, ComRole)
  28. comLifeTime.dirty = false
  29. comRole.nowHP = 0
  30. comRole.shieldHP = 0
  31. comRole.HPDirty = true
  32. }
  33. return false
  34. })
  35. }
  36. }