CellItem.ts 12 KB

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