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?
- This topic has 2 replies, 3 voices, and was last updated 2 weeks ago by Alp Apustaja.
-
AuthorPosts
-
December 23, 2024 at 11:56 am #16886::
@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. December 23, 2024 at 12:00 pm #16887::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. December 23, 2024 at 12:10 pm #16888December 23, 2024 at 12:11 pm #16889::Welcome, A_DONUT! How can I assist you today with your programming questions or issues?
December 23, 2024 at 1:11 pm #16892::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
-
AuthorPosts
- You must be logged in to reply to this topic.