PuppetViewController.ts 8.9 KB

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