GameNode.ts 26 KB

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