Well, I continued working on my project. I'm glad that I can say I implemented Game State Machine... maybe not optimized nor pretty but it works. That's enough for me for now.
Main menu works perfectly, and I'm working on my main part from scratch (done most of it, now only bullet collision and AI). I also began to look more on code efficiency and to use structures, classes and methods I didn't use before. In addition to that, I wrote fresh code and didn't use code from prototype since it was inefficient. When I looked at prototype code after these 2-3 weeks, I was going "Why did I do that" whole time and scrapped it in favor of better code (like splitting tank logic, bullet logic and AI logic from one into 3 separate classes).
So, after introduction, time for my questions. It's my habit to have them numbered etc. for ease of reading. Please keep in mind that, despite going through university education, I'm studying this on my own because my university doesn't have classes specialized for this field (especially NOT object oriented programming), so sorry if some questions are dumb or "dooh, you should know that" type. Also, on some questions I already assumed answers, I just want to see if I understand that correctly
1) Public declared class variables vs class properties. For example, I'm using class Tank which holds all data about its status in the game. So, technically speaking, what's the difference between those two:
public Rectangle BoundingBox
{
get { return tileRectangle; }
set { tileRectangle = value; }
}
//
Rectangle tileRectangle;
//
// vs
public Rectangle boundingBox;
2) Methods with multiple parameters vs overloaded methods. In my prototype (which I can upload, no problem) I wrote collision in a method which required 3 parameters which then covered and properly handled all possibilities. Now I'm using 2 methods with same name but with slighly different parameters and behaviour (method overload). Performance wise, which is better and why?
Method definitions:
[source lang="csharp"]// Prototype method, I think it's self explanatorypublic bool Collision(Rectangle Object, bool isProjectile, bool isPlayerTank);//// Current, overloaded method (Tank and Bullet are custom classes)bool Collision(Tank Object)bool Collision(Bullet Object)[/source]
3) Another "battle" question: List<T> vs Array. In prototype I used arrays to hold object information and sometimes it was pain in the a** to manage it. Now I'm using Lists which made my life a lot easier (I don't need to skip that pesky Player2 slot if Player 2 doesn't exist), but still, what is better to use in which situations performance wise?
4) Events and event handler. I've used them now but still, I don't quite understand them, so can somebody give me a brief explanation? I understand that event is a piece of code which triggers automatically (like interrupt) when certain conditions are met (eg. MouseClick) and uses its handler method to deal with the situation.
5) And final, my most important question: collision detection. I'm using combination of Bounding Box method and Pixel Perfect method; reason is that while most world objects are static and won't change - meaning Bounding Box method is sufficient for collision detection (check if tank's and tile's hitboxes overlap and if yes, reset the position of tank to previous one) - there are also destructible objects which can have only part destroyed. I know the general idea of it and I tried to reproduce it on my own in order to learn it better. It works for now but I didn't test it on situation I need to because I didn't get to that part; but even now, I'm worried about efficiency of code. I already made a stupid mistake which ended calling one line of code 16 milion times, so I'm quite worried because it will need to run up to 10 times in one cycle (2 player tanks and 8 enemy tanks) which not only could but will cause lag. Even worse when bullets come into play.
[source lang="csharp"] bool PixelPerfectCollision(Tank Object, List<Tile> Wall) { Rectangle tankBox = Object.GetRectangle(); int totalWidth = Math.Max(Wall[0].BoundingBox.Width, tankBox.Width) * 2; int totalHeight = Math.Max(Wall[0].BoundingBox.Height, tankBox.Height) * 2; Color[,] wallColor = new Color[totalWidth, totalHeight]; Color[,] tankColor = new Color[totalWidth, totalHeight]; Point[] location = new Point[Wall.Count]; Point offset; for (int x = 0; x < Wall.Count; x++) location[x] = Wall[x].BoundingBox.Location; // Get starting point via offset offset = location[0]; for (int x = 1; x < Wall.Count; x++) { if (location[x].X < offset.X) offset.X = location[x].X; if (location[x].Y < offset.Y) offset.Y = location[x].Y; } if (tankBox.X < offset.X) offset.X = tankBox.X; if (tankBox.Y < offset.Y) offset.Y = tankBox.Y; // Clear matrixes for (int x = 0; x < totalWidth; x++) for (int y = 0; y < totalHeight; y++) { wallColor[x, y] = Color.Transparent; tankColor[x, y] = Color.Transparent; } // Combine walls into single wall foreach (var tile in Wall) { Color[,] a = tile.GetColorMatrix(); for (int x = tile.BoundingBox.Left - offset.X; x < tile.BoundingBox.Right - offset.X; x++) for (int y = tile.BoundingBox.Top - offset.Y; y < tile.BoundingBox.Bottom - offset.Y; y++) wallColor[x, y] = a[x / 5, y / 5]; } // Prepare tank for comparsion Color[,] n = Object.GetColorMatrix(); for (int x = tankBox.Left - offset.X; x < tankBox.Right - offset.X; x++) for (int y = tankBox.Top - offset.Y; y < tankBox.Bottom - offset.Y; y++) tankColor[x, y] = n[x * 2 / 5, y * 2 / 5]; // Comparsion for collision for (int x = tankBox.Left - offset.X; x < tankBox.Right - offset.X; x++) for (int y = tankBox.Top - offset.Y; y < tankBox.Bottom - offset.Y; y++) if (wallColor[x, y].A != 0 && tankColor[x, y].A != 0) return true; return false; }[/source]
I've already though about some improvements like looking only where there is collision instead of looking through whole tank but I'd still like more recommendations. I can get to the solution on my own (more research and some trial&error) but I'd rather do the right thing immediately.
Thanks for sparing some time to read this.
I hope I'm not bothering anyone and I don't like to ask others much, I prefer to figure out things on my own but for some things it's better and faster if I ask someone to explain. Also, if you need some more information please ask me to provide it.






