CellItem.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import GamePlay from "../../GamePlay";
  2. import GameLogic from "../../util/GameLogic";
  3. import PoolMgr, { NODEPOOLPREFABTYPE } from "../../mgr/PoolMgr";
  4. import GameConst from "../../data/GameConst";
  5. import AniFinishDestroy from "../../util/common/AniFinishDestroy";
  6. import EliminateScore from "../effect/EliminateScore";
  7. import ScoreEnergy from "../effect/ScoreEnergy";
  8. const { ccclass, property } = cc._decorator;
  9. @ccclass
  10. export default class CellItem extends cc.Component {
  11. /**cellItem的ani效果 */
  12. @property(cc.Animation)
  13. ani_cellItem: cc.Animation = null;
  14. /**cellItem的Icon图 */
  15. @property(cc.Sprite)
  16. spr_cellItemIcon: cc.Sprite = null;
  17. /**cellItem的Icon图 */
  18. @property(cc.Node)
  19. node_redPackIcon: cc.Node = null;
  20. /**纵列数 */
  21. public x: number = 0;
  22. /**横列数 */
  23. public y: number = 0;
  24. /**类型 */
  25. public type: number = 0;
  26. /**是否移除 */
  27. public ifRemoved: boolean = false;
  28. /**是否有红包 */
  29. public ifRedPacket: boolean = false;
  30. // LIFE-CYCLE CALLBACKS:
  31. // onLoad () {}
  32. start() {
  33. this.node.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
  34. }
  35. // update (dt) {}
  36. /**初始化
  37. * @param x 横坐标
  38. * @param y 纵坐标
  39. */
  40. init(index_x: number, index_y: number) {
  41. this.x = index_x;
  42. this.y = index_y;
  43. this.ifRemoved = false;
  44. let endPos = this.getEndPos();
  45. this.node.x = endPos.x;
  46. this.node.y = cc.winSize.height * 0.5 + this.node.height + 50;
  47. this.ani_cellItem.play();
  48. //LogUtil.log("[CellItem] init", this.x, this.y, endPos);
  49. this.setType();
  50. cc.tween(this.node)
  51. .delay(0.2)
  52. .to(0.3, { y: endPos.y })
  53. .call(() => {
  54. }).start()
  55. }
  56. /**获取结束位置 */
  57. getEndPos(): cc.Vec2 {
  58. let endX = (this.x * this.node.width + this.node.width * 0.5) - GamePlay.Inst.node_content.width * 0.5;
  59. let endY = -(this.y * this.node.height + this.node.height * 0.5) + GamePlay.Inst.node_content.height * 0.5;
  60. return new cc.Vec2(endX, endY);
  61. }
  62. /**设置类型 */
  63. setType(type: number = null) {
  64. this.node.opacity = 255;
  65. let lastType = this.type;
  66. if (type) {
  67. //LogUtil.log("【cellItem】 重置重置动画动画!!!!!!!!!!!!!!!!!!!!")
  68. this.ani_cellItem.play("cellItem_reset");
  69. this.type = type;
  70. }
  71. else {
  72. // let type: number = null;
  73. // let level = gData.gameData.getProp(GameProp.levelNum);
  74. // if (level != null && level != undefined) {
  75. // //原先
  76. // //type = GameConst.config_level[PlayerConst.levelNum][this.x][this.y];
  77. // //现在(配置就对照游戏真实的布局)
  78. // type = GameConst.config_level[level][this.y][this.x];
  79. // }
  80. // if (type) {
  81. // this.type = type;
  82. // }
  83. // else {
  84. // this.type = Math.floor(Math.random() * 5) + 1;
  85. // }
  86. this.type = GameConst.curLevelCfg[this.y][this.x];
  87. }
  88. this.spr_cellItemIcon.node.width = 75;
  89. this.spr_cellItemIcon.node.height = 75;
  90. // LogUtil.log("lastType", lastType);
  91. // LogUtil.log("type", type);
  92. if (!lastType || lastType != this.type) {
  93. mk.loader.load(`game/otherGame/texture/cellItemIcon/${GameConst.iconIndex}/${this.type}`, cc.SpriteFrame).then((spriteFrame) => {
  94. this.spr_cellItemIcon.spriteFrame = spriteFrame;
  95. })
  96. }
  97. }
  98. /**重置 */
  99. reset() {
  100. //LogUtil.log("【cellItem】 重置重置动画动画!!!!!!!!!!!!!!!!!!!!")
  101. this.ani_cellItem.play("cellItem_reset");
  102. let type = Math.floor(Math.random() * 5) + 1;
  103. this.setType(type);
  104. }
  105. /**点击 */
  106. onClick() {
  107. //FC:是否可以点击
  108. if (!GamePlay.Inst.ifCouldClick) {
  109. return;
  110. }
  111. else {
  112. GamePlay.Inst.ifCouldClick = false;
  113. }
  114. //LogUtil.log("Game.Inst.cellItemDic", Game.Inst.cellItemDic, this.x, this.y);
  115. //重置提示
  116. GamePlay.Inst.intervalShowGuide();
  117. // GamePlay.Inst.intervalShowInter();
  118. // if (GamePlay.Inst.node_changeCellItemUI.active) {
  119. // GamePlay.Inst.node_changeCellItemUI.active = false;
  120. // }
  121. //重置
  122. GamePlay.Inst.cleanedVecArr = [];
  123. GamePlay.Inst.cleanXIndexArr = [];
  124. // //更改颜色道具
  125. // if (GamePlay.Inst.curPropType == PROPTYPE.Change) {
  126. // GamePlay.Inst.curPropType = PROPTYPE.Null;
  127. // GamePlay.Inst.curSelectCellItem = this;
  128. // GamePlay.Inst.showChangeCellItemUI();
  129. // this.node.opacity = 150;
  130. // return;
  131. // }
  132. // //锤子道具
  133. // else if (GamePlay.Inst.curPropType == PROPTYPE.Hammer) {
  134. // GamePlay.Inst.curPropType = PROPTYPE.Null;
  135. // GamePlay.Inst.curSelectCellItem = this;
  136. // let aroundSameTypeNum = GameLogic.getAroundSameType(this.x, this.y, this.type, false).length;
  137. // if (aroundSameTypeNum <= 0) {
  138. // mk.console.log("周围没有相同的Item ----------------------------------------");
  139. // GamePlay.Inst.cleanedVecArr.push(new cc.Vec2(this.x, this.y));
  140. // //this.ifRemoved = true;
  141. // }
  142. // }
  143. GamePlay.Inst.curClickCellItem = this;
  144. //获取清除的CellItem的位置信息列表
  145. GameLogic.getCleanedVecList(this.x, this.y, this.type);
  146. //根据vec列表清理cellItem
  147. GamePlay.Inst.cleanCellItemByVecList();
  148. }
  149. /**
  150. * 向下移动
  151. * @param index_x x索引值
  152. * @param index_y 下移至的y索引值
  153. * @param moveIndex moveIndex
  154. * @param ifLastMoveCellItem 是否是最后一个移动的CellItem
  155. */
  156. moveDown(index_x: number, index_y: number, moveIndex: number, ifLastMoveCellItem: boolean = false) {
  157. //如果不需要移动就return
  158. if (this.y == index_y) {
  159. if (ifLastMoveCellItem) {
  160. GamePlay.Inst.checkMoveLeftCellItem();
  161. }
  162. return;
  163. }
  164. // LogUtil.log("[CellItem] index_x index_y", index_x, index_y);
  165. // LogUtil.log("Game.Inst.cellItemDic", Game.Inst.cellItemDic);
  166. //移动方块数
  167. let intervalNum = index_y - this.y;
  168. //将原来位置置空
  169. GamePlay.Inst.cellItemDic[this.x][this.y] = null;
  170. //重新赋值位置
  171. this.x = index_x;
  172. this.y = index_y;
  173. //给现在位置重新赋值
  174. GamePlay.Inst.cellItemDic[this.x][this.y] = this;
  175. //let endX = index_x * this.node.width + this.node.width * 0.5 - Game.Inst.node_content.width * 0.5;
  176. let endY = -(this.y * this.node.height + this.node.height * 0.5) + GamePlay.Inst.node_content.height * 0.5;
  177. let delayTime = moveIndex * 0.03;
  178. let moveDwonTime = intervalNum * 0.12;
  179. //LogUtil.log("delay moveIndex", moveIndex, delayTime);
  180. cc.tween(this.node)
  181. .delay(delayTime)
  182. .call(() => {
  183. this.ani_cellItem.play("cellItem_down1");
  184. })
  185. .to(moveDwonTime, { y: endY })
  186. .call(() => {
  187. this.ani_cellItem.play("cellItem_down2");
  188. //如果是最后一个移动
  189. if (ifLastMoveCellItem) {
  190. mk.console.log("【CellItem】检测是否能消除 移动完毕最后一个");
  191. GamePlay.Inst.checkMoveLeftCellItem();
  192. //Game.Inst.ifCouldClick = true;
  193. }
  194. }).start()
  195. }
  196. moveLeft(index_x: number, index_y: number, ifLastCellItem: boolean = false) {
  197. if (this.x == index_x) {
  198. if (ifLastCellItem) {
  199. }
  200. return;
  201. }
  202. //将原来位置置空
  203. GamePlay.Inst.cellItemDic[this.x][this.y] = null;
  204. //重新赋值位置
  205. this.x = index_x;
  206. this.y = index_y;
  207. //给现在位置重新赋值
  208. GamePlay.Inst.cellItemDic[this.x][this.y] = this;
  209. let endX = index_x * this.node.width + this.node.width * 0.5 - GamePlay.Inst.node_content.width * 0.5;
  210. //let endY = Game.Inst.node_content.height * 0.5 - index_y * this.node.height + this.node.height * 0.5;
  211. cc.tween(this.node)
  212. .to(0.2, { x: endX })
  213. .call(() => {
  214. if (ifLastCellItem) {
  215. mk.console.log("检测是否能消除!!!!!!!!!!!!!!!!!!!!!!!!!!最后移除");
  216. //先注释 避免重复判断
  217. GamePlay.Inst.checkIfEliminate();
  218. GamePlay.Inst.ifCouldClick = true;
  219. }
  220. }).start()
  221. }
  222. /**抖动 */
  223. shake() {
  224. this.ani_cellItem.play("cellItem_shake");
  225. }
  226. /**normal */
  227. normal() {
  228. // this.ifRemoved = false;
  229. this.node.opacity = 255;
  230. this.ani_cellItem.play();
  231. }
  232. /** */
  233. showRedPacket() {
  234. this.node_redPackIcon.active = true;
  235. }
  236. /**
  237. * 回收
  238. * @param recycleType 是否最后回收
  239. * @param ifLastEliminate 是否最后一个消除
  240. * @param ifShowEffect 是否显示特效 暴力解决 返回主界面进入 会再消除一遍问题 有时间可以好好研究
  241. */
  242. async recycle(recycleType: boolean = false, ifLastEliminate: boolean = false, ifShowEffect: boolean = true) {
  243. //这边不置空的话
  244. GamePlay.Inst.cellItemDic[this.x][this.y] = null;
  245. // let index = Game.Inst.cellItemList.indexOf(this);
  246. // if (index != -1) {
  247. // Game.Inst.cellItemList.splice(index, 1);
  248. // }
  249. let pos_x = this.node.x + GamePlay.Inst.node_content.x;
  250. let pos_y = this.node.y + GamePlay.Inst.node_content.y;
  251. if (GamePlay.Inst.curSelectCellItem == this) {
  252. GamePlay.Inst.curSelectCellItem = null;
  253. // if (GamePlay.Inst.curPropType == PROPTYPE.Hammer) {
  254. // GamePlay.Inst.curPropType = null;
  255. // let node_hammer = PoolMgr.Inst.getPoolPrefab(NODEPOOLPREFABTYPE.Hammer);
  256. // node_hammer.setPosition(pos_x, pos_y);
  257. // GamePlay.Inst.node_effectUI.addChild(node_hammer);
  258. // }
  259. }
  260. if (ifShowEffect) {
  261. //爆炸特效
  262. let node_baozha = await PoolMgr.Inst.getPoolPrefab(NODEPOOLPREFABTYPE.Baozha);
  263. node_baozha.x = pos_x;
  264. node_baozha.y = pos_y;
  265. GamePlay.Inst.node_effectUI.addChild(node_baozha);
  266. node_baozha.getComponent(AniFinishDestroy).init();
  267. //LogUtil.log("10 + Game.Inst.cleanedVecArr.length * 5", 10 + Game.Inst.cleanedVecArr.length * 5);
  268. //得分
  269. let score = 2 + (GamePlay.Inst.cleanedVecArr.length - 2) * 2;
  270. //得分特效
  271. let node_score = await PoolMgr.Inst.getPoolPrefab(NODEPOOLPREFABTYPE.EliminateScore);
  272. //LogUtil.log("node_score", node_score);
  273. let x = this.node.x + GamePlay.Inst.node_content.x;
  274. let y = this.node.y + GamePlay.Inst.node_content.y + 30;
  275. node_score.getComponent(EliminateScore).init(new cc.Vec2(x, y), score);
  276. GamePlay.Inst.node_effectUI.addChild(node_score);
  277. if (recycleType) {
  278. // GamePlay.Inst.curGetScore += score;
  279. // GamePlay.Inst.finalGetScore += score;
  280. // GamePlay.Inst.initScore();
  281. }
  282. else {
  283. let node_scoreEnergy = await PoolMgr.Inst.getPoolPrefab(NODEPOOLPREFABTYPE.ScoreEnergy);
  284. node_scoreEnergy.getComponent(ScoreEnergy).init(x, y, score);
  285. GamePlay.Inst.node_effectUI.addChild(node_scoreEnergy);
  286. if (this.node_redPackIcon.active) {
  287. GamePlay.Inst.ifOpenScoreRedPacket = true;
  288. }
  289. //如果消除的最后一个则更新
  290. if (ifLastEliminate) {
  291. GamePlay.Inst.curTotalScore = GamePlay.Inst.finalGetScore;
  292. }
  293. }
  294. }
  295. if (this.node_redPackIcon.active) {
  296. this.node_redPackIcon.active = false;
  297. }
  298. //回收
  299. PoolMgr.Inst.recyclePoolPrefab(NODEPOOLPREFABTYPE.CellItem, this.node);
  300. }
  301. }