CellItem.ts 13 KB

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