So I've just been thinking about component and messaging systems recently for simple C# XNA games and came up with this. How extensible would this implementation be and what are the drawbacks? Example below:
Score
class Score
{
public int PlayerScore { get; set; }
public Score()
{
EnemySpaceship.Death += new EventHandler<SpaceshipDeathEventArgs>(Spaceship_Death);
}
private void Spaceship_Death(object sender, SpaceshipDeathEventArgs e)
{
PlayerScore += e.Points;
}
}
EnemySpaceship
class EnemySpaceship
{
public static event EventHandler<SpaceshipDeathEventArgs> Death;
public void Kill()
{
OnDeath();
}
protected virtual void OnDeath()
{
if (Death != null)
{
Death(this, new SpaceshipDeathEventArgs(50));
}
}
}
SpaceshipDeathEventArgs
class SpaceshipDeathEventArgs : EventArgs
{
public int Points { get; private set; }
internal SpaceshipDeathEventArgs(int points)
{
Points = points;
}
}
Basically, the idea uses the built in events system in C#. When something interesting happens to an object, it raises a static event. Then, anything else in the game that wants to know when that happens subscribes to that static event and does something in response. In this case, the Score class subscribes to the static Death event of a Spaceship and when it dies, it uses the EventArgs to adjust the score accordingly.
Would this kind of architecture start to crumble and get quite confusing as the development of the game progressed? Thanks for reading.
EnemySpaceship.Death += (sender, e) => { PlayerScore += e.Points; };– ashes999 Feb 8 '12 at 0:45