First commit

This commit is contained in:
sbularca
2026-05-19 15:52:04 +02:00
commit 27b7aeee46
1660 changed files with 240354 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Nox.Game {
public class AnswerReference : MonoBehaviour {
public TextMeshProUGUI number;
public TextMeshProUGUI dialogText;
public Button button;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0c14350aeeed44a28d98d22c6ef048dc
timeCreated: 1777235448

View File

@@ -0,0 +1,146 @@
using Jovian.EncounterSystem;
using Jovian.ZoneSystem;
using UnityEngine;
namespace Nox.Game {
public class EncounterHandler {
private readonly ZoneSystem zoneSystem;
private readonly EncounterRegistry encounterRegistry;
private readonly EncounterView encounterView;
private string previousZoneId;
private IEncounter activeEncounter;
public EncounterHandler(ZoneSystem zoneSystem, EncounterRegistry encounterRegistry, EncounterPrefabs encounterPrefabs) {
this.zoneSystem = zoneSystem;
this.encounterRegistry = encounterRegistry;
encounterView = new EncounterView(encounterPrefabs);
encounterView.OptionSelected += OnOptionSelected;
}
public bool AskForRandomEncounter(ZoneContext zoneContext, string encounterTableId, out IEncounter encounter) {
return ResolveEncounter(zoneContext, encounterTableId, out encounter);
}
public bool AskForRandomEncounterOfType(ZoneContext zoneContext, string encounterTableId, out IEncounter encounter, IEncounterKind encounterKind) {
return ResolveEncounter(zoneContext, encounterTableId, out encounter, encounterKind);
}
public bool AskForEncounter(string encounterId, out IEncounter encounter) {
return encounterRegistry.GetEncounters().TryGetValue(encounterId, out encounter);
}
private bool ResolveEncounter(ZoneContext zoneContext, string encounterTableId, out IEncounter encounter, IEncounterKind encounterKind = null) {
encounter = null;
if(zoneContext.isSafe) {
return false;
}
var randomChance = Random.Range(0f, 1f);
var shouldTrigger = randomChance <= zoneContext.finalEncounterChance;
if(!shouldTrigger) {
Debug.Log($"Rolled for encounter '{encounterTableId}': {randomChance:F2}/{zoneContext.finalEncounterChance:F2} -> none");
return false;
}
encounter = encounterKind == null
? encounterRegistry.GetRandomEncounter(encounterTableId)
: encounterRegistry.GetRandomEncounter(encounterTableId, encounterKind);
var resultName = encounter?.EncounterDefinition?.name ?? "none";
Debug.Log($"Rolled for encounter '{encounterTableId}': {randomChance:F2}/{zoneContext.finalEncounterChance:F2} -> {resultName}");
return encounter != null;
}
private void VerifyZones(Vector3 position) {
var zoneContext = zoneSystem.QueryZone(position);
if(string.IsNullOrEmpty(zoneContext.resolvedZoneId)) {
return;
}
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}");
}
previousZoneId = currentZoneId;
}
}
private void TriggerEncounter(IEncounter encounter) {
switch(encounter.EncounterDefinition.Kind) {
case CombatKind:
return;
default:
activeEncounter = encounter;
encounterView?.SetCurrentEncounter(encounter);
encounterView?.Show();
break;
}
}
private void OnOptionSelected(int optionIndex) {
if(activeEncounter == null) {
return;
}
var options = activeEncounter.EncounterDialogOptionSet?.options;
if(options == null || optionIndex < 0 || optionIndex >= options.Count) {
return;
}
ResolveOption(activeEncounter, options[optionIndex]);
encounterView?.Hide();
activeEncounter = null;
}
private void ResolveOption(IEncounter encounter, EncounterDialogOption option) {
if(option?.events == null) {
return;
}
for(var i = 0; i < option.events.Count; i++) {
var encounterEvent = option.events[i];
if(encounterEvent == null) {
continue;
}
switch(encounterEvent) {
case ChainToEncounterEvent chain:
if(AskForEncounter(chain.nextEncounterId, out var next)) {
TriggerEncounter(next);
return;
}
break;
case LogEvent log:
Debug.Log($"[Encounter '{encounter.EncounterDefinition.id}'] {log.message}");
break;
case StartCombatEvent _:
case GiveRewardEvent _:
Debug.Log($"[Encounter] unhandled event {encounterEvent.GetType().Name}");
break;
}
}
}
public void CheckForEncounters(Vector3 position) {
VerifyZones(position);
}
public void Tick() { }
public void Dispose() {
if(encounterView != null) {
encounterView.OptionSelected -= OnOptionSelected;
encounterView.Dispose();
}
activeEncounter = null;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 523274f9158f453dbfac02601a77c3f7
timeCreated: 1776506833

View File

@@ -0,0 +1,25 @@
using Jovian.EncounterSystem;
using System;
namespace Nox.Game {
[Serializable]
public class CombatKind : IEncounterKind { }
[Serializable]
public class SocialKind : IEncounterKind { }
[Serializable]
public class PuzzleKind : IEncounterKind { }
[Serializable]
public class ExplorationKind : IEncounterKind { }
[Serializable]
public class TutorialKind : IEncounterKind { }
[Serializable]
public class HazardKind : IEncounterKind { }
[Serializable]
public class OtherKind : IEncounterKind { }
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: abf01048c404a0240ad538b65a15f5fb

View File

@@ -0,0 +1,18 @@
using Jovian.EncounterSystem;
using System;
using UnityEngine;
namespace Nox.Game {
[CreateAssetMenu(fileName = "EncounterPrefabs", menuName = "Nox/EncounterPrefabs")]
public class EncounterPrefabs : ScriptableObject {
public EncounterSet[] encounterSets;
}
[Serializable]
public class EncounterSet {
[field: SerializeReference, SubclassSelector]
public IEncounterKind encounterKind;
public EncounterReference encounterReference;
public AnswerReference answerReference;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7fc36c84acd04836b1280aba57a64e24
timeCreated: 1777229589

View File

@@ -0,0 +1,167 @@
using Jovian.EncounterSystem;
using Nox.Game.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Nox.Game {
public class EncounterView : IMenuView {
private const int MaxAnswers = 4;
private readonly EncounterPrefabs encounterPrefabs;
private readonly Dictionary<Type, EncounterReference> kindToReference = new();
private readonly Dictionary<Type, List<AnswerReference>> kindToAnswerPool = new();
private IEncounter currentEncounter;
private EncounterReference currentReference;
private List<AnswerReference> currentAnswerPool;
public event Action<int> OptionSelected;
public EncounterView(EncounterPrefabs encounterPrefabs) {
this.encounterPrefabs = encounterPrefabs;
}
public void SetCurrentEncounter(IEncounter encounter) {
currentEncounter = encounter;
}
public void Initialize() { }
public void Show() {
if(currentEncounter?.EncounterDefinition?.Kind == null) {
return;
}
if(currentReference) {
currentReference.gameObject.SetActive(false);
}
var kindType = currentEncounter.EncounterDefinition.Kind.GetType();
var set = encounterPrefabs.encounterSets
.FirstOrDefault(s => s.encounterKind != null && s.encounterKind.GetType() == kindType);
if(set == null || !set.encounterReference) {
return;
}
if(!kindToReference.TryGetValue(kindType, out var reference) || !reference) {
reference = UnityEngine.Object.Instantiate(set.encounterReference);
kindToReference[kindType] = reference;
}
currentReference = reference;
currentAnswerPool = GetOrBuildAnswerPool(kindType, set);
PopulateEncounterReference();
currentReference.gameObject.SetActive(true);
}
public void Hide() {
if(currentReference) {
currentReference.gameObject.SetActive(false);
}
DeactivateAnswers(currentAnswerPool);
}
public void Tick() { }
private List<AnswerReference> GetOrBuildAnswerPool(Type kindType, EncounterSet set) {
if(kindToAnswerPool.TryGetValue(kindType, out var pool) && pool != null) {
return pool;
}
pool = new List<AnswerReference>(MaxAnswers);
kindToAnswerPool[kindType] = pool;
if(!set.answerReference || !currentReference.encounterOptionsContainer) {
return pool;
}
for(var i = 0; i < MaxAnswers; i++) {
var answer = UnityEngine.Object.Instantiate(set.answerReference, currentReference.encounterOptionsContainer);
answer.gameObject.SetActive(false);
pool.Add(answer);
}
return pool;
}
private void PopulateEncounterReference() {
var definition = currentEncounter.EncounterDefinition;
var visuals = currentEncounter.EncounterVisuals;
if(currentReference.encounterName) {
currentReference.encounterName.text = definition.name;
}
if(currentReference.encounterDescription) {
currentReference.encounterDescription.text = definition.description;
}
if(currentReference.encounterArt && visuals != null) {
currentReference.encounterArt.sprite = visuals.encounterArt;
}
PopulateAnswers();
}
private void PopulateAnswers() {
DeactivateAnswers(currentAnswerPool);
var optionSet = currentEncounter.EncounterDialogOptionSet;
if(currentAnswerPool == null || optionSet?.options == null) {
return;
}
var count = Mathf.Min(optionSet.options.Count, currentAnswerPool.Count);
for(var i = 0; i < count; i++) {
var option = optionSet.options[i];
var answer = currentAnswerPool[i];
if(option == null || !answer) {
continue;
}
if(answer.number) {
answer.number.text = (i + 1).ToString();
}
if(answer.dialogText) {
answer.dialogText.text = option.text.Resolve(optionSet.library);
}
BindAnswerButton(answer, i);
answer.gameObject.SetActive(true);
}
}
private void BindAnswerButton(AnswerReference answer, int optionIndex) {
var button = answer.button ? answer.button : answer.GetComponentInChildren<UnityEngine.UI.Button>(true);
if(!button) {
return;
}
button.onClick.RemoveAllListeners();
button.onClick.AddListener(() => OptionSelected?.Invoke(optionIndex));
}
private static void DeactivateAnswers(List<AnswerReference> pool) {
if(pool == null) {
return;
}
for(var i = 0; i < pool.Count; i++) {
if(pool[i]) {
pool[i].gameObject.SetActive(false);
}
}
}
public void Dispose() {
foreach(var reference in kindToReference.Values) {
if(reference) {
UnityEngine.Object.Destroy(reference.gameObject);
}
}
kindToReference.Clear();
kindToAnswerPool.Clear();
currentReference = null;
currentAnswerPool = null;
currentEncounter = null;
OptionSelected = null;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b69490c9f1d8471a84b4594d1b1be117
timeCreated: 1776590016

View File

@@ -0,0 +1,30 @@
using Jovian.EncounterSystem;
using System;
namespace Nox.Game {
[Serializable]
public class CurrencyRewardKind : IRewardKind {
public string currencyId;
public int amount;
}
[Serializable]
public class ItemRewardKind : IRewardKind {
public string itemId;
public int quantity;
}
[Serializable]
public class ExperienceRewardKind : IRewardKind {
public int amount;
}
[Serializable]
public class UnlockableRewardKind : IRewardKind {
public string unlockableId;
}
[Serializable]
public class OtherRewardKind : IRewardKind {
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 7895041af87249e4c8458a07b0bc6b2c