Tuesday, November 2, 2010

Clean up that code sonny!

Although i haven't had much time for my own stuff. I have been twiddling with the code a bit here and there. One of the jobs i am doing that does not usually require a lot of hours at any time is to tidy up my code.

Tidying up the A Shooter code is quite interesting, as i do not really remember the bits of glue code i had to write to make some stuff just work right. However as i look through the code i see parts where  some gameplay elements coordinate systems are being translated into the main coordinate system (the 2d one the sprites use). This is sort of fun tidying up this annoying things.

However one must have a plan to tidy things up. Simply changing this badness for other badness is not a recommended solution to anything. Let me give you an example of one of the main things i found annoying about A Shooter code.

In A Shooter all the sprites are 2D. They have an X/Y postion and width and a height. simple enough and it shouldbe always simple. There is a question about where to do some of the Width/2 coding though. for such things as detection. Or target to fire at coordinates. so a lot of my code would look at the player and add onto the X/Y coords the half width and half height offsets to shoot at the center of the player. Again that is simple enough in and of itself. However you also need to fire from the center of the Alien itslef. Aho! another X/Y pos plus half width and half height. We have now done it twice. Well then it gets worse. You then have to detect the hit boxes against each other also doing the very same calculations. So now we are up to 4 at least. There are other systems as well that interface with all these coordinate offsets. I knew there was a simpler way, but by the time i realised it was a problem i was too far down dev road to turn back.

The simpler way is to just draw the sprite with an origin set to it's center. Though this is great for A Shooter as it used mostly Radius checks for collision, it is not so good for box detections. If you are doing box detections, then you could of course keep your origin in the top left corner and just use the width and height for you box extremies.

What does this all mean ? Well it really means you should know which way is best for your style of game. Then work out how many different times you will be doing such calculations. You could even make a nice simple function to wrap the hideous offset adds in, and that might make ya feel better.

Myself, i am going to move the origins into the middle of the enemy and player graphics, and then use offsets for what i need from there. Simply because i KNOW for sure where the thing is, i donot have to do a math quiz to figure out how close i am etc.

g'night

No comments:

Post a Comment