API: XNA
Hi, I have a game that i worked on a while back that I wish to re-write/fix up. The code for it is ugly and definitely has major design flaws. One of the major flaws I found is my use of declaring objects as static. Here's an example of what I'm talking about
Original design
GameplayScreen snippet
class GameplayScreen : GameScreen
{
#region Fields
ContentManager content;
SpriteFont gameFont;
Random random = new Random();
//Code I've added
public Block player;
BlockManager blockManager = new BlockManager();
public static ItemManager itemManager;
public static Texture2D redBlock;
public static Texture2D yellowBlock;
public static Texture2D greenBlock;
public static Texture2D plusOrb;
public static Texture2D minusOrb;
public static Texture2D growBiggerArrow;
public static Texture2D growSmallerArrow;
public static Texture2D invincibility;
public static SpriteFont itemFont;
public static SoundEffect death;
public static SoundEffect spawn;
public static SoundEffect plusSound;
public static SoundEffect minusSound;
public static SoundEffect shrinkSound;
public static SoundEffect growSound;
public static SoundEffect invincibilitySound;
public static bool isPlayerAlive = true;
public static int score = 0;
GrowBiggerArrow Snippet
public override void Update(GameTime gameTime)
{
/*if (this.HitBounds.Intersects(GameplayScreen.player.HitBounds))
{
//do specific item powerup. in this case it is +size
if (GameplayScreen.player.Size.X < GameplayScreen.player.MaxSize.X || GameplayScreen.player.Size.Y < GameplayScreen.player.MaxSize.Y)
GameplayScreen.player.Size += new Vector2(16,16);
GameplayScreen.itemManager.RemoveItem(this);
}*/
if (0 == triggered)
{
itemTimer += gameTime.ElapsedGameTime;
if (itemTimer > TimeSpan.FromSeconds(3))
{
GameplayScreen.itemManager.RemoveItem(this);
return;
}
if (this.HitBounds.Intersects(GameplayScreen.player.HitBounds))
{
//do specific item powerup. in this case it is grow bigger
//start timer
if (OptionsMenuScreen.sound)
GameplayScreen.growSound.Play();
if (GameplayScreen.player.Size.X <= GameplayScreen.player.MaxSize.X || GameplayScreen.player.Size.Y <= GameplayScreen.player.MaxSize.Y)
{
GameplayScreen.player.Size += new Vector2(16, 16);
text = "You grew!";
}
else
text = "You're too big to grow!";
triggered = 1;
//GameplayScreen.itemManager.RemoveItem(this);
}
}
//otherwise we picked it up at some point so check the timer and see if its time to take away the buff
else
{
displayTimer += gameTime.ElapsedGameTime;
if (displayTimer > TimeSpan.FromSeconds(3))
{
GameplayScreen.itemManager.RemoveItem(this);
displayTimer = TimeSpan.Zero;
}
}
}
As you can see, I have statics running all over the place. I want to keep my Update function to only take gametime as a parameter, but I need a way of getting rid of that call to access the player via the gamescreen class. I'm not sure how to implement this but these are the ideas I came up with.
Idea 1: On a collision, call the Grow method. I would have to take the collision detection out of the grow bigger class and move it to the gameplay screen (maybe? or is there a better way?)
Idea fix for grow bigger class
//All game objects (players, cars, items) are derived from GameObject
//gameObjectType would be an enum with all the different object types. (This could get extremely large and would
//have to be maintained manually (Haven't figured out how to do this part automatically)
//By taking these two parameters, I would be able to make all the public statics in gameplayscreen into private members
void Grow(GameObject obj, enum gameObjectType)
{
switch (gameObjectType)
{
case: Player:
//Handle player specific growth
break;
case: Vehicle
//Handle vehicle specific growth
break
}
}
Idea 2: Similar to idea 1 except I would have the grow bigger item fire off an event on the object that it collided with. The object that it collided with would then get the onCollide event and it would have the onCollide event handled by itself.
Example: GrowBiggerItem collides with player. GrowBiggerItem fires off the collide event to player. Player gets the event and has a method "onCollide" and handles the collide event.
These are the ideas that I came up with to get rid of the public statics and hopefully provide a better OOP design to my classes. Any feedback on what I can do to fix this would be appreciated.
Here is the full code for the growbigger item. Please point out any issues you see that I can improve on, I'm still trying to grasp OOP design in my code. Thank you.