forked from Shardstone/trail-into-darkness
added encounter system
This commit is contained in:
103
Packages/com.jovian.encounter-system/Runtime/QuestProgress.cs
Normal file
103
Packages/com.jovian.encounter-system/Runtime/QuestProgress.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Jovian.EncounterSystem {
|
||||
/// <summary>
|
||||
/// Gated quest progression. Encounter E is gated iff some QuestKind encounter P has
|
||||
/// <c>P.nextEncounter == E</c> and P hasn't been resolved. Predecessor map is built once at
|
||||
/// construction; rolling and advancement are O(1).
|
||||
/// </summary>
|
||||
public class QuestProgress {
|
||||
private readonly HashSet<string> resolvedIds = new();
|
||||
private readonly Dictionary<string, IEncounter> predecessorOf = new();
|
||||
|
||||
public event Action<IEncounter> QuestStarted;
|
||||
public event Action<IEncounter, IEncounter> QuestAdvanced;
|
||||
public event Action<IEncounter> QuestCompleted;
|
||||
|
||||
public IReadOnlyCollection<string> ResolvedIds => resolvedIds;
|
||||
|
||||
public QuestProgress(EncountersCollection encountersCollection) {
|
||||
IndexQuests(encountersCollection);
|
||||
}
|
||||
|
||||
public bool IsGated(IEncounter encounter) {
|
||||
var id = encounter?.EncounterDefinition?.internalId;
|
||||
if(string.IsNullOrEmpty(id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!predecessorOf.TryGetValue(id, out var predecessor)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !resolvedIds.Contains(predecessor.EncounterDefinition.internalId);
|
||||
}
|
||||
|
||||
public void OnEncounterTriggered(IEncounter encounter) {
|
||||
if(encounter?.Kind is not QuestKind questKind) {
|
||||
return;
|
||||
}
|
||||
|
||||
var id = encounter.EncounterDefinition?.internalId;
|
||||
if(string.IsNullOrEmpty(id) || !resolvedIds.Add(id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
var next = questKind.nextEncounter.Resolve();
|
||||
var hasPredecessor = predecessorOf.TryGetValue(id, out var predecessor);
|
||||
|
||||
if(!hasPredecessor) {
|
||||
QuestStarted?.Invoke(encounter);
|
||||
}
|
||||
else {
|
||||
QuestAdvanced?.Invoke(predecessor, encounter);
|
||||
}
|
||||
|
||||
if(next == null) {
|
||||
QuestCompleted?.Invoke(encounter);
|
||||
}
|
||||
}
|
||||
|
||||
public void LoadResolvedIds(IEnumerable<string> ids) {
|
||||
resolvedIds.Clear();
|
||||
if(ids == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach(var id in ids) {
|
||||
if(!string.IsNullOrEmpty(id)) {
|
||||
resolvedIds.Add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void IndexQuests(EncountersCollection collection) {
|
||||
if(collection?.encounterTables == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach(var table in collection.encounterTables) {
|
||||
if(table?.encounters == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach(var encounter in table.encounters) {
|
||||
if(encounter?.Kind is not QuestKind questKind) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var next = questKind.nextEncounter.Resolve();
|
||||
if(next == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var nextId = next.EncounterDefinition?.internalId;
|
||||
if(!string.IsNullOrEmpty(nextId)) {
|
||||
predecessorOf[nextId] = encounter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user