2012-08-28 67 views
0

我想知道您是否能夠幫助我,因爲我有點卡住用碰撞方法在屏幕上繪製新瓷磚。我只是試圖在點擊鼠標時在屏幕上繪製一個圖塊。請注意,此圖塊需要位於我創建的網格內。用鼠標點擊創建新瓷磚

這裏是我的等級代碼,當我創建一個文本文件,它看起來像這樣

...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); 
       } 
      } 
     } 
    } 
} 
+0

你確切的問題是什麼?如何將鼠標座標轉換爲圖塊索引,或者如何獲取鼠標信息,或者如何繪製圖塊?這很不清楚。 – Dervall

+1

你應該更新這個問題是一個真正的問題,這樣Jim Perry可以獲得回答。 – jcollum

+2

請留下問題,以便讓同樣問題的人也受益,並接受答案。 – hvd

回答

3

類似的東西繪製圖塊在屏幕上:

MouseState mouseState = Mouse.GetState(); 

int x = (int)(mouseState.X/Tile.Size); 
int y = (int)(mouseState.Y/Tile.Size); 

tiles[x, y] = //whatever tile type you want here 
+0

謝謝!現在想出來。 #JimTheBoss。 – user1631026