PuppetViewController.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { Component, _decorator, Node, EventTouch, systemEvent, input, Input, geometry, Camera, PhysicsSystem, director, v3 } 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. const { ccclass, property } = _decorator;
  8. /** 角色资源加载 */
  9. @ccclass('PuppetViewController')
  10. export class PuppetViewController extends Component {
  11. /** 角色对象 */
  12. puppet: Puppet = 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 _ray: geometry.Ray = new geometry.Ray();
  18. onLoad() {
  19. const cameras = director.getScene().getComponentsInChildren(Camera);
  20. this.mainCamera = cameras.find(camera => camera.node.name === 'MainCamera');
  21. input.on(Input.EventType.TOUCH_END, this.onTouchStart, this);
  22. }
  23. onDestory() {
  24. input.off(Input.EventType.TOUCH_END, this.onTouchStart, this);
  25. }
  26. onTouchStart(event: EventTouch) {
  27. // 基于摄像机 画射线
  28. this.mainCamera?.screenPointToRay(event.getLocation().x, event.getLocation().y, this._ray);
  29. // 基于物理碰撞器的射线检测
  30. // 当点击 node_touch_1 时,控制台打印 “node_touch_1”
  31. if (PhysicsSystem.instance.raycast(this._ray)) {
  32. const r = PhysicsSystem.instance.raycastResults;
  33. for (let index = 0; index < r.length; index++) {
  34. const element = r[index];
  35. if (element.collider.node.uuid == this.node?.uuid) {
  36. console.log('当前点击: ' + element.collider.node.uuid);
  37. console.log('是否可达:', this.puppet.PathFind?.canReach)
  38. if (this.puppet.PathFind?.canReach&& !this.puppet.get(MoveToPathComp)) {
  39. const moveToPath = this.puppet.add(MoveToPathComp)
  40. moveToPath.speed = 1
  41. moveToPath.paths = this.puppet.PathFind.path.concat([v3(-1, 0, -3)])
  42. moveToPath.node = this.node
  43. moveToPath.ns = Node.NodeSpace.WORLD
  44. moveToPath.onComplete = () => {
  45. console.log('移动完成')
  46. }
  47. const pathGrid = smc.initialize.account.checkpoint.CheckpointModel.path_grid
  48. pathGrid[this.puppet.PuppetModel.x][this.puppet.PuppetModel.y].fill = null
  49. this.puppet.remove(PathFindComp)
  50. console.log('我开始动了')
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }