GameNode.ts 26 KB

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