CheckpointViewController.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { Component, _decorator, Node, EventTouch, systemEvent, input, Input, geometry, Camera, PhysicsSystem, director, v3, Vec3, Color, Line, GradientRange, CurveRange } from "cc";
  2. import { Checkpoint } from "../Checkpoint";
  3. import { PuppetViewComp } from "../../puppet/view/PuppetViewComp";
  4. import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
  5. import { GameEvent } from "../../common/config/GameEvent";
  6. import { Puppet } from "../../puppet/puppet";
  7. const { ccclass, property } = _decorator;
  8. /** 关卡点击控制 */
  9. @ccclass('CheckpointViewController')
  10. export class CheckpointViewController extends Component {
  11. /** 角色对象 */
  12. checkpoint: Checkpoint = null!;
  13. @property({ type: Camera, tooltip: '主相机' })
  14. public mainCamera: Camera | null = null;
  15. // @property({ type: Node, tooltip: '待触摸物体' })
  16. // public node_touch_1: Node | null = null;
  17. private line: Line = null;
  18. private _ray: geometry.Ray = new geometry.Ray();
  19. onLoad() {
  20. const cameras = director.getScene().getComponentsInChildren(Camera);
  21. this.mainCamera = cameras.find(camera => camera.node.name === 'MainCamera');
  22. input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
  23. // this.createLine();
  24. }
  25. onDestory() {
  26. input.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
  27. }
  28. createLine() {
  29. const lineNode = new Node('Line');
  30. lineNode.setParent(this.node);
  31. this.line = lineNode.addComponent(Line);
  32. const grad = new GradientRange()
  33. grad.color = Color.RED;
  34. this.line.color = grad;
  35. }
  36. drawRay(ray: geometry.Ray) {
  37. const rayStart = ray.o;
  38. const rayEnd = new Vec3();
  39. Vec3.multiplyScalar(rayEnd, ray.d, 1000); // 假设射线长度为1000
  40. Vec3.add(rayEnd, rayStart, rayEnd);
  41. this.line.worldSpace = true;
  42. const curve = new CurveRange()
  43. curve.mode = CurveRange.Mode.Constant
  44. curve.constant = 0.1
  45. this.line.width = curve;
  46. this.line.positions = [rayStart, rayEnd];
  47. }
  48. onTouchStart(event: EventTouch) {
  49. // 基于摄像机 画射线
  50. this.mainCamera?.screenPointToRay(event.getLocation().x, event.getLocation().y, this._ray);
  51. // this.drawRay(this._ray)
  52. if (PhysicsSystem.instance.raycast(this._ray)) {
  53. const r = PhysicsSystem.instance.raycastResults;
  54. if (r.length > 0) {
  55. let element = r.reduce((min, current) => {
  56. return !Number.isNaN(current.distance) && current.distance < min.distance ? current : min;
  57. }, r[0]);
  58. // console.log(this.node?.uuid)
  59. const view = element.collider.node?.getComponent(PuppetViewComp) as PuppetViewComp
  60. if (view) {
  61. view.controller.onPuppetClick()
  62. const puppet = view.ent as Puppet
  63. if (puppet?.PuppetModel) {
  64. oops.message.dispatchEvent(GameEvent.PuppetClick, [puppet.PuppetModel.x, puppet.PuppetModel.y])
  65. }
  66. }
  67. }
  68. // for (let index = 0; index < r.length; index++) {
  69. // const element = r[index];
  70. // if(Number.isNaN(element.distance)){
  71. // continue
  72. // }
  73. // break
  74. // }
  75. }
  76. }
  77. }