CellItem.ts 13 KB

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