I was wondering if such a design was a bad idea in C#. Here I have a static class EventLog
which holds a list of instances of Event
class. This is a simple event log of actions players can do in a game. What can I be doing in a better way?
public class EventLog
{
private const int maxEventsInLog = 50;
private static List<Event> events = new List<Event>();
private static string[] eventDescriptions = new string[8]
{
"<fromUnit> walked",
"<fromUnit> waited",
"<fromUnit> inquired",
"<fromUnit> allured <toUnit>",
"<fromUnit> persuaded <toUnit>",
"<fromUnit> bribed <toUnit>",
"<fromUnit> hired <toUnit>",
"<fromUnit> attacked <toUnit>",
};
public static void AddEvent(GameObject _fromUnit, GameObject _toUnit, Utils.ActionType _actionType)
{
events.Add(new Event(_fromUnit, _toUnit, _actionType));
if (events.Count > maxEventsInLog)
{
while (events.Count > maxEventsInLog)
{
events.RemoveAt(0);
}
}
}
public static int NumEventsInLog()
{
return events.Count;
}
public static string GetEventDescription(int eventIndex)
{
if (eventIndex < 0 || eventIndex > events.Count - 1)
return null;
return events[eventIndex].description;
}
private class Event
{
private GameObject fromUnit;
private GameObject toUnit;
private Utils.ActionType actionType;
public string description;
public Event(GameObject _fromUnit, GameObject _toUnit, Utils.ActionType _actionType)
{
fromUnit = _fromUnit;
toUnit = _toUnit;
actionType = _actionType;
description = eventDescriptions[(int)_actionType];
description = description.Replace("<fromUnit>", _fromUnit.GetComponent<UnitData>().firstName + " " + _fromUnit.GetComponent<UnitData>().lastName);
description = description.Replace("<toUnit>", _toUnit.GetComponent<UnitData>().firstName + " " + _toUnit.GetComponent<UnitData>().lastName);
}
}