PuppetViewController.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { Component, _decorator, Node, EventTouch, systemEvent, input, Input, geometry, Camera, PhysicsSystem, director, v3, Vec3 } from "cc";
  2. import { Puppet } from "../puppet";
  3. import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
  4. import { MoveToPathComp } from "../../common/ecs/path/MoveToPath";
  5. import { PathFindComp } from "../../common/ecs/path/PathFind";
  6. import { smc } from "../../common/SingletonModuleComp";
  7. import { CheckpointCheckFailComp } from "../../checkpoint/bll/CheckpointCheck";
  8. import { VibrationManager } from "../../../platform/vibration/VibrationManager";
  9. import { UIID } from "../../common/config/GameUIConfig";
  10. import { CheckpointPathTriggerComp } from "../../checkpoint/bll/CheckpointPathTrigger";
  11. const { ccclass, property } = _decorator;
  12. /** 角色资源加载 */
  13. @ccclass('PuppetViewController')
  14. export class PuppetViewController extends Component {
  15. /** 角色对象 */
  16. puppet: Puppet = null!;
  17. @property({ type: Camera, tooltip: '主相机' })
  18. public mainCamera: Camera | null = null;
  19. // @property({ type: Node, tooltip: '待触摸物体' })
  20. // public node_touch_1: Node | null = null;
  21. private _ray: geometry.Ray = new geometry.Ray();
  22. onLoad() {
  23. // const cameras = director.getScene().getComponentsInChildren(Camera);
  24. // this.mainCamera = cameras.find(camera => camera.node.name === 'MainCamera');
  25. // input.on(Input.EventType.TOUCH_END, this.onTouchStart, this);
  26. }
  27. onDestory() {
  28. // input.off(Input.EventType.TOUCH_END, this.onTouchStart, this);
  29. }
  30. onTouchStart(event: EventTouch) {
  31. // 基于摄像机 画射线
  32. this.mainCamera?.screenPointToRay(event.getLocation().x, event.getLocation().y, this._ray);
  33. // 基于物理碰撞器的射线检测
  34. // 当点击 node_touch_1 时,控制台打印 “node_touch_1”
  35. // console.log('this.puppet.PuppetModel.x:',this.puppet.PuppetModel.x)
  36. // console.log('this.puppet.PuppetModel.y',this.puppet.PuppetModel.y)
  37. if (PhysicsSystem.instance.raycast(this._ray)) {
  38. const r = PhysicsSystem.instance.raycastResults;
  39. for (let index = 0; index < r.length; index++) {
  40. const element = r[0];
  41. console.log(this.node?.uuid)
  42. if (element.collider.node.uuid == this.node?.uuid) {
  43. oops.audio.playEffect("common/audio/click");
  44. console.log('当前点击: ' + element.collider.node.uuid);
  45. console.log('是否可达:', this.puppet.PathFind?.canReach)
  46. if (this.puppet?.PathFind?.canReach && !this.puppet.get(MoveToPathComp)) {
  47. VibrationManager.getInstance().vibrateShort()
  48. const checkpoint = smc.initialize.account.checkpoint
  49. let end = v3(0, 0, 0)
  50. if (checkpoint.CheckpointModel.curVehicle.VehicleModel.color === this.puppet.PuppetModel.color && !checkpoint.CheckpointModel.curVehicle.VehicleModel.isFull) {
  51. // end = v3(0, 0, 0)
  52. this.puppet.PuppetView.animator.onRunPathComplete = () => {
  53. checkpoint.CheckpointModel.curVehicle.VehicleView.createPuppet()
  54. checkpoint.removeChild(this.puppet)
  55. this.puppet.destroy()
  56. checkpoint.CheckpointModel.peopleCount-=1
  57. }
  58. checkpoint.CheckpointModel.curVehicle.VehicleModel.useSit += 1
  59. } else if (checkpoint.CheckpointModel.emptyStation) {
  60. if (checkpoint.CheckpointModel.emptyStationCount <= 1) {
  61. console.log('游戏结束')
  62. oops.gui.open(UIID.Recovery)
  63. // checkpoint.add(CheckpointCheckFailComp)
  64. }
  65. end = checkpoint.CheckpointModel.emptyStation.StationView.node.position.clone()
  66. checkpoint.CheckpointModel.emptyStation.StationModel.puppet = this.puppet
  67. } else {
  68. console.log('游戏结束')
  69. return
  70. }
  71. this.puppet.PuppetView.animator.moveToPath(end)
  72. // const moveToPath = this.puppet.add(MoveToPathComp)
  73. // moveToPath.speed = 2
  74. // moveToPath.paths = this.puppet.PathFind.path.concat([end])
  75. // moveToPath.node = this.node
  76. // moveToPath.ns = Node.NodeSpace.WORLD
  77. // const forwardClone = this.puppet.PuppetView.node.forward.clone()
  78. // moveToPath.onComplete = () => {
  79. // console.log('移动完成')
  80. // this.puppet.PuppetView.ske.play('idle')
  81. // this.puppet.PuppetView.node.forward = forwardClone
  82. // needDestory&&this.puppet.destroy()
  83. // }
  84. // this.puppet.PuppetView.ske.play('run')
  85. // checkpoint.CheckpointModel.emptyStation
  86. const pathGrid = checkpoint.CheckpointModel.path_grid
  87. pathGrid[this.puppet.PuppetModel.x][this.puppet.PuppetModel.y].fill = null
  88. const cell = checkpoint.CheckpointModel.cells
  89. cell[this.puppet.PuppetModel.x][this.puppet.PuppetModel.y] = null
  90. this.puppet.remove(PathFindComp)
  91. // console.log('我开始动了')
  92. oops.audio.playEffect("common/audio/move");
  93. }else{
  94. VibrationManager.getInstance().vibrateLong()
  95. }
  96. }
  97. break
  98. }
  99. }
  100. }
  101. onPuppetClick(){
  102. oops.audio.playEffect("common/audio/click");
  103. // console.log('当前点击: ' + element.collider.node.uuid);
  104. // console.log('是否可达:', this.puppet.PathFind?.canReach)
  105. if (this.puppet?.PathFind?.canReach && !this.puppet.get(MoveToPathComp)) {
  106. VibrationManager.getInstance().vibrateShort()
  107. const checkpoint = smc.initialize.account.checkpoint
  108. let end = v3(0, 0, 0)
  109. if (checkpoint.CheckpointModel.curVehicle.VehicleModel.color === this.puppet.PuppetModel.color && !checkpoint.CheckpointModel.curVehicle.VehicleModel.isFull) {
  110. // end = v3(0, 0, 0)
  111. this.puppet.PuppetView.animator.onRunPathComplete = () => {
  112. checkpoint.CheckpointModel.curVehicle.VehicleView.createPuppet()
  113. checkpoint.removeChild(this.puppet)
  114. this.puppet.destroy()
  115. checkpoint.CheckpointModel.peopleCount-=1
  116. }
  117. checkpoint.CheckpointModel.curVehicle.VehicleModel.useSit += 1
  118. } else if (checkpoint.CheckpointModel.emptyStation) {
  119. if (checkpoint.CheckpointModel.emptyStationCount <= 1) {
  120. console.log('游戏结束')
  121. oops.gui.open(UIID.Recovery)
  122. // checkpoint.add(CheckpointCheckFailComp)
  123. }
  124. end = checkpoint.CheckpointModel.emptyStation.StationView.node.position.clone()
  125. checkpoint.CheckpointModel.emptyStation.StationModel.puppet = this.puppet
  126. } else {
  127. console.log('游戏结束')
  128. return
  129. }
  130. this.puppet.PuppetView.animator.moveToPath(end)
  131. // const moveToPath = this.puppet.add(MoveToPathComp)
  132. // moveToPath.speed = 2
  133. // moveToPath.paths = this.puppet.PathFind.path.concat([end])
  134. // moveToPath.node = this.node
  135. // moveToPath.ns = Node.NodeSpace.WORLD
  136. // const forwardClone = this.puppet.PuppetView.node.forward.clone()
  137. // moveToPath.onComplete = () => {
  138. // console.log('移动完成')
  139. // this.puppet.PuppetView.ske.play('idle')
  140. // this.puppet.PuppetView.node.forward = forwardClone
  141. // needDestory&&this.puppet.destroy()
  142. // }
  143. // this.puppet.PuppetView.ske.play('run')
  144. // checkpoint.CheckpointModel.emptyStation
  145. const pathGrid = checkpoint.CheckpointModel.path_grid
  146. pathGrid[this.puppet.PuppetModel.x][this.puppet.PuppetModel.y].fill = null
  147. const cell = checkpoint.CheckpointModel.cells
  148. cell[this.puppet.PuppetModel.x][this.puppet.PuppetModel.y] = null
  149. this.puppet.remove(PathFindComp)
  150. checkpoint.add(CheckpointPathTriggerComp)
  151. // console.log('我开始动了')
  152. oops.audio.playEffect("common/audio/move");
  153. }else{
  154. VibrationManager.getInstance().vibrateLong()
  155. }
  156. }
  157. }