Skip to main content
Bumped by Community user
added 17 characters in body
Source Link
class TileMap
{
    Player player = new Player(null, Vector2.Zero);
    private List<Texture2D> tileList = new List<Texture2D>();
    MapCell[,] mapCell;

    public const int TILE_WIDTH = 64;
    public const int TILE_HEIGHT = 64;
    private bool passable = true;

    public TileMap(int[,] exisitingMap)
    {
        //initialise this to a new multidimensional array;
        mapCell = new MapCell[exisitingMap.GetLength(0), exisitingMap.GetLength(1)];
        
        // x always starts on one 
        for (int x = 0; x < mapCell.GetLength(1); x++)
        {
            for (int y = 0; y < mapCell.GetLength(0); y++)
            {
                mapCell[y, x] = new MapCell(exisitingMap[y, x]);
            }
        }       

     }

    public void loadTextureFiles(ContentManager content, params string[] fileNames)
    {
        Texture2D tileTexture;
        foreach (string fileName in fileNames)
        {
            tileTexture = content.Load<Texture2D>(fileName);
            tileList.Add(tileTexture);
        }
    }

    public void checkForCollision()
    {
        int tileX = (int)(player.Position.X) / TILE_WIDTH;
        int tileY = (int)(player.Position.Y) / TILE_HEIGHT;

        int left = player.playerRect.Left / TILE_WIDTH;
        int top = player.playerRect.Top / TILE_HEIGHT;
        int right = (int)Math.Ceiling((float)player.playerRect.Right / TILE_WIDTH) - 1;
        int bottom = (int)Math.Ceiling((float)player.playerRect.Bottom / TILE_HEIGHT) - 1;

          for(int y = top; y<= bottom; ++y)
        {
            for (int x = left; x <= right; ++x)
            {
                if (new Rectangle(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT).Intersects(player.playerRect))
                {
                    Console.WriteLine("collision");
                  if (mapCell[top, tileX].TileID == 4)
                    {
                        Console.WriteLine("tile 4 has been hit");
                        passable = false;
                    }
                } 
            }
        }
                     
    }
    public void Update(GameTime gameTime)
    {
        checkForCollision();

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < mapCell.GetLength(1); x++)
        {
            for (int y = 0; y < mapCell.GetLength(1); y++)
            {
                // setting the index to the tile ID
                int index = mapCell[y,x].TileID;
               

                // checking if there is a tile
                if(index > 0)
                {
                    // start from the first index.
                    Texture2D texture = tileList[index -1 ];
                    spriteBatch.Draw(texture, new Rectangle(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT), Color.White);
                }       
            }
        }   
    }
class TileMap
{
    Player player = new Player(null, Vector2.Zero);
    private List<Texture2D> tileList = new List<Texture2D>();
    MapCell[,] mapCell;

    public const int TILE_WIDTH = 64;
    public const int TILE_HEIGHT = 64;
    private bool passable = true;

    public TileMap(int[,] exisitingMap)
    {
        //initialise this to a new multidimensional array;
        mapCell = new MapCell[exisitingMap.GetLength(0), exisitingMap.GetLength(1)];
        
        // x always starts on one 
        for (int x = 0; x < mapCell.GetLength(1); x++)
        {
            for (int y = 0; y < mapCell.GetLength(0); y++)
            {
                mapCell[y, x] = new MapCell(exisitingMap[y, x]);
            }
        }       

     }

    public void loadTextureFiles(ContentManager content, params string[] fileNames)
    {
        Texture2D tileTexture;
        foreach (string fileName in fileNames)
        {
            tileTexture = content.Load<Texture2D>(fileName);
            tileList.Add(tileTexture);
        }
    }

    public void checkForCollision()
    {
        int tileX = (int)(player.Position.X) / TILE_WIDTH;
        int tileY = (int)(player.Position.Y) / TILE_HEIGHT;

        int left = player.playerRect.Left / TILE_WIDTH;
        int top = player.playerRect.Top / TILE_HEIGHT;
        int right = (int)Math.Ceiling((float)player.playerRect.Right / TILE_WIDTH) - 1;
        int bottom = (int)Math.Ceiling((float)player.playerRect.Bottom / TILE_HEIGHT) - 1;

          for(int y = top; y<= bottom; ++y)
        {
            for (int x = left; x <= right; ++x)
            {
                if (new Rectangle(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT).Intersects(player.playerRect))
                {
                    Console.WriteLine("collision");
                  if (mapCell[top, tileX].TileID == 4)
                    {
                        Console.WriteLine("tile 4 has been hit");
                        passable = false;
                    }
                } 
            }
        }
                     
    }
    public void Update()
    {
        checkForCollision();

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < mapCell.GetLength(1); x++)
        {
            for (int y = 0; y < mapCell.GetLength(1); y++)
            {
                // setting the index to the tile ID
                int index = mapCell[y,x].TileID;
               

                // checking if there is a tile
                if(index > 0)
                {
                    // start from the first index.
                    Texture2D texture = tileList[index -1 ];
                    spriteBatch.Draw(texture, new Rectangle(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT), Color.White);
                }       
            }
        }   
    }
class TileMap
{
    Player player = new Player(null, Vector2.Zero);
    private List<Texture2D> tileList = new List<Texture2D>();
    MapCell[,] mapCell;

    public const int TILE_WIDTH = 64;
    public const int TILE_HEIGHT = 64;
    private bool passable = true;

    public TileMap(int[,] exisitingMap)
    {
        //initialise this to a new multidimensional array;
        mapCell = new MapCell[exisitingMap.GetLength(0), exisitingMap.GetLength(1)];
        
        // x always starts on one 
        for (int x = 0; x < mapCell.GetLength(1); x++)
        {
            for (int y = 0; y < mapCell.GetLength(0); y++)
            {
                mapCell[y, x] = new MapCell(exisitingMap[y, x]);
            }
        }       

     }

    public void loadTextureFiles(ContentManager content, params string[] fileNames)
    {
        Texture2D tileTexture;
        foreach (string fileName in fileNames)
        {
            tileTexture = content.Load<Texture2D>(fileName);
            tileList.Add(tileTexture);
        }
    }

    public void checkForCollision()
    {
        int tileX = (int)(player.Position.X) / TILE_WIDTH;
        int tileY = (int)(player.Position.Y) / TILE_HEIGHT;

        int left = player.playerRect.Left / TILE_WIDTH;
        int top = player.playerRect.Top / TILE_HEIGHT;
        int right = (int)Math.Ceiling((float)player.playerRect.Right / TILE_WIDTH) - 1;
        int bottom = (int)Math.Ceiling((float)player.playerRect.Bottom / TILE_HEIGHT) - 1;

          for(int y = top; y<= bottom; ++y)
        {
            for (int x = left; x <= right; ++x)
            {
                if (new Rectangle(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT).Intersects(player.playerRect))
                {
                    Console.WriteLine("collision");
                  if (mapCell[top, tileX].TileID == 4)
                    {
                        Console.WriteLine("tile 4 has been hit");
                        passable = false;
                    }
                } 
            }
        }
                     
    }
    public void Update(GameTime gameTime)
    {
        checkForCollision();

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < mapCell.GetLength(1); x++)
        {
            for (int y = 0; y < mapCell.GetLength(1); y++)
            {
                // setting the index to the tile ID
                int index = mapCell[y,x].TileID;
               

                // checking if there is a tile
                if(index > 0)
                {
                    // start from the first index.
                    Texture2D texture = tileList[index -1 ];
                    spriteBatch.Draw(texture, new Rectangle(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT), Color.White);
                }       
            }
        }   
    }
added 9 characters in body
Source Link

Currently the tile map is drawing fine but I am not sure how to proceed with collisions between the player and some of the tiles. Would it be better to specify certain tiles that the player can't move through or is there a better way to go about this? I basically want tile number 4 to not be passable when the player hits it. The issue as far as I'm aware is with the logic in the method Check for colllision in the TileMap class. When I write something to the console nothing is being displayed as if it isn't even being ran. At the minute I simply want it to state when it hits the tile 4 in the console log. After using the debugging tools the nested for loop in the check for colliision method isn't being run.

Currently the tile map is drawing fine but I am not sure how to proceed with collisions between the player and some of the tiles. Would it be better to specify certain tiles that the player can't move through or is there a better way to go about this? I basically want tile number 4 to not be passable when the player hits it. The issue as far as I'm aware is with the logic in the method Check for colllision in the TileMap class. When I write something to the console nothing is being displayed as if it isn't even being ran. At the minute I simply want it to state when it hits the tile 4 in the console log.

Currently the tile map is drawing fine but I am not sure how to proceed with collisions between the player and some of the tiles. Would it be better to specify certain tiles that the player can't move through or is there a better way to go about this? I basically want tile number 4 to not be passable when the player hits it. The issue as far as I'm aware is with the logic in the method Check for colllision in the TileMap class. At the minute I simply want it to state when it hits the tile 4 in the console log. After using the debugging tools the nested for loop in the check for colliision method isn't being run.

added 117 characters in body
Source Link
class TileMap
{
    Player player = new Player(null, Vector2.Zero);
    private List<Texture2D> tileList = new List<Texture2D>();
    MapCell[,] mapCell;

    public const int TILE_WIDTH = 64;
    public const int TILE_HEIGHT = 64;
    private bool passable = true;

    public TileMap(int[,] exisitingMap)
    {
        //initialise this to a new multidimensional array;
        mapCell = new MapCell[exisitingMap.GetLength(0), exisitingMap.GetLength(1)];
        
        // x always starts on one 
        for (int x = 0; x < mapCell.GetLength(1); x++)
        {
            for (int y = 0; y < mapCell.GetLength(0); y++)
            {
                mapCell[y, x] = new MapCell(exisitingMap[y, x]);
            }
        }       

     }

    public void loadTextureFiles(ContentManager content, params string[] fileNames)
    {
        Texture2D tileTexture;
        foreach (string fileName in fileNames)
        {
            tileTexture = content.Load<Texture2D>(fileName);
            tileList.Add(tileTexture);
        }
    }

    public void checkForCollision()
    {
        int tileX = (int)(player.Position.X) / TILE_WIDTH;
        int tileY = (int)(player.Position.Y) / TILE_HEIGHT;

        int left = player.playerRect.Left / TILE_WIDTH;
        int top = player.playerRect.Top / TILE_HEIGHT;
        int right = (int)Math.Ceiling((float)player.playerRect.Right / TILE_WIDTH) - 1;
        int bottom = (int)Math.Ceiling((float)player.playerRect.Bottom / TILE_HEIGHT) - 1;

          for(int y = top; y<= bottom; ++y)
        {
            for (int x = left; x <= right; ++x)
            {
                if (new Rectangle(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT).Intersects(player.playerRect))
                {
                    Console.WriteLine("collision");
                  if (mapCell[top, tileX].TileID == 4)
                    {
                        Console.WriteLine("tile 4 has been hit");
                        passable = false;
                    }
                } 
            }
        }
                     
    }
    public void Update()
    {
        checkForCollision();

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < mapCell.GetLength(1); x++)
        {
            for (int y = 0; y < mapCell.GetLength(1); y++)
            {
                // setting the index to the tile ID
                int index = mapCell[y,x].TileID;
               

                // checking if there is a tile
                if(index > 0)
                {
                    // start from the first index.
                    Texture2D texture = tileList[index -1 ];
                    spriteBatch.Draw(texture, new Rectangle(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT), Color.White);
                }       
            }
        }   
    }
class TileMap
{
    Player player = new Player(null, Vector2.Zero);
    private List<Texture2D> tileList = new List<Texture2D>();
    MapCell[,] mapCell;

    public const int TILE_WIDTH = 64;
    public const int TILE_HEIGHT = 64;
    private bool passable = true;

    public TileMap(int[,] exisitingMap)
    {
        //initialise this to a new multidimensional array;
        mapCell = new MapCell[exisitingMap.GetLength(0), exisitingMap.GetLength(1)];
        
        // x always starts on one 
        for (int x = 0; x < mapCell.GetLength(1); x++)
        {
            for (int y = 0; y < mapCell.GetLength(0); y++)
            {
                mapCell[y, x] = new MapCell(exisitingMap[y, x]);
            }
        }       

     }

    public void loadTextureFiles(ContentManager content, params string[] fileNames)
    {
        Texture2D tileTexture;
        foreach (string fileName in fileNames)
        {
            tileTexture = content.Load<Texture2D>(fileName);
            tileList.Add(tileTexture);
        }
    }

    public void checkForCollision()
    {
        
        int left = player.playerRect.Left / TILE_WIDTH;
        int top = player.playerRect.Top / TILE_HEIGHT;
        int right = (int)Math.Ceiling((float)player.playerRect.Right / TILE_WIDTH) - 1;
        int bottom = (int)Math.Ceiling((float)player.playerRect.Bottom / TILE_HEIGHT) - 1;

          for(int y = top; y<= bottom; ++y)
        {
            for (int x = left; x <= right; ++x)
            {
                if (new Rectangle(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT).Intersects(player.playerRect))
                {
                    Console.WriteLine("collision");
                  if (mapCell[top, tileX].TileID == 4)
                    {
                        Console.WriteLine("tile 4 has been hit");
                        passable = false;
                    }
                } 
            }
        }
                     
    }
    public void Update()
    {
        checkForCollision();

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < mapCell.GetLength(1); x++)
        {
            for (int y = 0; y < mapCell.GetLength(1); y++)
            {
                // setting the index to the tile ID
                int index = mapCell[y,x].TileID;
               

                // checking if there is a tile
                if(index > 0)
                {
                    // start from the first index.
                    Texture2D texture = tileList[index -1 ];
                    spriteBatch.Draw(texture, new Rectangle(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT), Color.White);
                }       
            }
        }   
    }
class TileMap
{
    Player player = new Player(null, Vector2.Zero);
    private List<Texture2D> tileList = new List<Texture2D>();
    MapCell[,] mapCell;

    public const int TILE_WIDTH = 64;
    public const int TILE_HEIGHT = 64;
    private bool passable = true;

    public TileMap(int[,] exisitingMap)
    {
        //initialise this to a new multidimensional array;
        mapCell = new MapCell[exisitingMap.GetLength(0), exisitingMap.GetLength(1)];
        
        // x always starts on one 
        for (int x = 0; x < mapCell.GetLength(1); x++)
        {
            for (int y = 0; y < mapCell.GetLength(0); y++)
            {
                mapCell[y, x] = new MapCell(exisitingMap[y, x]);
            }
        }       

     }

    public void loadTextureFiles(ContentManager content, params string[] fileNames)
    {
        Texture2D tileTexture;
        foreach (string fileName in fileNames)
        {
            tileTexture = content.Load<Texture2D>(fileName);
            tileList.Add(tileTexture);
        }
    }

    public void checkForCollision()
    {
        int tileX = (int)(player.Position.X) / TILE_WIDTH;
        int tileY = (int)(player.Position.Y) / TILE_HEIGHT;

        int left = player.playerRect.Left / TILE_WIDTH;
        int top = player.playerRect.Top / TILE_HEIGHT;
        int right = (int)Math.Ceiling((float)player.playerRect.Right / TILE_WIDTH) - 1;
        int bottom = (int)Math.Ceiling((float)player.playerRect.Bottom / TILE_HEIGHT) - 1;

          for(int y = top; y<= bottom; ++y)
        {
            for (int x = left; x <= right; ++x)
            {
                if (new Rectangle(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT).Intersects(player.playerRect))
                {
                    Console.WriteLine("collision");
                  if (mapCell[top, tileX].TileID == 4)
                    {
                        Console.WriteLine("tile 4 has been hit");
                        passable = false;
                    }
                } 
            }
        }
                     
    }
    public void Update()
    {
        checkForCollision();

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < mapCell.GetLength(1); x++)
        {
            for (int y = 0; y < mapCell.GetLength(1); y++)
            {
                // setting the index to the tile ID
                int index = mapCell[y,x].TileID;
               

                // checking if there is a tile
                if(index > 0)
                {
                    // start from the first index.
                    Texture2D texture = tileList[index -1 ];
                    spriteBatch.Draw(texture, new Rectangle(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT), Color.White);
                }       
            }
        }   
    }
Source Link
Loading