GameNode.ts 26 KB

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