GameNode.ts 26 KB

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