CellItem.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. import GamePlay from "../../GamePlay";
  2. import { PROPTYPE, SURPRISETASKTYPE } from "../../data/Enum";
  3. import GameLogic from "../../util/GameLogic";
  4. import PoolMgr, { NODEPOOLPREFABTYPE } from "../../mgr/PoolMgr";
  5. import GameConst from "../../data/GameConst";
  6. import AniFinishDestroy from "../../util/common/AniFinishDestroy";
  7. import EliminateScore from "../effect/EliminateScore";
  8. import ScoreEnergy from "../effect/ScoreEnergy";
  9. import PlayerConst from "../../data/PlayerConst";
  10. const { ccclass, property } = cc._decorator;
  11. @ccclass
  12. export default class CellItem extends cc.Component {
  13. /**cellItem的ani效果 */
  14. @property(cc.Animation)
  15. ani_cellItem: cc.Animation = null;
  16. /**cellItem的Icon图 */
  17. @property(cc.Sprite)
  18. spr_cellItemIcon: cc.Sprite = null;
  19. /**cellItem的Icon图 */
  20. @property(cc.Sprite)
  21. spr_redPackIcon: cc.Sprite = null;
  22. /**纵列数 */
  23. public x: number = 0;
  24. /**横列数 */
  25. public y: number = 0;
  26. /**类型 */
  27. public type: number = 0;
  28. /**是否移除 */
  29. public ifRemoved: boolean = false;
  30. /**是否有红包 */
  31. public ifRedPacket: boolean = false;
  32. // LIFE-CYCLE CALLBACKS:
  33. // onLoad () {}
  34. start() {
  35. this.node.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
  36. }
  37. // update (dt) {}
  38. /**初始化
  39. * @param x 横坐标
  40. * @param y 纵坐标
  41. */
  42. init(index_x: number, index_y: number) {
  43. this.x = index_x;
  44. this.y = index_y;
  45. this.ifRemoved = false;
  46. let endPos = this.getEndPos();
  47. this.node.x = endPos.x;
  48. this.node.y = cc.winSize.height * 0.5 + this.node.height + 50;
  49. this.ani_cellItem.play();
  50. //LogUtil.log("[CellItem] init", this.x, this.y, endPos);
  51. this.setType();
  52. cc.tween(this.node)
  53. .delay(0.2)
  54. .to(0.3, { y: endPos.y })
  55. .call(() => {
  56. }).start()
  57. }
  58. /**获取结束位置 */
  59. getEndPos(): cc.Vec2 {
  60. let endX = (this.x * this.node.width + this.node.width * 0.5) - GamePlay.Inst.node_content.width * 0.5;
  61. let endY = -(this.y * this.node.height + this.node.height * 0.5) + GamePlay.Inst.node_content.height * 0.5;
  62. return new cc.Vec2(endX, endY);
  63. }
  64. /**设置类型 */
  65. setType(type: number = null) {
  66. this.node.opacity = 255;
  67. let lastType = this.type;
  68. if (type) {
  69. //LogUtil.log("【cellItem】 重置重置动画动画!!!!!!!!!!!!!!!!!!!!")
  70. this.ani_cellItem.play("cellItem_reset");
  71. this.type = type;
  72. }
  73. else {
  74. let type: number = null;
  75. if (GameConst.config_level[PlayerConst.levelNum]) {
  76. //原先
  77. //type = GameConst.config_level[PlayerConst.levelNum][this.x][this.y];
  78. //现在(配置就对照游戏真实的布局)
  79. type = GameConst.config_level[PlayerConst.levelNum][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. // LogUtil.log("typ111111", type, this.type);
  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. 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.spr_redPackIcon.node.active = true;
  235. }
  236. /**
  237. * 回收
  238. * @param recycleType 回收类型
  239. * @param ifLastEliminate 是否最后一个消除
  240. * @param ifShowEffect 是否显示特效 暴力解决 返回主界面进入 会再消除一遍问题 有时间可以好好研究
  241. */
  242. 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 = 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 = 10 + GamePlay.Inst.cleanedVecArr.length * 5;
  270. //得分特效
  271. let node_score = 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 = 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.spr_redPackIcon.node.active) {
  287. mk.ui.openPanel("module/reward/rewardLuck");
  288. }
  289. }
  290. }
  291. if (this.spr_redPackIcon.node.active) {
  292. this.spr_redPackIcon.node.active = false;
  293. }
  294. //回收
  295. PoolMgr.Inst.recyclePoolPrefab(NODEPOOLPREFABTYPE.CellItem, this.node);
  296. }
  297. }