Wired in the encounters triggering and saving
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using Jovian.Calendar;
|
||||
using Jovian.EncounterSystem;
|
||||
using Jovian.PopupSystem;
|
||||
@@ -8,18 +9,23 @@ using Nox.Core;
|
||||
using Nox.Platform;
|
||||
using Nox.Game.UI;
|
||||
using Nox.UI;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using ZLinq;
|
||||
using Object = UnityEngine.Object;
|
||||
using PlayMode = Nox.Core.PlayMode;
|
||||
|
||||
namespace Nox.Game {
|
||||
[Serializable]
|
||||
public class AdventureData {
|
||||
public bool isPartyMoving;
|
||||
public bool isEncounterActive;
|
||||
public int currentDay = 0;
|
||||
public int suppliesAvailable = -1;
|
||||
public float currentTime = -1f;
|
||||
public DayPhase currentDayPhase = DayPhase.Morning;
|
||||
public List<string> completedEncounterIds = new List<string>();
|
||||
}
|
||||
|
||||
public class AdventurePlayMode : IPlayMode {
|
||||
@@ -115,12 +121,15 @@ namespace Nox.Game {
|
||||
partyInventoryHandler ??= new PartyInventoryHandler(adventureData, adventureSettings);
|
||||
partyInventoryHandler.Initialize();
|
||||
|
||||
var calendarSettings = Addressables.LoadAssetAsync<CalendarSettings>("CalendarSettings").WaitForCompletion();
|
||||
var calendarSettings = Addressables.LoadAssetAsync<CalendarSettings>("CalendarSettings").WaitForCompletion();
|
||||
var worldClock = new WorldClock(calendarSettings);
|
||||
var totalMinutes = adventureData.currentDay * calendarSettings.MinutesPerDay
|
||||
+ (int)(adventureData.currentTime * calendarSettings.MinutesPerDay);
|
||||
worldClock.AdvanceMinutes(totalMinutes);
|
||||
timeHandler ??= new TimeHandler(adventureSettings, adventureData, worldClock);
|
||||
|
||||
zoneSystem ??= new ZoneSystem(mapRef.zonesObjectHolder);
|
||||
encounterHandler = new EncounterHandler(zoneSystem, encounterRegistry, encounterPrefabs);
|
||||
encounterHandler = new EncounterHandler(zoneSystem, encounterRegistry, encounterPrefabs, adventureData);
|
||||
partyMovementHandler ??= new PartyMovementHandler(
|
||||
partyRef,
|
||||
cameraController,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Jovian.EncounterSystem;
|
||||
using Jovian.ZoneSystem;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Nox.Game {
|
||||
@@ -7,14 +8,19 @@ namespace Nox.Game {
|
||||
private readonly ZoneSystem zoneSystem;
|
||||
private readonly EncounterRegistry encounterRegistry;
|
||||
private readonly EncounterView encounterView;
|
||||
private readonly AdventureData adventureData;
|
||||
private Vector3 lastKnownPosition;
|
||||
private string previousZoneId;
|
||||
private IEncounter activeEncounter;
|
||||
private int lastCheckedDay;
|
||||
|
||||
public EncounterHandler(ZoneSystem zoneSystem, EncounterRegistry encounterRegistry, EncounterPrefabs encounterPrefabs) {
|
||||
public EncounterHandler(ZoneSystem zoneSystem, EncounterRegistry encounterRegistry, EncounterPrefabs encounterPrefabs, AdventureData adventureData) {
|
||||
this.zoneSystem = zoneSystem;
|
||||
this.encounterRegistry = encounterRegistry;
|
||||
this.adventureData = adventureData;
|
||||
encounterView = new EncounterView(encounterPrefabs);
|
||||
encounterView.OptionSelected += OnOptionSelected;
|
||||
lastCheckedDay = adventureData.currentDay;
|
||||
}
|
||||
|
||||
public bool AskForRandomEncounter(ZoneContext zoneContext, string encounterTableId, out IEncounter encounter) {
|
||||
@@ -35,7 +41,7 @@ namespace Nox.Game {
|
||||
return false;
|
||||
}
|
||||
|
||||
var randomChance = Random.Range(0f, 1f);
|
||||
var randomChance = UnityEngine.Random.Range(0f, 1f);
|
||||
var shouldTrigger = randomChance <= zoneContext.finalEncounterChance;
|
||||
|
||||
if(!shouldTrigger) {
|
||||
@@ -43,29 +49,72 @@ namespace Nox.Game {
|
||||
return false;
|
||||
}
|
||||
|
||||
encounter = encounterKind == null
|
||||
? encounterRegistry.GetRandomEncounter(encounterTableId)
|
||||
: encounterRegistry.GetRandomEncounter(encounterTableId, encounterKind);
|
||||
var filter = new Predicate<IEncounter>(e => e != null && !IsAlreadyCompleted(e));
|
||||
|
||||
var resultName = encounter?.EncounterDefinition?.name ?? "none";
|
||||
Debug.Log($"Rolled for encounter '{encounterTableId}': {randomChance:F2}/{zoneContext.finalEncounterChance:F2} -> {resultName}");
|
||||
if(encounterKind == null) {
|
||||
encounter = encounterRegistry.GetRandomEncounter(filter, encounterTableId);
|
||||
}
|
||||
else {
|
||||
var table = encounterRegistry.GetEncounterTable(encounterTableId);
|
||||
if(table == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for(var attempt = 0; attempt <= table.encounters.Count; attempt++) {
|
||||
encounter = encounterRegistry.GetRandomEncounter(encounterTableId, encounterKind);
|
||||
if(encounter == null || !IsAlreadyCompleted(encounter)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(encounter != null && IsAlreadyCompleted(encounter)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
var name = encounter?.EncounterDefinition?.name ?? "none";
|
||||
Debug.Log($"Rolled for encounter '{encounterTableId}': {randomChance:F2}/{zoneContext.finalEncounterChance:F2} -> {name}");
|
||||
return encounter != null;
|
||||
}
|
||||
|
||||
private void VerifyZones(Vector3 position) {
|
||||
var zoneContext = zoneSystem.QueryZone(position);
|
||||
if(string.IsNullOrEmpty(zoneContext.resolvedZoneId)) {
|
||||
private bool IsAlreadyCompleted(IEncounter encounter) {
|
||||
var def = encounter?.EncounterDefinition;
|
||||
if(def == null || string.IsNullOrEmpty(def.id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// no properties → defaults to repeatable (true). So can't be "already completed".
|
||||
if(encounter.EncounterProperties is not { isRepeatable: false }) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return adventureData.completedEncounterIds.Contains(def.id);
|
||||
}
|
||||
|
||||
private void RecordEncounterCompleted(IEncounter encounter) {
|
||||
var def = encounter?.EncounterDefinition;
|
||||
if(def == null || string.IsNullOrEmpty(def.id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// only record non-repeatable encounters that have explicit properties
|
||||
if(encounter.EncounterProperties is not { isRepeatable: false }) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(!adventureData.completedEncounterIds.Contains(def.id)) {
|
||||
adventureData.completedEncounterIds.Add(def.id);
|
||||
Debug.Log($"Recorded non-repeatable encounter: {def.id}");
|
||||
}
|
||||
}
|
||||
|
||||
private void TrackZoneTransition(Vector3 position) {
|
||||
var zoneContext = zoneSystem.QueryZone(position);
|
||||
var currentZoneId = zoneContext.resolvedZoneId;
|
||||
|
||||
if(currentZoneId != previousZoneId) {
|
||||
if(!string.IsNullOrEmpty(currentZoneId)) {
|
||||
Debug.Log($"Entered zone: {currentZoneId} (encounter: {zoneContext.encounterTableId}, safe: {zoneContext.isSafe})");
|
||||
if(ResolveEncounter(zoneContext, zoneContext.encounterTableId, out var encounter)) {
|
||||
TriggerEncounter(encounter);
|
||||
}
|
||||
|
||||
}
|
||||
else if(!string.IsNullOrEmpty(previousZoneId)) {
|
||||
Debug.Log($"Left zone: {previousZoneId}");
|
||||
@@ -74,18 +123,36 @@ namespace Nox.Game {
|
||||
}
|
||||
}
|
||||
|
||||
private void DailyEncounterCheck(Vector3 position) {
|
||||
var zoneContext = zoneSystem.QueryZone(position);
|
||||
if(string.IsNullOrEmpty(zoneContext.resolvedZoneId) || zoneContext.isSafe) {
|
||||
return;
|
||||
}
|
||||
if(ResolveEncounter(zoneContext, zoneContext.encounterTableId, out var encounter)) {
|
||||
TriggerEncounter(encounter);
|
||||
}
|
||||
}
|
||||
|
||||
private void TriggerEncounter(IEncounter encounter) {
|
||||
switch(encounter.EncounterDefinition.Kind) {
|
||||
case CombatKind:
|
||||
return;
|
||||
default:
|
||||
activeEncounter = encounter;
|
||||
adventureData.isEncounterActive = true;
|
||||
encounterView?.SetCurrentEncounter(encounter);
|
||||
encounterView?.Show();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void FinishEncounter() {
|
||||
RecordEncounterCompleted(activeEncounter);
|
||||
encounterView?.Hide();
|
||||
activeEncounter = null;
|
||||
adventureData.isEncounterActive = false;
|
||||
}
|
||||
|
||||
private void OnOptionSelected(int optionIndex) {
|
||||
if(activeEncounter == null) {
|
||||
return;
|
||||
@@ -93,16 +160,16 @@ namespace Nox.Game {
|
||||
|
||||
var options = activeEncounter.EncounterDialogOptionSet?.options;
|
||||
if(options == null || optionIndex < 0 || optionIndex >= options.Count) {
|
||||
FinishEncounter();
|
||||
return;
|
||||
}
|
||||
|
||||
ResolveOption(activeEncounter, options[optionIndex]);
|
||||
encounterView?.Hide();
|
||||
activeEncounter = null;
|
||||
}
|
||||
|
||||
private void ResolveOption(IEncounter encounter, EncounterDialogOption option) {
|
||||
if(option?.events == null) {
|
||||
FinishEncounter();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -114,6 +181,7 @@ namespace Nox.Game {
|
||||
switch(encounterEvent) {
|
||||
case ChainToEncounterEvent chain:
|
||||
if(AskForEncounter(chain.nextEncounterId, out var next)) {
|
||||
RecordEncounterCompleted(activeEncounter);
|
||||
TriggerEncounter(next);
|
||||
return;
|
||||
}
|
||||
@@ -127,13 +195,21 @@ namespace Nox.Game {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
FinishEncounter();
|
||||
}
|
||||
|
||||
public void CheckForEncounters(Vector3 position) {
|
||||
VerifyZones(position);
|
||||
lastKnownPosition = position;
|
||||
TrackZoneTransition(position);
|
||||
}
|
||||
|
||||
public void Tick() { }
|
||||
public void Tick() {
|
||||
if(adventureData.currentDay != lastCheckedDay) {
|
||||
lastCheckedDay = adventureData.currentDay;
|
||||
DailyEncounterCheck(lastKnownPosition);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
if(encounterView != null) {
|
||||
@@ -141,6 +217,7 @@ namespace Nox.Game {
|
||||
encounterView.Dispose();
|
||||
}
|
||||
activeEncounter = null;
|
||||
adventureData.isEncounterActive = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,6 +99,12 @@ namespace Nox.Game {
|
||||
HandleClickableLocation();
|
||||
}
|
||||
|
||||
if(adventureData.isEncounterActive) {
|
||||
partyCanMove = false;
|
||||
adventureData.isPartyMoving = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if(!partyCanMove) {
|
||||
adventureData.isPartyMoving = false;
|
||||
return;
|
||||
@@ -111,11 +117,12 @@ namespace Nox.Game {
|
||||
}
|
||||
|
||||
adventureData.isPartyMoving = true;
|
||||
partyReference.transform.position = Vector3.MoveTowards(
|
||||
var pos = Vector3.MoveTowards(
|
||||
new Vector3(partyReference.transform.position.x, 0,
|
||||
partyReference.transform.position.z), targetPosition,
|
||||
Time.deltaTime * adventureSettings.partyBaseSpeed);
|
||||
encounterHandler?.CheckForEncounters(partyReference.transform.position);
|
||||
partyReference.transform.position = pos;
|
||||
encounterHandler?.CheckForEncounters(pos);
|
||||
}
|
||||
|
||||
private void HandleHover() {
|
||||
|
||||
@@ -44,6 +44,7 @@ namespace Nox.Game {
|
||||
|
||||
if(localTime >= adventureSettings.dayLength) {
|
||||
localTime -= adventureSettings.dayLength;
|
||||
adventureData.currentDay++;
|
||||
}
|
||||
|
||||
var normalized = localTime / adventureSettings.dayLength;
|
||||
|
||||
Reference in New Issue
Block a user