Good day friends! I'm Barry (solo dev under the name Delayed Victory) and I make mining games with tile-based procedurally generated maps. I'm not a very good programmer and so I'm pretty sure the way I've coded this is not efficient. Therefore I figured I'd post my code here and ask for suggestions on how to improve this. I don't expect you to re-write it for me as I know that's not what this Discord is for, but I'm looking for suggestions on how to improve.
Before I continue, please check out the first 5 seconds of the game trailer here to get an idea of the game and what I'm trying to achieve. It's about efficiently drawing the map as seen here: https://store.steampowered.com/app/2830150/Super_Mining_Mechs/
Right now this is what I'm doing:
- I have a 2D array that holds all the map data. The array contains numbers that indicate what's in each tile i.e.: 0 = nothing, 1 = dirt, 2 = gold, 3 = iron, etc.
- Then I draw the array to the screen by looping the map array, drawing each resource tile one by one. So far so good.
- Then I loop through the entire map again to draw a 'shadow' tile, which is just a black square. The further away the tile is from and empty tile, the darker it gets. This is where I get extremely inefficient, and what I need help with. I've attached an image to indicate what I mean by this.
- I draw everything to a surface so that when the player is digging, I can subtract a sprite from this surface to basically make it seem like the player is digging into the map.
Some notes:
- I'm already only drawing the tiles that are on-screen
- It's possible for the player to move at such a high speed that all tiles on screen change, meaning I can't re-use parts of the previously drawn surface.
- The game has online multiplayer so any tile on screen can change at any point (not just the center).
This message is too long to post, so I'm continuing in the comments.
