使用结构从 class 获取信息
Getting information from a class using a struct
我正在尝试在创建像 tilemap 这样的二维网格时最小化 tile 对象的大小。我创建了一个 short[] 数组,每个 [y,x] 位置对应于一个 tile 的 id。为此,我创建了一个名为 TileType 的 class,并让一个结构体 Tile 根据其 id 从 TileType 访问有关其自身的信息。像这样:
struct Tile
{
short typeId;
public TileType Type
{
get
{
return TileType.Get(typeId);
}
set
{
typeId = value.Id;
}
}
}
class TileType
{
public short Id;
public string Name;
public Texture2D Texture;
public Rectangle TextureSource;
public bool IsObstacle;
static List<TileType> types;
static TileType()
{
types = new List<TileType>();
var type = new TileType();
type.Name = "Dirt";
//initialize here
types.Add(type);
}
public static TileType Get(short id)
{
return types[id];
}
}
我通过阅读 post 了解如何有效地为这样的地图存储数据找到了这一点。我没有写这个,它只是一个例子。但我的问题是如何使用这种方法在屏幕上绘制一个图块?我将设置一种方式,使纹理对应于图块图集中的源矩形 (TextureSource)。但我不明白我将如何实际绘制它。 IE 绘制(Tile.Type.Id)?但是Id只是一个简短的。
首先,你应该修复初始化中的一个错误——当你创建一个类型时,你应该在其中设置一个标识符。像这样:
var type = new TileType();
type.Id = 0; // This is unique identifier that we are using in Get method.
type.Name = "Dirt";
type.Texture = new Texture2D(...); //Here you assign your texture for Dirt tile
//Initialize position and size for your texture.
type.TextureSource = new Rectangle(dirtStartX, dirtStartY, dirtWidth, dirtHeight);
types.Add(type);
type = new TileType();
type.Id = 0; // This is unique identifier that we are using in Get method.
type.Name = "Water";
type.Texture = new Texture2D(...); //Here you assign your texture for Dirt tile
//Initialize position and size for your texture.
type.TextureSource = new Rectangle(waterStartX, waterStartY, waterWidth, waterHeight);
types.Add(type);
之后就可以使用标识符获取方法了。
我将解释在屏幕上绘制所有图块的主要思想(这不是一个有效的代码,但它显示了你应该做什么。它很简单:)):
for (int id=0; id<TileTypeCount; id++)
{
TileType tileType = TileType.Get(id); //Get tile by its identifier.
//Now we have current texture and rectangle (position). Draw it
DrawRectangleWithTexture(tileType.Rectangle, tileType.Texture);
}
DrawRectangleWithTexture 的实现取决于您使用的开发环境。无论如何,在这个函数中你有绘制图像的所有信息:
矩形用于存储有关图像位置和大小的信息。
纹理只是一张你应该画的图。
我正在尝试在创建像 tilemap 这样的二维网格时最小化 tile 对象的大小。我创建了一个 short[] 数组,每个 [y,x] 位置对应于一个 tile 的 id。为此,我创建了一个名为 TileType 的 class,并让一个结构体 Tile 根据其 id 从 TileType 访问有关其自身的信息。像这样:
struct Tile
{
short typeId;
public TileType Type
{
get
{
return TileType.Get(typeId);
}
set
{
typeId = value.Id;
}
}
}
class TileType
{
public short Id;
public string Name;
public Texture2D Texture;
public Rectangle TextureSource;
public bool IsObstacle;
static List<TileType> types;
static TileType()
{
types = new List<TileType>();
var type = new TileType();
type.Name = "Dirt";
//initialize here
types.Add(type);
}
public static TileType Get(short id)
{
return types[id];
}
}
我通过阅读 post 了解如何有效地为这样的地图存储数据找到了这一点。我没有写这个,它只是一个例子。但我的问题是如何使用这种方法在屏幕上绘制一个图块?我将设置一种方式,使纹理对应于图块图集中的源矩形 (TextureSource)。但我不明白我将如何实际绘制它。 IE 绘制(Tile.Type.Id)?但是Id只是一个简短的。
首先,你应该修复初始化中的一个错误——当你创建一个类型时,你应该在其中设置一个标识符。像这样:
var type = new TileType();
type.Id = 0; // This is unique identifier that we are using in Get method.
type.Name = "Dirt";
type.Texture = new Texture2D(...); //Here you assign your texture for Dirt tile
//Initialize position and size for your texture.
type.TextureSource = new Rectangle(dirtStartX, dirtStartY, dirtWidth, dirtHeight);
types.Add(type);
type = new TileType();
type.Id = 0; // This is unique identifier that we are using in Get method.
type.Name = "Water";
type.Texture = new Texture2D(...); //Here you assign your texture for Dirt tile
//Initialize position and size for your texture.
type.TextureSource = new Rectangle(waterStartX, waterStartY, waterWidth, waterHeight);
types.Add(type);
之后就可以使用标识符获取方法了。
我将解释在屏幕上绘制所有图块的主要思想(这不是一个有效的代码,但它显示了你应该做什么。它很简单:)):
for (int id=0; id<TileTypeCount; id++)
{
TileType tileType = TileType.Get(id); //Get tile by its identifier.
//Now we have current texture and rectangle (position). Draw it
DrawRectangleWithTexture(tileType.Rectangle, tileType.Texture);
}
DrawRectangleWithTexture 的实现取决于您使用的开发环境。无论如何,在这个函数中你有绘制图像的所有信息:
矩形用于存储有关图像位置和大小的信息。
纹理只是一张你应该画的图。