Second day of development
Talking with GPT, trying different implementations, braking what I implemented and fixing what I broke. (approximate development time 3 hours 30 minutes)
9/17/20253 min read
During the second day of development I was working on mining sand by clicking on block, shooting projectiles and on collision spawning simulated sand from projectile. To implement this I had to solve these issues: detecting that I clicked on block, spawning projectile after click, spawning projectile outside of blocks, giving spawned projectile velocity, spawning sand on projectile collision, don't replace collision pixel but spawn sand on top.
I'm gonna be honest I procrastinated a lot before starting my work. Looking back at it I think I was scared I won't be able to implement it and fail another project because of signature feature I chose for it. Let me tell you how it went in the end.
I started by giving GPT manual to asset I'm using and debated possible implementations. I started with detecting clicked block, this wasn't too hard, I just needed to calculate pixel world position from mouse position which was already implemented and then check if block is empty or not. Small issue I encountered was when I tried to decrease blocks HP so I can destroy it. I didn't know how to write it into pixel in this asset but it ended up being pretty simple. I just got pixel, edited it and then inserted it into list of pixels to redraw.
Next issue to solve was spawning projectiles in correct position and velocity. I ended up using square 2D sprite for projectile and made prefab from it. Then after I clicked valid block, I used algorithm to search in circle for closest available space. Next was velocity. Here I encountered few issue because I was mixing parts of my code and asset code so in some places I was using world position and in some pixel position. Once I unified it it was as simple as calculating directional vector from starting clicked position towards empty space and applying it to velocity.
Now time to spawn sand from my projectiles. Doing this should be as simple as giving my projectile collider and spawn new sand pixel on collision right? Unfortunately wrong, It was only part of the solution, there were 2 significant issues. First when I spawned sand it replaced pixel it collided it and second half of the time when projectile was shot out it collided with block that is next to empty slot and replaced it with sand.
I tried few approaches to solve the first issue but it ended up as simple as using circular search to find closest empty space to collision pixel.
To solve second issue I just added 2 times vector that I multiplied by pixel size. It ended up pretty simple but while trying to solve this I still had messy positions (somewhere world, somewhere pixel grid). But after I unified my positions it was pretty simple to implement.
Last bonus feature I wanted to add was spawning 10 sand projectiles after block is mined. Of course this came with few issues. At first I just added for loop into spawning my projectiles but I found out pretty soon that they collide with each other and immediately spawn 1 sand pixel and are destroyed. This was easy fix I just added check into my collision code that if it's pixel projectile then ignore collision. Than I added spread between each projectile to avoid them flying in the same line. But there is still one more issue, if you mine 2 full blocks that that are close to each other, one after another (1 hit mine), than second on won't spawn sand. This is because when you mined first block it will make hole so when you mine second one it will thrown all sand into that hole and only one will be created. I can solve this by filling hole with sand when block is destroyed or by searching for empty space after each projectile is spawned. I think first solution would be better and lot simpler but for now I left it as it is.
So in the end I managed to implement everything I wanted for the day and maybe even better than I expected. Next development day contains last big issue to solve and it's moving sand with drag and drop mouse. I hope it will go as good as this part. Not sure if I will be able to work daily this week as I wanted because of big family emergency but I will do my best.
Thank you for reading!