Collision Time
So today I focused on collision detection, now normally this should be a lot easier than what I am looking to do, reason being is normally you are detecting collision between a 8*8 square and another 8x8 square. My Bombs are not 8x8 so I had to look into how to do this, I read up about bounding/hit boxes, which essentially is that you draw an invisible box/rectangle around the area you want to check. Sounds simple right? Only thing is just because you created this space doesn't mean its attached to your sprite, it is a completely separate entity. So to get around this (only for this game unfortunately) I need to work out the exact location within your sprite this needs to be drawn, then when I perform the hit check, pass it the sprites x&y and from there use my new knowledge of where within the sprite space the box would be and only check in there. Simple in Theory.
I found a tutorial online and took that and got that working on a 1 to 1 basis, basically I drew a bounding box on screen, have a moveable other box and printed out if the moveable box goes over the other. It worked (although both were 8x8), so next step was to create a smaller box and put a sprite in it resembling the bomb. It worked. All was going well, so I took my bomb code from the main game and dropped it in here, so our sprite bounding box would act like one of our bombs and scroll down the screen. Got that working with a tweak or two, so the next step was to have multiple bombs all falling and checking against all of them.
Now this took a fair bit of tweaking and some re-writing of what we already have, but in the end I got it working. So e have 4 bombs a falling, if you scroll over one it was showing up as a hit, but the bomb still continued to fall till it got to the bottom then re-spawned at the top. Next step was to have the bomb do something if it was rolled over. I added to the bomb code, putting in a new var, then if it gets rolled over, we can set this new var to hit. Then update the check in the bomb to say if its hit then del and make a new bomb. All simple stuff and yes it worked pretty much first time.
All great but one thing was annoying me, and this was also something I was going to have to add into the game. Delays for bombs. Yes bombs run at different speeds down the screen but, as soon as they get re-spawned they auto start,there is no delay. So in this test all 4 bombs fall at the same speed so if you allow them to fall to the bottom they al reappear at the same time at the top. Now this is not good and doesn't feel real, so I decided t add in a delay. Easier said than done as delays in this don't really work, you need to end up counting against frames and such. Also ow was I going to do it, should it be in the bomb code or else where. I automatically wanted to add it in the bomb code as it felt that that's where it should be, when you call the bomb to be created it could then wait for a time then go.
Ultimately I decided against this (it might come back o bite my in the ass) an created a new function called re-spawn. As it dawned on me that if I did it on the bomb I would have to keep the bomb obj alive after it had been hit, that would mean moving it else where on screen so it could not be hit again and a few other issues as well. With a new function, I could create a system that then triggers a new bomb and keep the main code cleaner.
So in the new function, I decided that when called, it is passed a time (the bomb creates one randomly when created) and it then inserts this into an array(table), every bomb that explodes a new entry is added. This function every update, cycles through each entry in the table and reduces the amount by 1, if an amount is 0, then it triggers the make bomb function and deletes that entry. Simple effective stuff and it worked perf...... no it did not.
So I kept getting errors at random points but it seems that some bobs would re-spawn others wouldn't. I spent about an hour scratching my head, re-writing the code and what not until I found out that once an entry is deleted the rest of the array/table move down. So the issue was because say you have 4 entries and your looping through all 4 but you delete the 2nd one, when you get to the 4th loop there is no longer an entry. I tried a couple of things, my one idea was to reverse the loop, so you start at the en and work your way down, that way any deletes should not effect what you still have to loop through. This brought with it other issues and extra code to work and even then it didn't wok quite right. I read a few solutions online but they were all about creating other functions and re-changing everything I had done, so I gave up and went to do the shopping. Literally 10 minutes later on my way to the shops I had a eureka moment, an offset. Basically create a variable that's set to 0 at loop start, then increment by one for every deletion. Back in the main loop code when it references what array/table item number instead of saying just the loop count call loop count - the offset. That way it should always be right. ok this might not be he best way to do t but god damn it it works.
That's where I ended for the day and you can see it all working in this test cart, I will prob not touch this for a day or two as ive spent the last 4 ish days solidly working on this in my spare time and I have a few other things to do. But next up will be bullets and hopefully I will be able to integrate them into this for collisions to work.