Skip to content

Commit

Permalink
Made GemCollected event, for better decoupling and flexibility. Moder…
Browse files Browse the repository at this point in the history
…nised event invocation.
  • Loading branch information
CartBlanche committed Jan 21, 2025
1 parent 9463a56 commit f182ad3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public SettingsManager<___SafeGameName___Leaderboard> LeaderboardManager
public const int NUMBER_OF_LEVELS = 5;
private const int NUMBER_OF_LAYERS = 3;

public event EventHandler<(Gem, Player)> GemCollected;

/// <summary>
/// Constructs a new level.
/// </summary>
Expand Down Expand Up @@ -198,6 +200,16 @@ public Level(ScreenManager screenManager, string levelPath, int levelIndex)

// Our backpack to store the collected gems :)
backpack = content.Load<Texture2D>("Sprites/backpack");

// Hook into the GemCollected event
GemCollected += Level_GemCollected;
}

private void Level_GemCollected(object sender, (Gem gem, Player collectedBy) e)
{
score += e.gem.Value;

e.gem.OnCollected(e.collectedBy);
}

/// <summary>
Expand Down Expand Up @@ -454,6 +466,11 @@ public void Update(
DisplayOrientation displayOrientation,
bool readyToPlay = true)
{
if (gameTime == null)
throw new ArgumentNullException(nameof(gameTime));
if (inputState == null)
throw new ArgumentNullException(nameof(inputState));

this.readyToPlay = readyToPlay;
particleManager.Update(gameTime);

Expand Down Expand Up @@ -620,9 +637,8 @@ private void UpdateEnemies(GameTime gameTime)
/// <param name="collectedBy">The player who collected this gem.</param>
private void OnGemCollected(Gem gem, Player collectedBy)
{
score += gem.Value;

gem.OnCollected(collectedBy);
// Call any associated events
GemCollected?.Invoke(this, new(gem, collectedBy));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public bool Enabled
/// </summary>
protected internal virtual void OnSelectEntry(PlayerIndex playerIndex)
{
if (Selected != null)
Selected(this, new PlayerIndexEventArgs(playerIndex));
Selected?.Invoke(this, new PlayerIndexEventArgs(playerIndex));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ public override void HandleInput(GameTime gameTime, InputState inputState)
&& inputState.IsUIClicked(new Rectangle((int)yesButtonPosition.X, (int)yesButtonPosition.Y, (int)yesTextSize.X, (int)yesTextSize.Y))))
{
// Raise the accepted event, then exit the message box.
if (Accepted != null)
Accepted(this, new PlayerIndexEventArgs(playerIndex));
Accepted?.Invoke(this, new PlayerIndexEventArgs(playerIndex));

ExitScreen();
}
Expand All @@ -116,8 +115,7 @@ public override void HandleInput(GameTime gameTime, InputState inputState)
&& inputState.IsUIClicked(new Rectangle((int)noButtonPosition.X, (int)noButtonPosition.Y, (int)noTextSize.X, (int)noTextSize.Y))))
{
// Raise the cancelled event, then exit the message box.
if (Cancelled != null)
Cancelled(this, new PlayerIndexEventArgs(playerIndex));
Cancelled?.Invoke(this, new PlayerIndexEventArgs(playerIndex));

ExitScreen();
}
Expand Down

0 comments on commit f182ad3

Please sign in to comment.