samedi 28 novembre 2020

I'm creating terrain that overlaps while it isn't supposed to

I'm creating a simple platformer in MonoGame with procedurally generated islands as terrain. I have 4 important classes: AllIslands, Islands, Ground, and GroundBlocks. AllIslands is a class that manages the islands and causes them to procedurally generate. Islands is the class that makes the island with random length and position. Ground is the class that manages and creates rows of GroundBlocks.

I'm trying to generate the islands with room in between them, to do this I use the variable RightXOfLastIsland, this variable contains the rightmost X value of an island. I look at the RightXOfLastIsland and then add a random amount to it for the position of the new island. My problem is that the islands overlap and I have no idea why.

class AllIslands
{
    public List<Island> ListOfIslands = new List<Island>(0);
    public int RightXOfLastIsland = 0;

    public AllIslands()
    {

    }

    public void CreateIsland(Ground ground)
    {
        if (ListOfIslands.Count > 0)
        {
            Island previousIsland = ListOfIslands[ListOfIslands.Count - 1];
            RightXOfLastIsland = previousIsland.RightXOfLastIsland;
        }
        Island island = new Island(ground, RightXOfLastIsland);
        ListOfIslands.Add(island);
    }
    public void ProcedurallySpawnIslands(Player player, Ground ground)
    {
        if (player.Position.X > RightXOfLastIsland - 200)
        {
            CreateIsland(ground);
        }
    }
}

class Island
{
    public int RightXOfLastIsland;

    public Island(Ground ground, int rightXOfLastIsland)
    {
        RightXOfLastIsland = rightXOfLastIsland;

        Random random = new Random();

        int tempRightX = RightXOfLastIsland / 100;

        int locationX = random.Next(tempRightX + 5, tempRightX + 10);
        int locationY = random.Next(5, 9);
        int lengthRowOfBlocks = random.Next(5, 10);

        Vector2 location = new Vector2(locationX * 100, locationY * 100);

        ground.CreateGroundBlocks(location, lengthRowOfBlocks);

        RightXOfLastIsland = RightXOfLastIsland + (lengthRowOfBlocks * 100);
    }
}



Aucun commentaire:

Enregistrer un commentaire