CheckpointViewController.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { Component, _decorator, Node, EventTouch, systemEvent, input, Input, geometry, Camera, PhysicsSystem, director, v3, Vec3 } from "cc";
  2. import { Checkpoint } from "../Checkpoint";
  3. import { PuppetViewComp } from "../../puppet/view/PuppetViewComp";
  4. const { ccclass, property } = _decorator;
  5. /** 关卡点击控制 */
  6. @ccclass('CheckpointViewController')
  7. export class CheckpointViewController extends Component {
  8. /** 角色对象 */
  9. checkpoint: Checkpoint = null!;
  10. @property({ type: Camera, tooltip: '主相机' })
  11. public mainCamera: Camera | null = null;
  12. // @property({ type: Node, tooltip: '待触摸物体' })
  13. // public node_touch_1: Node | null = null;
  14. private _ray: geometry.Ray = new geometry.Ray();
  15. onLoad() {
  16. const cameras = director.getScene().getComponentsInChildren(Camera);
  17. this.mainCamera = cameras.find(camera => camera.node.name === 'MainCamera');
  18. input.on(Input.EventType.TOUCH_END, this.onTouchStart, this);
  19. }
  20. onDestory() {
  21. input.off(Input.EventType.TOUCH_END, this.onTouchStart, this);
  22. }
  23. onTouchStart(event: EventTouch) {
  24. // 基于摄像机 画射线
  25. this.mainCamera?.screenPointToRay(event.getLocation().x, event.getLocation().y, this._ray);
  26. if (PhysicsSystem.instance.raycast(this._ray)) {
  27. const r = PhysicsSystem.instance.raycastResults;
  28. for (let index = 0; index < r.length; index++) {
  29. const element = r[index];
  30. // console.log(this.node?.uuid)
  31. const view = element.collider.node?.getComponent(PuppetViewComp) as PuppetViewComp
  32. if (view) {
  33. view.controller.onPuppetClick()
  34. }
  35. break
  36. }
  37. }
  38. }
  39. }