I've been coding professionally for most of my life, sometimes games, sometimes other stuff. The reason I write this post is that I feel that I keep make (at least) one reoccuring "newbie design flaw" over and over again.
Let me try to explain with the current example:
I build a game. It's object oriented C#, everything is beautifully encapsulated and working fine.
There is a main "game" class and declarations of my objects:
public class MySuperGame : Microsoft.Xna.Framework.Game { MyPlayerClass thePlayer; MyTilemapClass theTilemap; //(...) }
Problems begin when these objects need access to each other.
In this fabricated example, the "Player" class is doing collision checking with stuff on the Tilemap.
So, my options, I can either
1) Break out the logic, and do everything in the game class
MySuperGame.DoCollisionStuff(MyPlayerClass thePlayer, MyTilemapClass theTilemap)
or
2) cross-reference the pointers, for example initialize the player class with a reference to the tilemap
public MyPlayerClass(MyTilemapClass theTilemap [, textures and stuff]
or
3) just pass a pointer when it's needed:
protected override void Update(GameTime gameTime) { thePlayer.HandleCollisions(theGameTilemap); //(...) }
I have been mostly doing #3, but I feel that it's flawed design and it quickly becomes cluttered with unnecessary cross-referencing pointers and variables.
My next step is to make an "achievement system".
I imagine an AchievementHandler class that keeps track of everything that happens in the game, so this class needs to be accessible from every single corner of the code.
MyPlayerClass.DoJumpyStuff() { theAchievementHandler.Add(AchievementType.PlayerJumpCount, 1) //(...) } MyLevelHandler.StartNewLevel() { theAchievementHandler.Add(AchievementType.LevelsPlayedTotal, 1) //(...) } //etc.etc.
This makes me think even more urgently of a "good code"-solution to this dilemma.
I hope this makes sense. Thanks for reading, I value all feedback.
Cheers,
Murmix
PS: The game in question is Turtle Boogie, a little arcade action fun.
A concept prototype demo download is available here:
http://www.murmix.com/TurtleBoogie