我想知道你是否可以在这里帮助我,因为我有点难以用碰撞方法在屏幕上绘制一个新的瓷砖。单击鼠标时,我只是想在屏幕上绘制图块。注意这个图块需要在我创建的网格内。
这是我的关卡代码,当我创建一个看起来像这样的文本文件时,它会将图块绘制到屏幕上
...GGGGG....
....dddd....
............
代码
public class Level
{
private Tile[,] tiles;
ContentManager content;
public Vector2 startPosition;
public Level(ContentManager newContent, Stream fileStream)
{
content = newContent;
LoadTiles(fileStream);
}
private void LoadTiles(Stream fileStream)
{
int width;
List<string> lines = new List<string>();
using (StreamReader reader = new StreamReader(fileStream))
{
string line = reader.ReadLine();
width = line.Length;
while (line != null)
{
lines.Add(line);
if (line.Length != width)
throw new Exception(String.Format("Line {0} isn't the right length, sort it out, good debug method here ;)", lines.Count));
line = reader.ReadLine();
}
}
tiles = new Tile[width, lines.Count];
for (int y = 0; y < Height; ++y)
for (int x = 0; x < Width; ++x)
{
char tileType = lines[y][x];
tiles[x, y] = LoadTile(tileType, x, y);
}
}
private Tile LoadTile(char tileType, int x, int y)
{
switch (tileType)
{
case '.':
return new Tile(null, TileCollision.Passable);
// Grass
case 'g':
return LoadTile("Grass", TileCollision.Impassable);
// Dirt
case 'd':
return LoadTile("Dirt", TileCollision.Impassable);
// Log + Leaves || Log = "L" Leaves "l"
case 'L':
return LoadTile("Log", TileCollision.Passable);
case 'l':
return LoadTile("Leaves", TileCollision.Passable);
// Stone
case 'S':
return LoadTile("Ore_Stone", TileCollision.Impassable);
// Coal
case 'c':
return LoadTile("Ore_Coal", TileCollision.Impassable);
// Door
case '!':
return LoadTile("Door_Top", TileCollision.Door);
case '1':
return LoadTile("Door_Bottom", TileCollision.Door);
case 'b':
// Brick
return LoadTile("Brick", TileCollision.Passable);
// Player spawn 'S'
case 's':
return LoadStartTile(x, y);
default:
throw new NotSupportedException(String.Format("Unsupported tile type character '{0}' at position {1}, {2}.", tileType, x, y));
}
}
private Tile LoadStartTile(int x, int y)
{
startPosition = new Vector2(x * 32, y * 32);
return new Tile(null, TileCollision.Passable);
}
private Tile LoadTile(string name, TileCollision collision)
{
return new Tile(content.Load<Texture2D>("Blocks/" + name), collision);
}
public Rectangle GetBounds(int x, int y)
{
return new Rectangle(x * Tile.Width, y * Tile.Height, Tile.Width, Tile.Height);
}
public int Width
{
get { return tiles.GetLength(0); }
}
public int Height
{
get { return tiles.GetLength(1); }
}
public TileCollision GetCollision(int x, int y)
{
// Prevent escaping past the level ends.
if (x < 0 || x >= Width)
return TileCollision.Passable;
// Allow jumping past the level top and falling through the bottom.
if (y < 0 || y >= Height)
return TileCollision.Passable;
return tiles[x, y].Collision;
}
public void Draw(SpriteBatch spriteBatch)
{
DrawTiles(spriteBatch);
}
public void DrawTiles(SpriteBatch spriteBatch)
{
for (int y = 0; y < Height; ++y)
{
for (int x = 0; x < Width; ++x)
{
Texture2D texture = tiles[x, y].Texture;
if (texture != null)
{
Vector2 position = new Vector2(x, y) * Tile.Size;
spriteBatch.Draw(texture, position, Color.White);
}
}
}
}
}