#Learning to make a console roguelike for fun - collision help

1 messages · Page 1 of 1 (latest)

tawdry moon
#

This is my movement function and collision checker

int Engine::isCollision(int x, int y, int z)
{
    if (collision_map[x][y][z] == 0)
    {
        return 0;
    }
    if (collision_map[x][y][z] == 1)
    {
        return 1;
    }
}

int Inputs::input_handler(int x, int y, int z, int cpf)
{
    constexpr auto KEY_UP = 72;

    constexpr auto KEY_DOWN = 80;

    constexpr auto KEY_LEFT = 75;

    constexpr auto KEY_RIGHT = 77;

    constexpr auto ESC = 27;
    
    int checkx = x;
    int checky = y;
    int checkz = z;

    char input_ = _getch();

    if (input_ == KEY_UP) {

        checky -= 1;

        if (isCollision(checkx, checky, checkz) == 1)
        {
            y -= 0;
            std::cout << "I can't do this";
        }
        if (isCollision(checkx, checky, checkz) == 0)
        {
            y -= 1;
        }
        
    }
//other key inputs removed for brevity
}
cyan crowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

tawdry moon
#

This function initializes three different arrays. One used to draw symbols to the console, one used to track the player so I don't have to erase positions in the original array when the player moves, and one used to mark what symbols positions have collision.

void Engine::create_world(int x1, int y1, int z1)
{
    height = 10;
    length = 10;
    depth = 5;
    
    Tiles map_tile;
    map_tile.wall = '#';
    map_tile.player = '@';
    map_tile.floor = '.';
    map_tile.tree = 'T';

    using Random = effolkronium::random_static;
    auto random_number = Random::get(1, 100);

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < length; x++)
        {
            for (int z = 0; z < depth; z++)
            {
                if (x > 0 || x < length - 1)
                {
                    world_map[x][y][z] = map_tile.floor;

                    if (world_map[x][y][z] == map_tile.floor)
                    {
                        auto random_number = Random::get(1, 100);

                        if (random_number <= 10 && z == 0)
                        {
                            world_map[x][y][z] = map_tile.tree;

                        }
                    }
                }

                if (x == 0 || x == length - 1 || y == 0 || y == height - 1) //flags the perimeter of the map and puts walls
                {
                    world_map[x][y][z] = map_tile.wall;

                }

                if (x == x1 && y == y1 && z == z1)
                {
                    entity_map[x][y][z] = map_tile.player;
                }

                
                if (world_map[x][y][z] == map_tile.tree || world_map[x][y][z] ==  map_tile.wall)
                {
                    collision_map[x][y][z] = 1;
                }
                if (world_map[x][y][z] == map_tile.floor)
                {
                    collision_map[x][y][z] = 0;
                }

            }
        }
    }


}
#

The issue is that I know the array for collision (collision_map) is being initialized correctly, but my input_handler function doesn't seem to check it correclt and I don't know why. I can move over symbols that I shouldn't be able to