GameNode.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. import { _decorator,Node, geometry, Camera, screen, EventTouch, Prefab, instantiate, v3, tween, RigidBody, MeshRenderer, Vec3, find, input, Input, Tween, PhysicsSystem, view } from 'cc';
  2. import { TileItem } from './TileItem';
  3. import { Main } from './Main';
  4. import { ResUtil } from '../core/utils/ResUtil';
  5. import { ComponentEx } from '../core/component/ComponentEx';
  6. import { audioMgr } from '../core/manager/AudioManager';
  7. import BusyLoadingManager, { BUSY_TYPE } from '../core/manager/BusyLoadingManager';
  8. import Data from '../core/manager/Data';
  9. import WindowManager from '../core/manager/WindowManager';
  10. import MsgHints from '../core/utils/MsgHints';
  11. import Utils from '../core/utils/Utils';
  12. import { DrawStarLayer } from '../gameui/DrawStarLayer';
  13. import { eventEmitter } from '../core/event/EventEmitter';
  14. import platformSystem from '../platform/platformSystem';
  15. import { GameConst, ITEM_TYPE } from '../core/common/GameConst';
  16. import { levelsData } from '../user/LevelsData';
  17. const { Ray } = geometry;
  18. const { ccclass, property, executeInEditMode } = _decorator;
  19. @ccclass('GameNode')
  20. export class GameNode extends ComponentEx {
  21. public lock: boolean = false;
  22. public collectpos: Vec3[] = [];
  23. public collectTiles: Node[] = [];
  24. public allTiles: Node[] = [];
  25. public optionTiles: Node[] = [];
  26. public start() {
  27. setTimeout(() => {
  28. this.initWall();
  29. this.collectpos = [];
  30. this.GetNode("collectbox").children.map(a => {
  31. this.collectpos.push(a.worldPosition);
  32. a.active = false;
  33. })
  34. }, 100);
  35. eventEmitter.register(this, GameConst.CLEAR_ALL_BOX, () => {
  36. console.log("清空盒子")
  37. this.lock=false
  38. setTimeout(() => {
  39. for (let i = 0; i < this.collectTiles.length; ++i) {
  40. this.pushBask(this.collectTiles[i].name);
  41. }
  42. this.collectTiles.map(a => {
  43. a.destroy();
  44. })
  45. this.collectTiles = [];
  46. }, 500);
  47. })
  48. //提示
  49. eventEmitter.register(this, GameConst.USE_ITEM_HINT,this.prompt.bind(this))
  50. }
  51. onEnable() {
  52. input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
  53. input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
  54. input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
  55. }
  56. onDisable() {
  57. input.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
  58. input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
  59. input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
  60. }
  61. moveToRightPos() {
  62. this.collectTiles.map((a, i) => {
  63. let tileItem = a.getComponent(TileItem)
  64. Tween.stopAllByTarget(a);
  65. tileItem.auto_rotation = true;
  66. tween(a).to(0.3, { worldPosition: this.collectpos[i].clone(), worldScale: v3(1, 1, 1) }).call(() => {
  67. if (tileItem.removed) {
  68. Utils.remove(this.optionTiles,a);
  69. tween(a).to(0.25, { worldPosition: tileItem.removedPos }, { easing: 'backIn' }).call(async () => {
  70. //最中央的播放移除特效
  71. if (tileItem.playRmovedEff) {
  72. //增加星星
  73. let pos = this.GetNode("Main Camera").getComponent(Camera).convertToUINode(a.worldPosition, find("Canvas"));
  74. let node_ani = await ResUtil.playSkAni("spine/effect_hecheng", "effect", find("Canvas"), pos);
  75. audioMgr.playOneShot(GameConst.audios.starCollect);
  76. platformSystem.platform.vibrateShort();
  77. Utils.flyAnim(ITEM_TYPE.Star, node_ani, Main.I._GameUI.starNode, 1, 0, (b) => {
  78. if (b) {
  79. Main.I._GameUI.star++;
  80. }
  81. });
  82. }
  83. a.destroy();
  84. this.moveToRightPos()
  85. this.checkResult()
  86. }).start()
  87. }
  88. }).start();
  89. })
  90. }
  91. chooseTile(tile: Node) {
  92. if (!tile || !tile.isValid) return;
  93. let tileItem = tile.getComponent(TileItem);
  94. if (!tileItem) return;
  95. if (tileItem.isMoving) return;
  96. let chooseName = tile.name;
  97. this.optionTiles.push(tile);
  98. // 删除点击对象从列表
  99. tileItem.destoryCollider();
  100. for (var i = 0; i < this.allTiles.length; ++i) {
  101. if (this.allTiles[i] == tile) {
  102. this.allTiles.splice(i, 1);
  103. break;
  104. }
  105. }
  106. // 计算目标位置
  107. let targetPos: Vec3;
  108. let bInsert = false;
  109. for (var i = this.collectTiles.length - 1; i >= 0; --i) {
  110. if (this.collectTiles[i].name == chooseName) {
  111. targetPos = this.collectpos[i + 1]?.clone() || this.collectpos[this.collectpos.length - 1].clone();
  112. this.collectTiles.splice(i + 1, 0, tile);
  113. bInsert = true;
  114. break;
  115. }
  116. }
  117. if (!bInsert) {
  118. targetPos = this.collectpos[this.collectTiles.length]?.clone() || this.collectpos[0].clone();
  119. this.collectTiles.push(tile);
  120. }
  121. //添加上抛动画 标记物品为移动中
  122. tileItem.isMoving = true;
  123. const startPos = tile.worldPosition.clone();
  124. const midHeight = 10; // 上抛高度
  125. const duration = 0.2; // 动画时长
  126. this.parabolicMovementWithScale(
  127. tile,
  128. startPos,
  129. targetPos,
  130. midHeight,
  131. duration,
  132. tile.scale.clone(),
  133. v3(1, 1, 1),
  134. (n: Node) => {
  135. this.moveToRightPos()
  136. tile.getComponent(TileItem).isMoving = false;
  137. //动画完成后执行原有逻辑
  138. let tmp = this.collectTiles.filter(a => {
  139. return a.name == chooseName;
  140. })
  141. if (tmp.length >= 3) {
  142. for (var i = this.collectTiles.length - 1; i >= 0; --i) {
  143. if (this.collectTiles[i].name == chooseName) {
  144. let delNode = this.collectTiles[i];
  145. delNode.getComponent(TileItem).removed = true;
  146. this.collectTiles.splice(i, 1);
  147. delNode.getComponent(TileItem).removedPos = tmp[1].worldPosition;
  148. if (delNode == tmp[1])
  149. delNode.getComponent(TileItem).playRmovedEff = true;
  150. }
  151. }
  152. }
  153. this.checkResult();
  154. }
  155. );
  156. }
  157. checkResult() {
  158. if(this.lock)return
  159. //检测胜利和失败
  160. if (this.collectTiles.length >= 7) {
  161. this.lock=true
  162. if (!WindowManager.ins.isShow("OutOfBoxLayer"))
  163. WindowManager.ins.open("OutOfBoxLayer");
  164. } else if (this.collectTiles.length == 0 && this.allTiles.length == 0) {
  165. this.lock=true
  166. WindowManager.ins.open("DrawStarLayer").then((com: DrawStarLayer) => {
  167. com.setStar(Main.I._GameUI.star);
  168. })
  169. }
  170. }
  171. private touchTileItem: TileItem = null;
  172. onTouchStart(event: EventTouch) {
  173. if (Main.I._GameUI.pasue) return;
  174. if (Data.user.useMagnet) return;
  175. if (this.allTiles.length == 0) return;
  176. if (this.collectTiles.length >= 7) return;
  177. const p = event.getLocation();
  178. let camera = this.GetNode("Main Camera").getComponent(Camera);
  179. const r = new Ray();
  180. camera?.screenPointToRay(p.x, p.y, r);
  181. let b = PhysicsSystem.instance.raycastClosest(r, 1);
  182. if (b) {
  183. let collider = PhysicsSystem.instance.raycastClosestResult.collider;
  184. if (!collider || !collider.node || !collider.node.parent) return;
  185. let item = collider.node.parent.getComponent(TileItem);
  186. if (!item || !item.node || !item.node.isValid) return;
  187. item.enableCollider = false;
  188. let pos = item.node.getWorldPosition();
  189. pos.y += 0.3;
  190. item.node.setWorldPosition(pos);
  191. this.touchTileItem = item;
  192. }
  193. }
  194. onTouchEnd(event: EventTouch) {
  195. if (Main.I._GameUI.pasue) return;
  196. if (Data.user.useMagnet) return;
  197. if (!this.touchTileItem) return;
  198. if (this.allTiles.length == 0) return;
  199. if (this.collectTiles.length >= 7) {
  200. return;
  201. }
  202. const p = event.getLocation();
  203. let camera = this.GetNode("Main Camera").getComponent(Camera);
  204. const r = new Ray();
  205. camera?.screenPointToRay(p.x, p.y, r);
  206. let b = PhysicsSystem.instance.raycastClosest(r, 1);
  207. if (b) {
  208. let collider = PhysicsSystem.instance.raycastClosestResult.collider;
  209. // if(!collider.getComponent(RigidBody))return;
  210. let item = collider.node.parent.getComponent(TileItem);
  211. if (item && item == this.touchTileItem) {
  212. platformSystem.platform.vibrateShort();
  213. this.chooseTile(item.node);
  214. this.touchTileItem = null;
  215. audioMgr.playOneShot(GameConst.audios.tap);
  216. }else {
  217. this.touchTileItem.enableCollider = true;
  218. }
  219. }
  220. }
  221. onTouchMove(event: EventTouch) {
  222. if (Main.I._GameUI.pasue) return;
  223. if (Data.user.useMagnet) return;
  224. if (this.allTiles.length == 0) return;
  225. if (this.collectTiles.length >= 7) {
  226. return;
  227. }
  228. const p = event.getLocation();
  229. let camera = this.GetNode("Main Camera").getComponent(Camera);
  230. const r = new Ray();
  231. camera?.screenPointToRay(p.x, p.y, r);
  232. let b = PhysicsSystem.instance.raycastClosest(r, 1)
  233. if (b) {
  234. let collider = PhysicsSystem.instance.raycastClosestResult.collider
  235. // if(!collider.getComponent(RigidBody))return;
  236. let item = collider.node.parent.getComponent(TileItem);
  237. if (item) {
  238. if (this.touchTileItem && item == this.touchTileItem) {
  239. }else {
  240. if (this.touchTileItem) this.touchTileItem.enableCollider = true;
  241. item.enableCollider = false;
  242. let pos = item.node.getWorldPosition();
  243. pos.y += 0.3;
  244. // console.log("切换")
  245. item.node.setWorldPosition(pos);
  246. this.touchTileItem = item;
  247. }
  248. }
  249. else {
  250. if (this.touchTileItem) {
  251. // console.log("无目标")
  252. this.touchTileItem.enableCollider = true;
  253. this.touchTileItem = null;
  254. }
  255. }
  256. }
  257. }
  258. initWall() {
  259. if (!PhysicsSystem.instance) return;
  260. const OFFSET = 25;
  261. let camera = this.GetNode("Main Camera").getComponent(Camera);
  262. const r = new Ray();
  263. let size = screen.windowSize;
  264. //左边墙
  265. camera?.screenPointToRay(OFFSET, size.height / 2, r);
  266. if (PhysicsSystem.instance.raycastClosest(r)) {
  267. const result = PhysicsSystem.instance.raycastClosestResult;
  268. this.GetNode("PlaneLeft").setWorldPosition(result.hitPoint);
  269. }
  270. //右边墙
  271. camera?.screenPointToRay(size.width - OFFSET, size.height / 2, r);
  272. if (PhysicsSystem.instance.raycastClosest(r)) {
  273. const result = PhysicsSystem.instance.raycastClosestResult;
  274. this.GetNode("PlaneRight").setWorldPosition(result.hitPoint);
  275. }
  276. //上边墙
  277. camera?.screenPointToRay(size.width / 2, size.height - OFFSET * 4, r);
  278. if (PhysicsSystem.instance.raycastClosest(r)) {
  279. const result = PhysicsSystem.instance.raycastClosestResult;
  280. this.GetNode("PlaneTop").setWorldPosition(result.hitPoint);
  281. }
  282. let minx = this.GetNode("PlaneLeft").worldPosition.x;
  283. let maxx = this.GetNode("PlaneRight").worldPosition.x;
  284. let scale = (maxx - minx) / 8.461091041564941;
  285. this.GetNode("bottom").scale = v3(scale, scale, scale);
  286. //格子位置
  287. camera?.screenPointToRay(size.width / 2, 10, r);
  288. if (PhysicsSystem.instance.raycastClosest(r)) {
  289. const result = PhysicsSystem.instance.raycastClosestResult;
  290. //result.hitPoint
  291. let p: Vec3 = new Vec3(result.hitPoint.x,4,result.hitPoint.z);
  292. this.GetNode("bottom").setWorldPosition(p);
  293. }
  294. //下边墙
  295. this.GetNode("PlaneDown").setWorldPosition(this.GetNode("bottom").worldPosition.add3f(0, 0, -2.5 * scale));
  296. }
  297. async createTiles() {
  298. let Margin = 0
  299. let minx = this.GetNode("PlaneLeft").worldPosition.x + Margin;
  300. let maxx = this.GetNode("PlaneRight").worldPosition.x - Margin;
  301. let maxz = this.GetNode("PlaneDown").worldPosition.z + Margin;
  302. let minz = this.GetNode("PlaneTop").worldPosition.z - Margin;
  303. let obj = levelsData.getCurLevelInfo();
  304. //数量
  305. let tileCount = Math.floor(obj.count / 3);
  306. let names:Array<string> = levelsData.getModesNames();
  307. let tilepools = Utils.getRandomElements(names,obj.kind);
  308. tilepools = Utils.extendArray(tilepools,tileCount);
  309. Main.I._GameUI.pasue = true;
  310. BusyLoadingManager.ins.addBusy(BUSY_TYPE.RES);
  311. for (let i = 0; i < tileCount; i++) {
  312. let ret = await ResUtil.loadModelPrefabName(tilepools[i]) as Prefab;
  313. if (!ret) continue;
  314. //获取节点下的 MeshRenderer 组件 设置阴影投射模式
  315. (ret.data.getComponentInChildren(MeshRenderer) as MeshRenderer).shadowCastingMode = MeshRenderer.ShadowCastingMode.ON;
  316. for (let j = 0; j < 3; j++) {
  317. const globalIndex = (i * 3 + j) % 9;
  318. let lnode = this.GetNode(`l${globalIndex}`);
  319. tween(this.node).delay(j * 0.03).call(() => {
  320. let nomal_node: Node = instantiate(ret);
  321. nomal_node.name = ret.name;
  322. let tile = nomal_node.addComponent(TileItem);
  323. let rigid = tile.addCollider();
  324. nomal_node.scale = v3(1.25, 1.25, 1.25)
  325. if (Data.user.lv == 1) {
  326. nomal_node.setWorldPosition(lnode.getWorldPosition());
  327. nomal_node.parent = this.node;
  328. this.allTiles.push(nomal_node);
  329. }else{
  330. let p = v3(Utils.getRandomInt(minx / 2, maxx / 2), Utils.getRandom(12, 15), Utils.getRandomInt(minz / 2, maxz / 2));
  331. nomal_node.setWorldRotationFromEuler(Utils.getRandom(0, 300), Utils.getRandom(0, 300), Utils.getRandom(0, 300))
  332. nomal_node.setWorldPosition(p);
  333. nomal_node.parent = this.node;
  334. rigid.applyImpulse(v3(0, 3, 0))
  335. this.allTiles.push(nomal_node);
  336. }
  337. }).start()
  338. }
  339. if (i == tileCount - 1) {
  340. BusyLoadingManager.ins.removeBusy(BUSY_TYPE.RES);
  341. Main.I._GameUI.pasue = false;
  342. }
  343. }
  344. setTimeout(() => {
  345. if (Data.user.useMagnet) {
  346. Main.I._GameUI.pasue = true;
  347. Data.user.magnet--;
  348. let p = this.GetNode("magnet").worldPosition;
  349. this.GetNode("magnet").worldPosition = v3(-10, p.y, p.z);
  350. tween(this.GetNode("magnet")).to(0.5, { worldPosition: v3(0, p.y, p.z) }).call(async () => {
  351. let arr = [];
  352. for (var i = this.allTiles.length - 1; i >= 0; --i) {
  353. let pick = false;
  354. let item = this.allTiles[i];
  355. let tmp: Node = arr[0];
  356. if(tmp){
  357. if (tmp.name == item.name && arr.length < 3) {
  358. arr.push(item);
  359. pick = true;
  360. }
  361. }else{
  362. arr.push(item);
  363. pick = true;
  364. }
  365. if(pick) {
  366. let tile: TileItem = this.allTiles[i].getComponent(TileItem);
  367. tile.destoryCollider();
  368. this.GetNode("magnet_content").addChild(tile.node);
  369. tween(tile.node).to(0.5, { worldPosition: this.GetNode(arr.length % 2 ? "m_left" : "r_left").worldPosition }).start();
  370. this.allTiles.splice(i, 1);
  371. console.log("删除", tile.name, this.allTiles.length)
  372. }
  373. }
  374. Data.user.useMagnet = false;
  375. }).delay(1.5).to(0.5, { worldPosition: v3(10, p.y, p.z) }).call(() => {
  376. this.GetNode("magnet_content").removeAllChildren();
  377. Main.I._GameUI.pasue = false;
  378. if (Data.user.useTime && Data.user.time > 0)
  379. eventEmitter.dispatch(GameConst.USE_ITEM_TIME)
  380. }).start();
  381. }else {
  382. if (Data.user.useTime && Data.user.time > 0){
  383. eventEmitter.dispatch(GameConst.USE_ITEM_TIME)}
  384. }
  385. }, 2000);
  386. }
  387. /**
  388. * 提示
  389. */
  390. restart() {
  391. this.allTiles.map(a => a.destroy());
  392. this.allTiles = [];
  393. Main.I._GameUI.star = 0;
  394. this.collectTiles.map(a => a.destroy());
  395. this.collectTiles = [];
  396. this.createTiles();
  397. this.lock=false
  398. }
  399. /**移除*/
  400. public remove(){
  401. if (this.optionTiles.length === 0 || this.lock) return;
  402. // 获取最后一个收集的物品
  403. const lastItem = this.optionTiles.pop();
  404. if (!lastItem) return;
  405. Utils.remove(this.collectTiles,lastItem);
  406. const tile = lastItem.getComponent(TileItem);
  407. tile.removed = false;
  408. tile.auto_rotation = false;
  409. //播放撤销动画
  410. Tween.stopAllByTarget(lastItem);
  411. //const globalIndex = Math.floor(Math.random() * 9);
  412. let lnode = this.GetNode(`l${4}`);
  413. let p:Vec3 = lnode.position.clone();
  414. tween(tile.node)
  415. .to(0.35, {
  416. worldPosition: new Vec3(p.x,p.y + 1,p.z),
  417. worldScale: v3(1.25, 1.25, 1.25)
  418. })
  419. .call(() => {
  420. tile.addCollider();
  421. tile.enableCollider = true;
  422. this.allTiles.push(lastItem);
  423. //向上施加力 然后在向下重力效果
  424. tile.rigid.applyImpulse(v3(0, 3, 0))
  425. this.moveToRightPos(); // 重新排列收集框中的物品
  426. })
  427. .start();
  428. }
  429. /**乱序*/
  430. public mess(){
  431. if(this.allTiles.length === 0 || this.lock) return;
  432. this.allTiles.forEach((item, index) => {
  433. const tileItem = item.getComponent(TileItem);
  434. tileItem.auto_rotation = false;
  435. tileItem.enableCollider = true;
  436. Tween.stopAllByTarget(item);
  437. tween(item)
  438. .to(0.3, {
  439. worldPosition: this.GetNode("l4").worldPosition.clone().add(v3(0, 1, 0)),
  440. worldScale: v3(1.25, 1.25, 1.25)
  441. })
  442. .call(() => {
  443. const rigid = tileItem.addCollider();
  444. rigid.applyImpulse(v3(
  445. Utils.getRandom(-3, 3),
  446. Utils.getRandom(5, 8),
  447. Utils.getRandom(-3, 3)
  448. ));
  449. item.setWorldRotationFromEuler(Utils.getRandom(0, 300), Utils.getRandom(0, 300), Utils.getRandom(0, 300))
  450. })
  451. .start();
  452. });
  453. //audioManager.playOneShot(GameConst.audios.shuffle);
  454. platformSystem.platform.vibrateShort();
  455. }
  456. /**凑齐*/
  457. public prompt(){
  458. if(this.collectTiles.length <= 0 || Data.user.hint <= 0)return;
  459. let obj = {}
  460. for (var i = 0; i < this.collectTiles.length; ++i) {
  461. let name = this.collectTiles[i].name;
  462. if (!obj[name]) obj[name] = 0;
  463. obj[name]++;
  464. }
  465. let hint_succ = false;
  466. let bFind = false;
  467. let hint_name = ""
  468. for (const key in obj) {
  469. if (obj[key] == 2) {
  470. hint_name = key;
  471. bFind = true;
  472. break;
  473. }
  474. }
  475. //找到两个一样
  476. if (bFind) {
  477. console.log("找到两个一样")
  478. for (let i = 0; i < this.allTiles.length; ++i) {
  479. if (this.allTiles[i].name == hint_name) {
  480. this.chooseTile(this.allTiles[i]);
  481. break;
  482. }
  483. }
  484. hint_succ = true;
  485. }else {
  486. if (this.collectTiles.length >= 5) {
  487. MsgHints.show("未找到合适物品");
  488. return
  489. }else {
  490. hint_name = this.collectTiles[0] ? this.collectTiles[0].name : this.allTiles[0].name;
  491. let lsit = this.allTiles.filter(a => {
  492. return a.name == hint_name;
  493. })
  494. lsit.map((a, i) => {
  495. setTimeout(() => {
  496. this.chooseTile(a);
  497. }, 350 * i);
  498. })
  499. hint_succ = true;
  500. }
  501. }
  502. if (hint_succ) {
  503. Data.user.hint--;
  504. }
  505. }
  506. //放回场景中
  507. async pushBask(name: string) {
  508. let Margin = 0;
  509. let maxz = this.GetNode("PlaneDown").worldPosition.z + Margin;
  510. let minz = this.GetNode("PlaneTop").worldPosition.z - Margin;
  511. let minx = this.GetNode("PlaneLeft").worldPosition.x + Margin;
  512. let maxx = this.GetNode("PlaneRight").worldPosition.x - Margin;
  513. let ret = await ResUtil.loadModelPrefabName(name) as Prefab;
  514. let nomal_node: Node = instantiate(ret);
  515. nomal_node.name = name;
  516. let tile = nomal_node.addComponent(TileItem);
  517. let rigid = tile.addCollider();
  518. nomal_node.scale = v3(1.25,1.25, 1.25)
  519. nomal_node.parent = this.node;
  520. rigid.applyImpulse(v3(0, 3, 0))
  521. let p = v3(minx + (maxx - minx) / 2, Utils.getRandom(12, 15), minz + (maxz - minz) / 2);
  522. nomal_node.setWorldRotationFromEuler(Utils.getRandom(0, 300), Utils.getRandom(0, 300), Utils.getRandom(0, 300))
  523. nomal_node.setWorldPosition(p);
  524. this.allTiles.push(nomal_node);
  525. }
  526. /**
  527. * 抛物线运动工具(带缩放动画)
  528. * @param node 要移动的节点
  529. * @param startPos 起始坐标(世界坐标)
  530. * @param endPos 目标坐标(世界坐标)
  531. * @param height 抛物线最高点高度(相对于起点)
  532. * @param duration 动画时长(秒)
  533. * @param startScale 起始缩放(默认原始大小)
  534. * @param endScale 目标缩放(默认原始大小)
  535. * @param onComplete 完成回调
  536. */
  537. public parabolicMovementWithScale(
  538. node: Node,
  539. startPos: Vec3,
  540. endPos: Vec3,
  541. height: number = 2,
  542. duration: number = 1.5,
  543. startScale: Vec3 = Vec3.ONE,
  544. endScale: Vec3 = Vec3.ONE,
  545. onComplete?: (n:Node) => void
  546. ): Tween<Node> {
  547. if(Vec3.equals(startPos, endPos)) {
  548. onComplete?.(node);
  549. }else{
  550. //初始化节点状态
  551. node.setWorldPosition(startPos.clone());
  552. node.setScale(startScale);
  553. //计算抛物线顶点
  554. const midPoint = new Vec3(
  555. (startPos.x + endPos.x) / 2,
  556. Math.max(startPos.y, endPos.y) + height,
  557. (startPos.z + endPos.z) / 2
  558. );
  559. //创建组合动画
  560. return tween(node)
  561. .to(duration,
  562. {
  563. worldPosition: endPos,
  564. worldScale: endScale
  565. },
  566. {
  567. onUpdate: (target: Node, ratio: number) => {
  568. //1. 计算抛物线位置
  569. const t = ratio;
  570. const invT = 1 - t;
  571. const tempPos = new Vec3();
  572. // XZ轴线性插值
  573. Vec3.lerp(tempPos, startPos, endPos, t);
  574. //Y轴抛物线计算
  575. const y = invT * invT * startPos.y
  576. + 2 * invT * t * midPoint.y
  577. + t * t * endPos.y;
  578. //2. 计算当前缩放比例
  579. const currentScale = new Vec3();
  580. Vec3.lerp(currentScale, startScale, endScale, t);
  581. //更新节点状态
  582. target.worldPosition = new Vec3(tempPos.x, y, tempPos.z);
  583. target.setScale(currentScale);
  584. }
  585. }
  586. )
  587. .call(() => {
  588. // 最终确保缩放精确
  589. node.setScale(endScale);
  590. onComplete?.(node);
  591. })
  592. .start();
  593. }
  594. }
  595. }