本文由“壹伴编辑器”提供技术支

前言

来了来了,终于来了!上篇文章我们实现了方块的生成和交换,那本篇文章就到了该系列的重头戏,我们一起来实现消消乐的消除算法!

别说了,冲!!!

本文由“壹伴编辑器”提供技术支

正文

思路讲解

1. 首先我们确定消除规则,这里我们采用和开心消消乐类似的消除规则(核心为超过 3 个连续的同类型即可消除),具体分为以下几点:

1-1. 横型和竖型;这两种种情况很简单,我们只需要遍历每一行每一列,找出那些连续超过 3 个的组合就可以了

普通横竖型

1-2. 十字型T 型 L 型;这三种情况相对比较复杂了,但是实质上他们都是由一个横型加上一个竖型组合而成的,这三种组合的不同点在于他们的共同方块的上下左右有多少方块(比如十字型的共同方块上下左右都有 1 个以上的方块),我们需要进行额外的判断:

    

左:十字型和 T 型 | 右:L 型

代码实现

提示:项目已更新至码云,点击文章底部阅读原文获取该项目

1. 在 Enum 文件中定义一个组合类型的枚举:

// 以下为 Enum.ts 中添加的内容/*** 组合类型*/
export enum CombinationType {Horizontal = 1, // 横型Vertical, // 竖型Cross, // 十字型TShape, // T 型LShape, // L 型
}

2. 在 DataStructure 文件中定义一个组合的类:

// 以下为 DataStructure.ts 文件添加的内容/*** 组合*/
export class Combination {public coords: Coordinate[]; // 坐标集public commonCoord: Coordinate; // 共同坐标public type: CombinationType; // 组合类型constructor(coords: Coordinate[]) {this.coords = coords;this.updateType();}/*** 更新类型*/private updateType() {let up = 0;let down = 0;let left = 0;let right = 0;let keyCoord = this.commonCoord ? this.commonCoord : this.coords[0]; // 关键坐标// 收集数量for (let i = 0; i < this.coords.length; i++) {if (this.coords[i].compare(keyCoord)) continue; // 同一个坐标时跳过// 判断位置if (this.coords[i].x === keyCoord.x) {if (this.coords[i].y > keyCoord.y) up++;else down++;} else {if (this.coords[i].x < keyCoord.x) left++;else right++;}}// 判断类型if (up === 0 && down === 0) this.type = CombinationType.Horizontal;else if (left === 0 && right === 0) this.type = CombinationType.Vertical;else if (up > 0 && down > 0 && left > 0 && right > 0) this.type = CombinationType.Cross;else if ((up > 0 && down === 0 && left === 0 && right > 0) ||(up > 0 && down === 0 && left > 0 && right === 0) ||(up === 0 && down > 0 && left === 0 && right > 0) ||(up === 0 && down > 0 && left > 0 && right === 0)) {this.type = CombinationType.LShape;} else if ((up === 0 && down > 0 && left > 0 && right > 0) ||(up > 0 && down === 0 && left > 0 && right > 0) ||(up > 0 && down > 0 && left === 0 && right > 0) ||(up > 0 && down > 0 && left > 0 && right === 0)) {this.type = CombinationType.TShape;}}/*** 组合是否包含坐标集中的任意一个,有得返回对应坐标* @param coords 查询坐标集*/public include(coords: Coordinate[]): Coordinate {for (let i = 0; i < this.coords.length; i++) {for (let j = 0; j < coords.length; j++) {if (this.coords[i].compare(coords[j])) return coords[j];}}return null;}/*** 合并组合* @param coords 坐标集* @param commonCoord 共同坐标*/public merge(coords: Coordinate[], commonCoord: Coordinate) {for (let i = 0; i < coords.length; i++) {if (!coords[i].compare(commonCoord))this.coords.push(coords[i]);}this.commonCoord = commonCoord;this.updateType();}
}

3. 接下来在 GameUtil 中实现获取当前所有可消除组合的函数:

/*** 获取可消除的组合*/
public static getCombinations(typeMap: TileType[][]) {let combinations: Combination[] = [];// 逐行检测for (let r = 0; r < GameConfig.row; r++) {let count: number = 0;let type: TileType = null;for (let c = 0; c < GameConfig.col; c++) {if (c === 0) {count = 1; // 连续计数type = typeMap[c][r]; // 保存类型} else {if (typeMap[c][r] && typeMap[c][r] === type) {// 类型相同count++;// 到最后一个了,是不是有 3 个以上连续if (c === GameConfig.col - 1 && count >= 3) {let coords = [];for (let i = 0; i < count; i++) {coords.push(Coord(c - i, r));}combinations.push(new Combination(coords));}} else {// 类型不同if (count >= 3) {// 已累积 3 个let coords = [];for (let i = 0; i < count; i++) {coords.push(Coord(c - 1 - i, r));}combinations.push(new Combination(coords));}// 重置count = 1;type = typeMap[c][r];}}}}// 逐列检测for (let c = 0; c < GameConfig.col; c++) {let count: number = 0;let type: TileType = null;for (let r = 0; r < GameConfig.row; r++) {if (r === 0) {count = 1;type = typeMap[c][r];} else {if (typeMap[c][r] && typeMap[c][r] === type) {count++;if (r === GameConfig.row - 1 && count >= 3) {let coords = [];for (let i = 0; i < count; i++) {coords.push(Coord(c, r - i));}// 是否可以和已有组合合并let hasMerge = false;for (let i = 0; i < combinations.length; i++) {let common = combinations[i].include(coords);if (common) {combinations[i].merge(coords, common);hasMerge = true;break;}}if (!hasMerge) combinations.push(new Combination(coords));}} else {if (count >= 3) {let coords = [];for (let i = 0; i < count; i++) {coords.push(Coord(c, r - 1 - i));}// 是否可以和已有组合合并let hasMerge = false;for (let i = 0; i < combinations.length; i++) {let common = combinations[i].include(coords);if (common) {combinations[i].merge(coords, common);hasMerge = true;break;}}if (!hasMerge) combinations.push(new Combination(coords));}count = 1;type = typeMap[c][r];}}}}return combinations;
}

4. 然后我们对 TileManager 进行改造;添加了 combinations 变量、更新 tryExchange 函数并加入 eliminateCombinations 和 eliminateTile 函数:

private combinations: Combination[] = null; // 可消除组合/*** 尝试交换方块* @param coord1 1* @param coord2 2*/
private async tryExchange(coord1: Coordinate, coord2: Coordinate) {// 交换方块await this.exchangeTiles(coord1, coord2);// 获取可消除组合this.combinations = GameUtil.getCombinations(this.typeMap);if (this.combinations.length > 0) {// 消除!!!this.eliminateCombinations();} else {// 不能消除,换回来吧await this.exchangeTiles(coord1, coord2);}
}/*** 消除组合*/
private eliminateCombinations() {for (let i = 0; i < this.combinations.length; i++) {for (let j = 0; j < this.combinations[i].coords.length; j++) {this.eliminateTile(this.combinations[i].coords[j]);}}this.combinations = [];
}/*** 消除方块* @param coord 坐标*/
private eliminateTile(coord: Coordinate) {this.getTileMap(coord).disappear(); // 方块消失this.setTileMap(coord, null); // 数据置空this.setTypeMap(coord, null); // 数据置空
}

5. 此时,我们的消除功能也实现了:

★ 但是现在还有一个问题,游戏开始时就随机出现了一些可消除的组合,理论上来说开局时是不能有任何消除但是同时又要存在可一步消除的情况,所以这就是我们下篇文章会讲到的东西了。

本文由“壹伴编辑器”提供技术支

结束语

以上皆为本菜鸡的个人观点,文采实在不太好,如果写得不好还请各位见谅。如果有哪些地方说的不对,还请各位指出,大家共同进步。

接下来我会持续分享自己所学的知识与见解,欢迎各位关注本公众号。

我们,下次见!

本文由“壹伴编辑器”提供技术支

扫描二维码

获取更多精彩

文弱书生陈皮皮

点击 阅读原文 获取完整项目

[Cocos Creator] 制作简版消消乐(四):实现消除算法相关推荐

  1. [Cocos Creator] 制作简版消消乐(三):实现方块的生成与交换

    本文由"壹伴编辑器"提供技术支 前言 在上一篇文章中我们实现部分基础组件和管理脚本,那么本篇文章将和大家一起实现方块的生成与交换的逻辑. 温馨提醒:本文含有大量代码和注释,请提前做 ...

  2. App Store游戏付费榜前三,推荐一款Cocos Creator制作的文字类游戏

    不靠怒刷,不喜宣传,这款基于Cocos Creator制作的文字类游戏「荒野求生」就这样默默无闻地一跃而上,取得了App Store游戏榜前十的好成绩.而且上周还一举挤入游戏付费榜第三.文字类游戏排行 ...

  3. 用 Cocos Creator 制作平台跳跃游戏

    前言 平台跳跃类游戏如<超级马里奥><Celeste蔚蓝>等,非常考验玩家的操作和判断,有着非常本真的游戏乐趣.这类游戏乍一看,挺容易做的,但是要做好却不太容易.今天,我将使用 ...

  4. 初学者使用cocos creator制作第一个小游戏以及脚本编辑器的选择

    这是一个官方文档的补充版,以官方文档的快速上手:制作第一个游戏为基础,整合了其他文档内容,同时添加一下自己学习文档的理解,不仅能复习一下自己学到的知识,也帮助新人学习开发更加简单,好了,话不多说,进入 ...

  5. Cocos Creator - 制作精灵帧动画

    本文档主要根据官方文档上的图集资源.动画系统.音乐和音效等章节内容进行综合使用. 1.制作图集 制作图集有两种工具: TexturePacker Zwoptex(只能在Windows上使用) 我这里使 ...

  6. cocos creator制作游戏实战-Flak Cannon(二)

    上一文主要介绍了游戏的设计思想和一些cocos creator的基本操作,本文将继续讲述下游戏的具体制作. 本文所涉及的知识点包括,预制体(prefab)的制作,按角度移动物体等 一.制作飞机,战舰, ...

  7. 使用Cocos Creator制作试玩广告(PlayableAd)

    一.制作的试玩广告平台要求 1.zip包大小必须小于5M 2.资源文件必须处理成base64 3.代码依赖的素材放在本地,不能存在在线请求的资源 二.制作环境和工具 环境:Mac(版本10.15.1) ...

  8. cocos creator 制作作砸金蛋

    cocos Creator 引擎2.4.3 编辑工具HBuild X 目标:制作砸金蛋 项目目录结构: 项目最终效果 给出的代码: eggAlert.js绑定在eggAlert上,代码如下: cc.C ...

  9. tilemap 菱形_使用Cocos creator制作【治愈七夕】-音乐游戏图形api绘制跳舞的线

    专栏概述及目录:笑苍天Smile:专栏概述及目录​zhuanlan.zhihu.com 游戏截图: 游戏地址:微信扫一扫 游戏源码 游戏技术:前端引擎-Cocos creator,语言-Ts. 写作目 ...

  10. cocos creator制作游戏实战-Flak Cannon(一)

    首先设定游戏设定 游戏类垄:射击类 游戏描述:玩家守卫战舰,发射炮弹防御来自飞机的自杀袭击 玩家目标:不要让地方的飞机袭击战舰并消灭飞机 敌人描述:飞机从屏幕上方随机位置飞向战舰,从下方飞出,它们的目 ...

最新文章

  1. jenkins环境搭建
  2. fft qt 代码_最简洁的FFT代码(C++实现)
  3. CCF202012-5 星际旅行【线段树】(100分题解链接)
  4. [julia]本地离线安装package
  5. 用SQL语言操作数据
  6. 阿里架构师墙裂推荐Java岗实战文档:Spring全家桶+Docker+Redis
  7. vue事件修饰符prevent、self、native
  8. three.js自定义材质各向异性
  9. 高并发场景设计与解决方案
  10. 2023年最新微信小程序获取用户openid、头像昵称的填写能力和方法原生写法
  11. NO.ONE进程、进程、线程、线程——阿古兽高级超级终极究极进化暴龙兽喷火暴龙兽机械暴龙兽战斗暴龙兽
  12. Ubuntu安装有线网卡驱动
  13. 字符串的最大递增子串
  14. 基于java房屋租赁系统计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署
  15. 无线充QI认证有什么用?
  16. docusign文档打不开_关于docusignapi:Docusign签名URL-显示复合模板的文档1
  17. 继电保护计算机化,电力系统继电保护现状和发展.doc
  18. 港股暴跌719点 成交额超过2000亿港元
  19. 【vim】系统剪切板、vim寄存器之间的复制粘贴操作命令?系统剪切板中的内容复制粘贴到命令行?vim文本中复制粘贴到命令行
  20. linux读取iso,Linux下iso文件的读取,创建

热门文章

  1. cudnn下载注意事项
  2. 图片如何转PDF格式?这些方法值得收藏
  3. 斐讯w3固件下载_【2019.11.13更新】斐讯 K3 openwrt固件
  4. 话单数据仓库搭建(1)- 数仓概念及数据采集
  5. 开源字体_开源字体的前5大资源
  6. 2022年全国大学生电子设计大赛省赛A题
  7. Audio Jungle超级音效库精选影视片头【10月18日更新】
  8. 数学建模综合评价方法
  9. 服务器处理器以及选择
  10. 公安如何通过大数据破案?知识图谱实现公安情报分析(人工智能大数据公司)