GameNode.ts 26 KB

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