81 lines
3.2 KiB
C#
81 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Jovian.EncounterSystem {
|
|
/// <summary>Tag for a quest log entry.</summary>
|
|
public enum QuestLogEventType {
|
|
Started,
|
|
Advanced,
|
|
Completed
|
|
}
|
|
|
|
/// <summary>
|
|
/// One chronological entry in the quest log. Names are cached alongside ids so a loaded save
|
|
/// can display the log even if the underlying encounter has since been renamed or removed.
|
|
/// </summary>
|
|
[Serializable]
|
|
public class QuestLogEntry {
|
|
public QuestLogEventType type;
|
|
public string encounterInternalId;
|
|
public string encounterName;
|
|
public string fromEncounterName;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Chronological, serializable record of quest events. Subscribes to <see cref="QuestProgress"/>
|
|
/// events at construction time — build it before any encounter fires so nothing is missed.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This is the save payload for quest progression. On load, call <see cref="Restore"/> with the
|
|
/// saved entries and then <see cref="QuestProgress.LoadResolvedIds"/> with
|
|
/// <see cref="ResolvedEncounterIds"/> to rehydrate the gating set.
|
|
/// </remarks>
|
|
public class QuestLog {
|
|
private readonly List<QuestLogEntry> entries = new();
|
|
|
|
/// <summary>The log in chronological order.</summary>
|
|
public IReadOnlyList<QuestLogEntry> Entries => entries;
|
|
|
|
/// <summary>Subscribe to <paramref name="progress"/>'s quest events; every fire appends an entry.</summary>
|
|
public QuestLog(QuestProgress progress) {
|
|
progress.QuestStarted += quest => Record(QuestLogEventType.Started, null, quest);
|
|
progress.QuestAdvanced += (from, to) => Record(QuestLogEventType.Advanced, from, to);
|
|
progress.QuestCompleted += quest => Record(QuestLogEventType.Completed, null, quest);
|
|
}
|
|
|
|
/// <summary>Return a copy of the current entries suitable for serialization.</summary>
|
|
public List<QuestLogEntry> CreateSnapshot() {
|
|
return new List<QuestLogEntry>(entries);
|
|
}
|
|
|
|
/// <summary>Replace the current entries with those from a save. Pass the list straight from
|
|
/// the save payload.</summary>
|
|
public void Restore(IEnumerable<QuestLogEntry> saved) {
|
|
entries.Clear();
|
|
if(saved == null) {
|
|
return;
|
|
}
|
|
|
|
entries.AddRange(saved);
|
|
}
|
|
|
|
/// <summary>Enumerate the distinct encounter ids present in the log — what
|
|
/// <see cref="QuestProgress.LoadResolvedIds"/> needs on load.</summary>
|
|
public IEnumerable<string> ResolvedEncounterIds() {
|
|
return entries
|
|
.Select(entry => entry.encounterInternalId)
|
|
.Where(id => !string.IsNullOrEmpty(id));
|
|
}
|
|
|
|
private void Record(QuestLogEventType type, IEncounter from, IEncounter to) {
|
|
entries.Add(new QuestLogEntry {
|
|
type = type,
|
|
encounterInternalId = to?.EncounterDefinition?.internalId,
|
|
encounterName = to?.EncounterDefinition?.name,
|
|
fromEncounterName = from?.EncounterDefinition?.name
|
|
});
|
|
}
|
|
}
|
|
}
|