1 Replies - 2155 Views - Last Post: 05 September 2012 - 07:41 PM

#1 Kizzyiee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-August 12

Creating new Tiles with mouseclick.

Posted 28 August 2012 - 10:39 AM

Hello forum, I was wondering if you could help me out here as I'm a little stuck with drawing a new tile onto the screen with a collision method. I'm simply trying to draw a tile on the screen when the mouse is clicked. NOTE this tile needs to be within the grid that I have created. :stupid:

Here is my Level code that draws the tiles onto the screen when I create a text file that looks something like this

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



Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Creating new Tiles with mouseclick.

#2 LiberLogic969  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 85
  • Joined: 03-September 12

Re: Creating new Tiles with mouseclick.

Posted 05 September 2012 - 07:41 PM

Hello,

If you want this tile to be added to the array then I believe you just need to divide the mouse position with the width/height of your Tiles.
int x = (int)(Mouse.x / TileWidth);
int y = (int)(Mouse.y / TileHeight);

tiles[x, y] = newTile;



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1