Forum begins after the advertisement:


[Part 2] More elegant way to code the tile generation?

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 2] More elegant way to code the tile generation?

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #16886
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    1
    ::

    @mountain_settler from YouTube has an interesting question:

    is it really necessary to build such a huge pile of conditions and is there truly no other way to make it more elegant? Every frame will go through a mountain of checks for 95% of cases will nothing happen.
    has upvoted this post.
    #16887
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    1
    ::

    It is always possible that there is a more elegant way.

    In our case here we only check for collision when the character is moving (highlighted portions). This makes the code way more efficient than checking for collision directly, because collision checking is much more expensive than positional checking. Checking which direction the player is moving in is just simple arithmetic (comparing numbers), which the computer performs very quickly.

    void ChunkChecker()
    {
        if(!currentChunk)
        {
            return;
        }
    
        if (pm.moveDir.x > 0 && pm.moveDir.y == 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Right").position;  //Right
                SpawnChunk();
            }
        }
        else if (pm.moveDir.x < 0 && pm.moveDir.y == 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Left").position;    //Left
                SpawnChunk();
            }
        }
        else if (pm.moveDir.y > 0 && pm.moveDir.x == 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Up").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Up").position; //Up
                SpawnChunk();
            }
        }
        else if (pm.moveDir.y < 0 && pm.moveDir.x == 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Down").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Down").position;    //Down
                SpawnChunk();
            }
        }
        else if (pm.moveDir.x > 0 && pm.moveDir.y > 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Up").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Right Up").position;   //Right up
                SpawnChunk();
            }
        }
        else if (pm.moveDir.x > 0 && pm.moveDir.y < 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Right Down").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Right Down").position;  //Right down
                SpawnChunk();
            }
        }
        else if (pm.moveDir.x < 0 && pm.moveDir.y > 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Up").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Left Up").position;  //Left up
                SpawnChunk();
            }
        }
        else if (pm.moveDir.x < 0 && pm.moveDir.y < 0)
        {
            if (!Physics2D.OverlapCircle(currentChunk.transform.Find("Left Down").position, checkerRadius, terrainMask))
            {
                noTerrainPosition = currentChunk.transform.Find("Left Down").position; //Left down
                SpawnChunk();
            }
        }
    }

    A lot of core game dev code is like this! Collision checking between objects works like this too.

    If you are looking at elegance, it may be possible to group all the repeated code in a loop instead of donig so many if / else conditions like this. In one of the upcoming parts, I’m planning to make some optimisations to the map generation code.

    has upvoted this post.
    #16888
    A_DONUT
    Level 7
    Moderator
    Helpful?
    Up
    0
    ::

    Mmmm nice

    #16889
    Alp Apustaja
    Level 1
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Welcome, A_DONUT! How can I assist you today with your programming questions or issues?

    #16892
    Alp Apustaja
    Level 1
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Great job on optimizing the collision checking in your code! It’s always good to prioritize efficiency, especially in game development where performance is crucial.

    Regarding your idea of simplifying the code further by grouping the repeated logic into a loop, that sounds like a good approach. By reducing the redundancy in your code, you can make it more maintainable and easier to read.

    Here’s a suggestion on how you could potentially refactor the ChunkChecker() method using a loop:

    
    void ChunkChecker()
    {
        if (!currentChunk)
        {
            return;
        }
    
        Vector2[] directions = { Vector2.right, Vector2
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: