Skip to content

Commit

Permalink
Adding narrative events.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidadas committed Sep 26, 2024
1 parent a4148ac commit 0fdbb25
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/workflows/unity-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:
projectPath: .
unityVersion: 2021.3.12f1
testMode: editmode
githubToken: ${{ secrets.GITHUB_TOKEN }}
dockerCpuLimit: 2
dockerMemoryLimit: 8g

Expand Down
141 changes: 141 additions & 0 deletions Assets/Scripts/Conditionals/ConditionalFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using System;
using System.Collections.Generic;

/// <summary>
///
/// </summary>
public static class ConditionalFactory
{
/// <summary>
///
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
private delegate IConditional ConditionalCreator(Dictionary<string, object> parameters);

/// <summary>
///
/// </summary>
private static readonly Dictionary<string, ConditionalCreator> conditionCreators = new Dictionary<string, ConditionalCreator>
{
{ "IsOnSamePlanet", CreateIsOnSamePlanet },
{ "HasFamilialRelationship", CreateHasFamilialRelationship },
{ "IsOnOpposingFactions", CreateIsOnOpposingFactions },
{ "IsOnMission", CreateIsOnMission }
// Add more condition types and their creation functions here
};

/// <summary>
///
/// </summary>
/// <param name="conditionType"></param>
/// <param name="parameters"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static IConditional CreateConditional(string conditionType, Dictionary<string, object> parameters)
{
if (conditionCreators.TryGetValue(conditionType, out var creator))
{
return creator(parameters);
}
else
{
throw new ArgumentException($"Invalid condition type: {conditionType}");
}
}

/// <summary>
///
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
private static IConditional CreateIsOnSamePlanet(Dictionary<string, object> parameters)
{
return new GenericConditional(IsOnSamePlanet, parameters);
}

/// <summary>
///
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
private static IConditional CreateHasFamilialRelationship(Dictionary<string, object> parameters)
{
return new GenericConditional(HasFamilialRelationship, parameters);
}

/// <summary>
///
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
private static IConditional CreateIsOnOpposingFactions(Dictionary<string, object> parameters)
{
return new GenericConditional(IsOnOpposingFactions, parameters);
}

/// <summary>
///
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
private static IConditional CreateIsOnMission(Dictionary<string, object> parameters)
{
return new GenericConditional(IsOnMission, parameters);
}

/// <summary>
///
/// </summary>
/// <param name="game"></param>
/// <param name="parameters"></param>
/// <returns></returns>
private static bool IsOnSamePlanet(Game game, Dictionary<string, object> parameters)
{
string gameID1 = (string)parameters["GameID1"];
string gameID2 = (string)parameters["GameID2"];
// Implement the logic here
return true; // Placeholder
}

/// <summary>
///
/// </summary>
/// <param name="game"></param>
/// <param name="parameters"></param>
/// <returns></returns>
private static bool HasFamilialRelationship(Game game, Dictionary<string, object> parameters)
{
string gameID1 = (string)parameters["GameID1"];
string gameID2 = (string)parameters["GameID2"];
// Implement the logic here
return true; // Placeholder
}

/// <summary>
///
/// </summary>
/// <param name="game"></param>
/// <param name="parameters"></param>
/// <returns></returns>
private static bool IsOnOpposingFactions(Game game, Dictionary<string, object> parameters)
{
string gameID1 = (string)parameters["GameID1"];
string gameID2 = (string)parameters["GameID2"];
// Implement the logic here
return true; // Placeholder
}

/// <summary>
///
/// </summary>
/// <param name="game"></param>
/// <param name="parameters"></param>
/// <returns></returns>
private static bool IsOnMission(Game game, Dictionary<string, object> parameters)
{
string gameID = (string)parameters["GameID"];
// Implement the logic here
return true; // Placeholder
}
}
32 changes: 32 additions & 0 deletions Assets/Scripts/Conditionals/GenericConditional.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;

/// <summary>
///
/// </summary>
public class GenericConditional : IConditional
{
private readonly Func<Game, Dictionary<string, object>, bool> callback;
private Dictionary<string, object> parameters;

/// <summary>
///
/// </summary>
/// <param name="callback"></param>
/// <param name="parameters"></param>
public GenericConditional(Func<Game, Dictionary<string, object>, bool> callback, Dictionary<string, object> parameters)
{
this.callback = callback;
this.parameters = parameters;
}

/// <summary>
///
/// </summary>
/// <param name="game"></param>
/// <returns></returns>
public bool IsMet(Game game)
{
return callback(game, parameters);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <summary>
/// Represents a condition that must be met for an event or action to proceed.
/// </summary>
public interface ICondition
public interface IConditional
{
/// <summary>
/// Evaluates the condition in the context of the provided game instance.
Expand Down
7 changes: 6 additions & 1 deletion Assets/Scripts/Events/GameEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public abstract class GameEvent : GameEntity
public event Action<GameEvent> OnEventTriggered;

/// <summary>
/// Initializes a new instance of the <see cref="GameEvent"/> class with the specified parameters.
/// Default constructor used for serialization.
/// </summary>
public GameEvent() { }

/// <summary>
///
/// </summary>
/// <param name="scheduledTick">The tick at which the event is scheduled to occur.</param>
protected GameEvent(int scheduledTick)
Expand Down
7 changes: 6 additions & 1 deletion Assets/Scripts/Events/MissionEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ public class MissionEvent : GameEvent
public Mission Mission { get; private set; }

/// <summary>
/// Initializes a new instance of the MissionEvent class.
/// Default constructor used for serialization.
/// </summary>
public MissionEvent() { }

/// <summary>
/// Initializes a <see cref="MissionEvent"/> with the specified parameters.
/// </summary>
/// <param name="scheduledTick">The tick at which the event is scheduled to occur.</param>
/// <param name="mission">The mission that is being executed.</param>
Expand Down
29 changes: 29 additions & 0 deletions Assets/Scripts/Events/NarrativeEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using System;

public class NarrativeEvent : GameEvent
{
public IConditional Conditionals { get; set; }
public List<SerializableDictionary<string, object>> ConditionalParams { get; set; }

/// <summary>
/// Default constructor used for serialization.
/// </summary>
public NarrativeEvent() { }

/// <summary>
///
/// </summary>
/// <param name="scheduledTick"></param>
/// <param name="conditionals"></param>
public NarrativeEvent(int scheduledTick, List<IConditional> conditionals) : base(scheduledTick) { }

/// <summary>
///
/// </summary>
/// <param name="game"></param>
protected override void TriggerEvent(Game game)
{
// @TODO: Implement trigger.
}
}
4 changes: 2 additions & 2 deletions Assets/Scripts/Managers/GameEventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
/// <summary>
/// Manages game events and their scheduling.
/// </summary>
public class EventManager
public class GameEventManager
{
private readonly Game game;

public EventManager(Game game)
public GameEventManager(Game game)
{
this.game = game;
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Managers/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum TickSpeed
public class GameManager
{
private Game currentGame;
public EventManager EventManager { get; private set; }
public GameEventManager EventManager { get; private set; }
public MissionManager MissionManager { get; private set; }
private float? tickInterval;
private float tickTimer;
Expand All @@ -29,7 +29,7 @@ public class GameManager
public GameManager(Game game)
{
currentGame = game;
EventManager = new EventManager(game);
EventManager = new GameEventManager(game);
MissionManager = new MissionManager(EventManager);
}

Expand Down
17 changes: 9 additions & 8 deletions Assets/Scripts/Managers/MissionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@
/// </summary>
public class MissionManager
{
protected EventManager eventManager;
protected GameEventManager eventManager;

/// <summary>
/// Initializes a new instance of the <see cref="MissionManager"/> class.
///
/// </summary>
/// <param name="eventManager">The event manager.</param>
public MissionManager(EventManager eventManager)
public MissionManager(GameEventManager eventManager)
{
this.eventManager = eventManager;
InitializeMissions();

// Initialize missions (i.e. register event handlers).
initializeMissions();
}

/// <summary>
/// Starts a mission by generating a random tick for the mission and scheduling a mission event.
/// Initializes the missions by registering event handlers for mission events.
/// </summary>
/// <param name="mission">The mission to start.</param>
private void InitializeMissions()
private void initializeMissions()
{
// Register event handlers for mission events.
List<MissionEvent> missionEvents = eventManager
.GetEventsByType<GameEvent>()
.GetEventsByType<MissionEvent>()
.OfType<MissionEvent>()
.ToList();

Expand Down
39 changes: 39 additions & 0 deletions Assets/Scripts/Managers/NarrativeEventManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using System.Linq;
using System;

/// <summary>
///
/// </summary>
public class NarrativeEventManager
{
private GameEventManager eventManager;

public NarrativeEventManager(GameEventManager eventManager)
{
this.eventManager = eventManager;

initializeNarrativeEvents();
}

/// <summary>
///
/// </summary>
private void initializeNarrativeEvents()
{
List<NarrativeEvent> narrativeEvents = eventManager
.GetEventsByType<NarrativeEvent>()
.OfType<NarrativeEvent>()
.ToList();

foreach(NarrativeEvent narrativeEvent in narrativeEvents)
{
List<IConditional> conditionals = narrativeEvent.ConditionalParams.Aggregate(new List<IConditional>(), (acc, parameters) => {
acc.Add(ConditionalFactory.CreateConditional(narrativeEvent.Conditionals, parameters));
return acc;
});

}

}
}
5 changes: 2 additions & 3 deletions Assets/Tests/EditMode/Managers/GameManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NUnit.Framework;
using UnityEngine.TestTools;
using System.Collections;

// Mock GameEvent for testing
Expand Down Expand Up @@ -55,12 +54,12 @@ public void TestEventExecutesAtScheduledTick()
// Simulate 4 ticks (the event should not trigger yet)
for (int i = 0; i < 4; i++)
{
gameManager.Update(1f); // Simulate 1 second of game time (1 tick)
gameManager.Update(1f); // Simulate 1 second of game time (1 tick)
Assert.IsFalse(mockEvent.WasExecuted, "Event should not have been executed yet.");
}

// Simulate 1 more tick (the 5th tick, where the event is scheduled)
gameManager.Update(1f); // Simulate 1 second of game time (1 tick)
gameManager.Update(1f); // Simulate 1 second of game time (1 tick)

Assert.IsTrue(mockEvent.WasExecuted, "Event should have been executed at tick 5.");
}
Expand Down
1 change: 0 additions & 1 deletion Assets/Tests/EditMode/Managers/SaveGameManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.TestTools;

[TestFixture]
public class SaveGameManagerTests
Expand Down

0 comments on commit 0fdbb25

Please sign in to comment.