CellItem.ts 13 KB

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