PuppetViewController.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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[index];
  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(-1, 0, -3)
  49. if (checkpoint.CheckpointModel.curVehicle.VehicleModel.color === this.puppet.PuppetModel.color && !checkpoint.CheckpointModel.curVehicle.VehicleModel.isFull) {
  50. end = v3(0, 0, -5)
  51. this.puppet.PuppetView.animator.onRunPathComplete = () => {
  52. const cell = checkpoint.CheckpointModel.cells
  53. cell[this.puppet.PuppetModel.x][this.puppet.PuppetModel.y] = null
  54. checkpoint.removeChild(this.puppet)
  55. this.puppet.destroy()
  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. this.puppet.remove(PathFindComp)
  88. // console.log('我开始动了')
  89. oops.audio.playEffect("common/audio/move");
  90. }else{
  91. VibrationManager.getInstance().vibrateLong()
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }