NodeUtils.ts 972 B

12345678910111213141516171819202122232425262728293031323334
  1. import { Vec3,Node } from "cc";
  2. import { GameMgr } from "../../module_basic/scripts/GameMgr";
  3. const tempV3 = new Vec3();
  4. export type ExtraNode = Node & {
  5. __radius__: number;
  6. }
  7. export class NodeUtils{
  8. public static clampInMapBoundary(node: ExtraNode) {
  9. if(!node || !GameMgr.inst.gameData){
  10. return;
  11. }
  12. let radius = node.__radius__;
  13. node.getWorldPosition(tempV3);
  14. const halfWidth = GameMgr.inst.gameData.mapWidth / 2 - radius;
  15. const halfHeight = GameMgr.inst.gameData.mapHeight / 2 - radius;
  16. node.getWorldPosition(tempV3);
  17. if (tempV3.x < -halfWidth) {
  18. tempV3.x = -halfWidth;
  19. }
  20. if (tempV3.x > halfWidth) {
  21. tempV3.x = halfWidth;
  22. }
  23. if (tempV3.y < -halfHeight) {
  24. tempV3.y = -halfHeight;
  25. }
  26. if (tempV3.y > halfHeight) {
  27. tempV3.y = halfHeight;
  28. }
  29. node.setWorldPosition(tempV3);
  30. }
  31. }