[英]Placing walls/roads in a grid-based game
我正在制作一个基于网格的游戏。
在这个游戏中你可以放置建筑物、墙壁、道路。
当某物被放置在网格中时,我将单元格 id 设置为该对象 id。 到目前为止一切顺利,这一切都有效。
当我需要对我的墙壁/道路(通过拖动鼠标放置)进行位掩码时,我的问题就出现了。
目前我进行位掩码的方式是检查所有相邻cells
是否匹配,并相应地设置正确的模型。 即,如果西部和北部匹配,我会创建一个弯曲。
当我将墙壁放在已被占用的cells
顶部时,我的问题就出现了。 我仍然想以图形方式显示墙(着色为红色),但我不能覆盖该cell
当前的内容。
这意味着我的位掩码不起作用,因为墙不在实际grid
,它仅在世界空间中。
墙壁将单元格设置为id 0
,房屋将单元格设置为id 1
。 我无法覆盖房屋单元中的 id,因此位掩码(正确)最终会破坏。
如何管理这样的场景?
细胞:
public class Cell
{
public int x, z;
public int id;
public GameObject go
}
您可以将图层添加到网格,并仅合并同一图层上的单元格。
如果您想避免重叠单元格,请检查任何其他图层中是否已存在某些内容。 从其他层查找值也将允许您交叉合并单元格,尽管它增加了很多复杂性。
这是一些伪代码:
enum Layer {
Building,
Wall,
Road
}
struct Point {
int x,y;
}
class Cell {
Point point;
int value;
}
class Cells {
Dictionary<Point, Cell> cells; // mapping position to the cell object
Cells(int width, int height) {
for x in width
for y in height
Point point = new Point(x,y);
cells.Add(point, new Cell(point, default))
}
Cell Get(Point point) => cells[point];
}
class Grid {
Dictionary<Layer, Cells> layers; // mapping the layer enum to the cells group object
Grid(int width, int height) {
foreach layer in Layer
layers.Add(layer, new Cells(width, height));
}
Cells GetLayer(Layer layer) {
return layers[layer]
}
int GetValue(Point point, Layer layer) {
return GetLayer(layer).Get(point).value;
}
bool IsValueInAllLayers(Point point, int value) {
foreach layer in Layers
if layer.Get(point).value != value
return false;
return true;
}
}
// program entry point
void Main() {
// construct a 10x10 grid
Grid grid = new Grid(10, 10);
Point mousePoint = new Point(2,2);
// to place a building all layers have to be empty (0)
if(grid.IsValueInAllLayers(mousePoint, 0)) {
grid.SetValue(mousePoint, Layer.Building);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.