0
\$\begingroup\$

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.

Thank you very much in advance and any help is greatly appreciated.

MapCell

public class MapCell
{

    public int TileID { get; set; }

    public MapCell(int tileID) 
    {
        TileID = tileID;              
    }
}

`}

Player class

in the update method

playerRect = new Rectangle((int)Position.X, (int)Position.Y, playerRect.Width, playerRect.Height);

TileMap

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

Game 1

        TileMap tileMap = new TileMap(new int[,]
    {
        // needs to be separated by a comma
        { 4,4,4,4,4,4,4 },
        { 4,1,1,1,1,1,4 },
        { 4,1,1,0,1,1,4 },
        { 4,0,1,0,1,3,4 },
        { 4,3,3,3,3,2,4 },
        { 4,2,0,2,0,0,4 },
        { 4,4,4,4,4,4,4 },
    });
\$\endgroup\$
1
\$\begingroup\$

Each time the player is going to move, save his current position, calculate his new position and determine the tile that he then would stand on. Check your tilemap and determine the type of that tile. If it is 4, don't make the player move.

Using that method, the player will be able to overlap a type-4-tile with half of his sprite. To avoid that, you can incorporate the dimension of the player collision rectangle into the calculation.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.