factored the character system - not funtional yet

This commit is contained in:
Sebastian Bularca
2026-03-22 14:46:19 +01:00
parent 00bb430a7f
commit 0f0189726e
36 changed files with 639 additions and 513 deletions

View File

@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Nox.Game {
public interface IPerkFactory {
IReadOnlyCollection<PerkDefinition> GetAll();
PerkDefinition GetById(string perkId);
IReadOnlyCollection<PerkDefinition> GetPerksFor(CharacterDefinition character);
bool TryAddPerk(CharacterDefinition character, string perkId);
}
public sealed class PerkFactory : IPerkFactory {
private readonly Dictionary<string, PerkDefinition> perkPool = new ();
public PerkFactory(PerkRegistry perkRegistry) {
if(perkRegistry == null) {
throw new ArgumentNullException(nameof(perkRegistry));
}
var allAvailablePerks = perkRegistry.perksData;
foreach(var perk in allAvailablePerks.perks) {
perkPool.Add(perk.id, perk);
}
}
public IReadOnlyCollection<PerkDefinition> GetAll() {
return perkPool.Values.ToList();
}
public PerkDefinition GetById(string perkId) {
if(string.IsNullOrWhiteSpace(perkId)) {
return null;
}
perkPool.TryGetValue(perkId, out var perk);
return perk;
}
public IReadOnlyCollection<PerkDefinition> GetPerksFor(CharacterDefinition character) {
if(character == null) {
return perkPool.Values.ToList();
}
var ownedPerkIds = character.perksData.perks
.Where(p => p != null && !string.IsNullOrWhiteSpace(p.id))
.Select(p => p.id)
.ToHashSet();
return perkPool.Values.Where(p => !ownedPerkIds.Contains(p.id)).ToList();
}
public bool TryAddPerk(CharacterDefinition character, string perkId) {
if(character == null || string.IsNullOrWhiteSpace(perkId)) {
return false;
}
if(character.perksData.perks.Any(p => p != null && p.id == perkId)) {
return false;
}
if(!perkPool.TryGetValue(perkId, out PerkDefinition perk)) {
return false;
}
character.perksData.perks.Add(new PerkDefinition {
id = perk.id,
name = perk.name
});
return true;
}
}
}