GameNode.ts 26 KB

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