GameNode.ts 26 KB

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