SubwayGenPuppet.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { Vec3, v3 } from "cc";
  2. import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
  3. import { MoveToComp } from "../../common/ecs/position/MoveTo";
  4. import { Subway } from "../Subway";
  5. import { SubwayModelComp } from "../model/SubwayModelComp";
  6. import { smc } from "../../common/SingletonModuleComp";
  7. import { SubwayEnum } from "../model/SubwayEnum";
  8. import { Puppet } from "../../puppet/puppet";
  9. import { PuppetModelComp } from "../../puppet/model/PuppetModelComp";
  10. import { PuppetAnimatorType } from "../../puppet/model/PuppetEnum";
  11. import { PathFindComp } from "../../common/ecs/path/PathFind";
  12. /** 检查左侧和右侧是否可以出人 */
  13. @ecs.register('SubwayGenPuppet')
  14. export class SubwayGenPuppetComp extends ecs.Comp {
  15. isGen:boolean = false
  16. reset() {
  17. this.isGen = false
  18. }
  19. }
  20. export class SubwayGenPuppetSystem extends ecs.ComblockSystem implements ecs.ISystemUpdate {
  21. filter(): ecs.IMatcher {
  22. return ecs.allOf(SubwayGenPuppetComp, SubwayModelComp);
  23. }
  24. // entityEnter(e: Subway): void {
  25. // e.remove(SubwayGenPuppetComp)
  26. // }
  27. update(e: Subway): void {
  28. const checkpoint = smc.initialize.account.checkpoint
  29. const genComp = e.get(SubwayGenPuppetComp)
  30. if(genComp.isGen||e.SubwayModel.colorArr.length===0)return
  31. if (checkpoint && checkpoint.CheckpointModel) {
  32. const cells = checkpoint.CheckpointModel.cells
  33. const grids = checkpoint.CheckpointModel.grids
  34. if (e.SubwayModel.type === SubwayEnum.LEFT) {
  35. if (e.SubwayModel.y === 0) {
  36. console.log('地铁靠边出不来的哦')
  37. return
  38. }
  39. const cell = cells[e.SubwayModel.x][e.SubwayModel.y-1]
  40. const grid = grids[e.SubwayModel.x][e.SubwayModel.y-1]
  41. if (grid && !cell) {
  42. genComp.isGen = true
  43. const pos = grid.GridView.node.position.clone()
  44. this.createPuppet(e, e.SubwayModel.x, e.SubwayModel.y-1, pos)
  45. }
  46. } else {
  47. if (e.SubwayModel.y === cells[0]?.length - 1) {
  48. console.log('地铁靠边出不来的哦')
  49. return
  50. }
  51. const cell = cells[e.SubwayModel.x][e.SubwayModel.y+1]
  52. const grid = grids[e.SubwayModel.x][e.SubwayModel.y+1]
  53. if (grid && !cell) {
  54. genComp.isGen = true
  55. const pos = grid.GridView.node.position.clone()
  56. this.createPuppet(e, e.SubwayModel.x, e.SubwayModel.y + 1, pos)
  57. }
  58. }
  59. }
  60. }
  61. createPuppet(e: Subway, x: number, y: number, end: Vec3) {
  62. const genComp = e.get(SubwayGenPuppetComp)
  63. if (!end|| !e.SubwayView.node) {
  64. genComp.isGen = false
  65. return
  66. }
  67. const account = smc.initialize.account
  68. const puppet = ecs.getEntity<Puppet>(Puppet)
  69. const puppetModel = puppet.get(PuppetModelComp)
  70. const color = e.SubwayModel.colorArr.shift()
  71. e.SubwayView.updateCount(e.SubwayModel.colorArr.length)
  72. puppetModel.color = color
  73. puppetModel.withColider = true
  74. puppet.load(account.checkpoint.CheckpointView.node, color, e.SubwayView.node.position.clone(), account.AccountModel.skin);
  75. e.addChild(puppet)
  76. if (e.SubwayModel.type === SubwayEnum.LEFT) {
  77. puppet.PuppetView.animator.toLeft()
  78. } else {
  79. puppet.PuppetView.animator.toRight()
  80. }
  81. puppet.PuppetView.animator.playAni(PuppetAnimatorType.Run)
  82. puppet.PuppetView.animator.scale()
  83. puppet.PuppetView.animator.onRunComplete = () => {
  84. puppet.PuppetView.animator.toStation()
  85. }
  86. puppet.PuppetView.animator.moveToTarget(end)
  87. if (e.SubwayModel.type === SubwayEnum.LEFT) {
  88. account.checkpoint.CheckpointModel.cells[e.SubwayModel.x][e.SubwayModel.y - 1] = puppet
  89. account.checkpoint.CheckpointModel.path_grid[e.SubwayModel.x][e.SubwayModel.y - 1] = {
  90. x: e.SubwayModel.x,
  91. y: e.SubwayModel.y - 1,
  92. fill: color,
  93. pos: end
  94. }
  95. puppetModel.x = e.SubwayModel.x
  96. puppetModel.y = e.SubwayModel.y-1
  97. const pathComp = puppet.add(PathFindComp)
  98. pathComp.x = e.SubwayModel.x
  99. pathComp.y = e.SubwayModel.y - 1
  100. } else {
  101. account.checkpoint.CheckpointModel.cells[e.SubwayModel.x][e.SubwayModel.y + 1] = puppet
  102. account.checkpoint.CheckpointModel.path_grid[e.SubwayModel.x][e.SubwayModel.y + 1] = {
  103. x: e.SubwayModel.x,
  104. y: e.SubwayModel.y + 1,
  105. fill: color,
  106. pos: end
  107. }
  108. puppetModel.x = e.SubwayModel.x
  109. puppetModel.y = e.SubwayModel.y+1
  110. const pathComp = puppet.add(PathFindComp)
  111. pathComp.x = e.SubwayModel.x
  112. pathComp.y = e.SubwayModel.y + 1
  113. }
  114. genComp.isGen = false
  115. }
  116. }