• 地图之战争迷雾/地图算法/自动导航(一)


    战争迷雾

    TiledMap 创建黑色覆盖块,然后使用碰撞组件,控制黑色块的显示和隐藏

    地图算法

    在有些游戏中,地图需要随机生成,比如游戏中的迷宫等,这就需要地图生成的算法;在角色扮演类游戏中,角色需要在地图中找到一条合适的路径,这就需要寻路算法,最常用的寻路算法就是A星路径搜索算法

    Roguelike算法(地图生成)

    Roguelike是角色扮演游戏(RPG)的一个子类(Roguelike-RPG),其原型——《Rogue》是20世纪80年代初,由Michael Toy和Glenn Wichman两位软件工程师共同在UNIX系统上开发,并在大型机上运行的游戏,Roguelike是角色扮演游戏(RPG)的一个子类(Roguelike-RPG),其原型——《Rogue》是20世纪80年代初,由Michael Toy和Glenn Wichman两位软件工程师共同在UNIX系统上开发,并在大型机上运行的游戏,在2008年的国际Roguelike发展会议上Roguelike游戏有了明确的定义,它的特点包括:

    1)生成随机性。每一次新开局游戏都会随机生成游戏场景、敌人、宝物等不同事物。这样玩家的每一次冒险历程也都将是独一无二,不可复制的。

    2)进程单向性。存档功能的唯一作用就是记录你当前的游戏进度,每当存档被读取时,对应的进度就会被清空,直到你进行下一次存档。

    3)不可挽回性。在大多数Roguelike游戏中,每一个角色只有一次生命,一个角色的死亡意味着玩家将永远失去该角色。无论你是主角、敌人、物品还是场景。在很多玩家眼中,这正是Roguelike的乐趣所在。

    4)游戏非线性。严谨而不失灵活性的游戏规则,使游戏具备了很高的自由度,在这类游戏中,玩家可以发挥想象力,利用各种方法实现任何他们想做的事情,或合乎常理,或匪夷所思,目的只在于解决他们在游戏中遇到的问题。

    5)系统复杂性。可能会在一款游戏中包括多到无法估量的元素,例如地质、气候和生物分布,以及精细到皮肤、肌肉、血液、骨骼和脂肪的战斗系统,甚至战损痊愈后会留下伤疤以及后遗症。在有些游戏里则可能包括数百种的死亡原因,数千种的生物,数万种的物品。

    Roguelike 地图生成算法

    地图生成算法是这样一个黑盒,它需要你输入地图的限制规则和大小等信息,它的输出是具体的地图数据,具体到Roguelike游戏的地图生成算法,它有如下特点:

    1)要同时有开放的房间和走廊,房间在Roguelike游戏中起着至关重要的作用,开放的空间可以让玩家有空间进行战斗,同时房间也可以通过不同的装饰风格来增强游戏场景的表现力。同时,这个地牢不应该完全由房间组成,玩家需要在游戏过程中有不同的感受,走廊会让他们有封闭感,同时增加游戏的策略性。

    2)地图生成中部分参数是可调的,由于关卡的难度要有梯度,所以生成规则应该是可调的,理想的做法是将生成器的一些参数设置成可调,可以通过同一套代码生成不同风格和感觉的地牢。

    3)地图不是完美的,完美的地图意味着两点之间只有唯一的一条通路,这样玩起来缺少乐趣。当玩家遇到一个死胡同的时候,必须要回溯到之前的路线去,然后寻找新的可探索的地方。游戏是一个决定和做出不同选择的过程。因此,过于完美的地图不能让游戏变得更有趣。

    生成地图的具体步骤包括:

    1)随机生成房间,保证房间之间不相互覆盖。

    2)计算如何连接各个房间。

    3)把房间之外的空地用迷宫填满,移除掉死胡同。

    4)连接相连的迷宫和房间,增加少量连接。首先是生成房间,这个过程需要注意的是要检查房间之间不相互重叠,每一个房间的生成过程包括随机生成房间左下角坐标和尺寸,判断重叠与否,创建房间。需要注意的是横纵方向个数要保证为奇数。然后是生成迷宫,生成迷宫的过程可以抽象为树的生成,生成的过程为连接每一个节点。首先判断起点上下左右是否在一个方向有连接,不存在就需要将节点放入列表中,如果第一步完成后列表不为空,则将节点向列表中的某一个方向移动两格并将移动后的坐标压入栈中,重复第一步。如果列表为空,则弹出栈顶元素,直到栈为空时。对于每一个走廊的块,如果其四个方向中有3个为空,则把它删除,就可以移除死胡同了。连接迷宫和房间,需要把每个区域联系起来,首先随机找到一个点,连通合并两个区域,然后删除p以外所有能连通两个区域的点,继续第一步,直到所有区域连通,为所有连通区域创建走廊,使所有房间可以连通。

    实现

    1、地图参数初始化
    1. //计算房间数量范围
    2. calculateRoomSize(size, cell)
    3. {
    4. var max = Math.floor((size/cell) * 0.8);
    5. var min = Math.floor((size/cell) * 0.25);
    6. if (min < 2) {
    7. min = 2;
    8. }
    9. if (max < 2) {
    10. max = 2;
    11. }
    12. return [min, max];
    13. },
    14. //初始化地图
    15. initMap()
    16. {
    17. //地图宽高的格子
    18. this._width = 96
    19. this._height = 64
    20. this._options = {
    21. cellWidth: 10, //单元格宽
    22. cellHeight: 10, //单元格高
    23. roomWidth: [2,10], //房间个数范围
    24. roomHeight: [2,7], //房间个数范围};
    25. if (! this._options.hasOwnProperty("roomWidth")) {
    26. this._options["roomWidth"] = this.calculateRoomSize(
    27. this._width, this._options["cellWidth"]);
    28. }
    29. if (! this._options.hasOwnProperty("roomHeight")) {
    30. this._options["roomHeight"]=this.calculateRoomSize(
    31. this._height, this._options["cellHeight"]);
    32. }
    33. },
    34. //入口函数
    35. onLoad()
    36. {
    37. //初始化
    38. this.initMap()
    39. //地图生成
    40. this.mapGenerate()
    41. //绘制地图
    42. this.drawMap()
    43. },

    地图生成主要根据如上三步进行,每一步都有一些需要注意的地方。当然,在做这些之前,首先需要进行地图数据的初始化,在这里首先初始化map数组,这是一个二维数组。用来存储最后地图表示的数据。首先把这个数组中的每一个值都初始化为0,也就是所有的位置都是空地,然后初始化房间和房间联通的数组

    2、地图的初始化和生成步骤
    1. //设置map数值,初始化为一个值
    2. fillMap(value) {
    3. var map = [];
    4. for (var i = 0; i < this._width; i ++){
    5. map.push([]);
    6. for (var j = 0; j < this._height; j ++)
    7. {
    8. map[i].push(value);
    9. }
    10. }
    11. return map;
    12. },
    13. //初始化房间的数量
    14. initRooms() {
    15. for (var i = 0; i < this._options.cellWidth; i++) {
    16. this.rooms.push([]);
    17. for(var j = 0; j < this._options.cellHeight; j++) {
    18. this.rooms[i].push({"x":0, "y":0, "width":0, "height":0,
    19. "connections":[], "cellx":i, "celly":j});
    20. }
    21. }
    22. },
    23. //地图生成过程
    24. mapGenerate(){
    25. this.map = this.fillMap(0); //初始化地图数据
    26. this.rooms = []; //房间
    27. this.connectedCells = []; //连通的房间
    28. //初始化房间
    29. this.initRooms()
    30. //连接房间
    31. this.connectRooms()
    32. this.connectUnconnectedRooms()
    33. //创建房间
    34. this.createRooms()
    35. //创建走廊
    36. this.createCorridors()
    37. },

    在initRooms函数里,只是初始化了房间数组,并没有创建房间的数据。生成房间的过程从connectRooms开始,首先连接房间,然后遍历一下房间,看看有没有“被遗忘”的角落,一定要确保所有房间都是连通的,这样才能避免死角的出现,最后才生成房间的数据,调用createRooms生成房间数据

    3、生成房间和连接房间
    1. //创建房间
    2. createRooms() {
    3. var w = this._width;
    4. var h = this._height;
    5. var cw = this._options.cellWidth;
    6. var ch = this._options.cellHeight;
    7. var cwp = Math.floor(this._width / cw);
    8. var chp = Math.floor(this._height / ch);
    9. //房间属性
    10. var roomw;
    11. var roomh;
    12. var roomWidth = this._options["roomWidth"];
    13. var roomHeight = this._options["roomHeight"];
    14. var sx;
    15. var sy;
    16. var otherRoom;
    17. //遍历房间中每一个点
    18. for (var i = 0; i < cw; i++) {
    19. for (var j = 0; j < ch; j++) {
    20. sx = cwp * i;
    21. sy = chp * j;
    22. if (sx == 0) {
    23. sx = 1;
    24. }
    25. if (sy == 0) {
    26. sy = 1;
    27. }
    28. //房间宽高,随机获得
    29. roomw = GlobalHandle.getRandomInt(roomWidth[0], roomWidth[1]);
    30. roomh = GlobalHandle.getRandomInt(roomHeight[0], roomHeight[1]);
    31. if (j > 0) {
    32. otherRoom = this.rooms[i][j-1];
    33. while (sy - (otherRoom["y"] + otherRoom["height"] ) < 3) {
    34. sy++;
    35. }
    36. }
    37. if (i > 0) {
    38. otherRoom = this.rooms[i-1][j];
    39. while(sx - (otherRoom["x"] + otherRoom["width"]) < 3) {
    40. sx++;
    41. }
    42. }
    43. var sxOffset = Math.round(GlobalHandle.getRandomInt(
    44. 0, cwp - roomw)/2);
    45. var syOffset = Math.round(GlobalHandle.getRandomInt(
    46. 0, chp - roomh)/2);
    47. while (sx + sxOffset + roomw >= w) {
    48. if(sxOffset) {
    49. sxOffset--;
    50. } else {
    51. roomw--;
    52. }
    53. }
    54. while (sy + syOffset + roomh >= h) {
    55. if(syOffset) {
    56. syOffset--;
    57. } else {
    58. roomh--;
    59. }
    60. }
    61. sx = sx + sxOffset;
    62. sy = sy + syOffset;
    63. this.rooms[i][j]["x"] = sx;
    64. this.rooms[i][j]["y"] = sy;
    65. this.rooms[i][j]["width"] = roomw;
    66. this.rooms[i][j]["height"] = roomh;
    67. //设置地图
    68. for (var ii = sx; ii < sx + roomw; ii++) {
    69. for (var jj = sy; jj < sy + roomh; jj++) {
    70. this.map[ii][jj] = 1;
    71. }
    72. }
    73. }
    74. }
    75. },

    地图生成结果

    小方块组成的即“房间”。可以发现,房间之间都互相独立,并不能互相连通,这就是后续我们要做的,即生成走廊。首先在数据上,参考之前生成的房间连通数据,生成走廊,随后在绘制走廊的函数里更新map数据。

    1. //绘制走廊,设置map值
    2. drawCorridor(startPosition, endPosition) {
    3. var xOffset = endPosition[0] - startPosition[0];
    4. var yOffset = endPosition[1] - startPosition[1];
    5. var xpos = startPosition[0];
    6. var ypos = startPosition[1];
    7. var tempDist;
    8. var xDir;
    9. var yDir;
    10. var move;
    11. var moves = [];
    12. var xAbs = Math.abs(xOffset);
    13. var yAbs = Math.abs(yOffset);
    14. var percent = Math.random();
    15. var firstHalf = percent;
    16. var secondHalf = 1- percent;
    17. xDir = xOffset > 0 ? 2 : 6;
    18. yDir = yOffset > 0 ? 4 : 0;
    19. if (xAbs < yAbs) {
    20. tempDist = Math.ceil(yAbs * firstHalf);
    21. moves.push([yDir, tempDist]);
    22. moves.push([xDir, xAbs]);
    23. tempDist = Math.floor(yAbs * secondHalf);
    24. moves.push([yDir, tempDist]);
    25. } else {
    26. tempDist = Math.ceil(xAbs * firstHalf);
    27. moves.push([xDir, tempDist]);
    28. moves.push([yDir, yAbs]);
    29. tempDist = Math.floor(xAbs * secondHalf);
    30. moves.push([xDir, tempDist]);
    31. }
    32. this.map[xpos][ypos] = 2;
    33. while (moves.length > 0) {
    34. move = moves.pop();
    35. while (move[1] > 0) {
    36. xpos += GlobalHandle.DIRS[8][move[0]][0];
    37. ypos += GlobalHandle.DIRS[8][move[0]][1];
    38. this.map[xpos][ypos] = 2;
    39. move[1] = move[1] -1;
    40. }
    41. }
    42. },
    43. createCorridors() {
    44. //创建走廊
    45. var cw = this._options.cellWidth;
    46. var ch = this._options.cellHeight;
    47. var room;
    48. var connection;
    49. var otherRoom;
    50. var wall;
    51. var otherWall;
    52. for (var i = 0; i < cw; i++) {
    53. for (var j = 0; j < ch; j++) {
    54. room = this.rooms[i][j];
    55. for (var k = 0; k < room["connections"].length; k++) {
    56. connection = room["connections"][k];
    57. otherRoom = this.rooms[connection[0]][connection[1]];
    58. //获得墙体数量
    59. if (otherRoom["cellx"] > room["cellx"]) {
    60. wall = 2;
    61. otherWall = 4;
    62. } else if (otherRoom["cellx"] < room["cellx"]) {
    63. wall = 4;
    64. otherWall = 2;
    65. } else if(otherRoom["celly"] > room["celly"]) {
    66. wall = 3;
    67. otherWall = 1;
    68. } else if(otherRoom["celly"] < room["celly"]) {
    69. wall = 1;
    70. otherWall = 3;
    71. }
    72. this.drawCorridor(this.getWallPosition(room, wall),
    73. this.getWallPosition(otherRoom, otherWall));
    74. }
    75. }
    76. }
    77. },

    生成地图数据后,接下来的任务就是把这个地图绘制出来,在drawMap中绘制,根据map里每个元素值对应的不同类型渲染不同颜色的方块。

    1. //绘制地图
    2. drawMap()
    3. {
    4. for (var i = 0; i < this._width; i ++){
    5. for (var j = 0; j < this._height; j ++) {
    6. if(this.map[i][j] == 1){ //房间地图格
    7. var ctx = this.mapLayer.getComponent(cc.Graphics)
    8. ctx.fillColor.fromHEX('#FF0000');
    9. ctx.rect((i) * this._options.cellWidth, (j) *
    10. this ._options.cellHeight, this._options.cellWidth, this._options.
    11. cellHeight)
    12. ctx.fill()
    13. ctx.stroke()
    14. }
    15. else if(this.map[i][j] == 2){ //门口地图格
    16. var ctx = this.mapLayer.getComponent(cc.Graphics)
    17. ctx.fillColor.fromHEX('#7B68EE');
    18. ctx.rect((i) * this._options.cellWidth, (j) * this._options.
    19. cellHeight, this._options.cellWidth, this._options.cellHeight)
    20. ctx.fill()
    21. }
    22. else if(this.map[i][j] == 3) {//走廊地图格
    23. var ctx = this.mapLayer.getComponent(cc.Graphics)
    24. ctx.fillColor.fromHEX('#00FF00');
    25. ctx.rect((i) * this._options.cellWidth, (j) * this._options.
    26. cellHeight, this._options.cellWidth, this._options.cellHeight)
    27. ctx.fill()
    28. }
    29. }
    30. }
    31. },

    结果

    A星算法

    ,A星搜索算法用来实现敌人的智能运动,比如敌人巡逻或者角色寻径

  • 相关阅读:
    5. 标准库类型string
    KY91 String Matching
    C++标准库之:IO库
    logging日志及其使用代码
    PLC数据采集网关是如何应用的?-天拓四方
    生产订单自动下达
    FreeRTOS入门笔记——FreeRTOS介绍
    指纹面容识别登录流程概述
    (实战)WebApi第10讲:Swagger配置、RESTful与路由重载
    实战EDA电子设计自动化经典入门模型VHDL代码编写(含代码解释)中上篇--2-4译码器 信号十分频
  • 原文地址:https://blog.csdn.net/qq_38248841/article/details/139415607