End of refactor for the Entities

This commit is contained in:
Sebastian Bularca
2026-03-30 00:33:07 +02:00
parent 00c1764fdb
commit 30f319a52d
11 changed files with 215 additions and 210 deletions

View File

@@ -1,23 +1,29 @@
using Jovian.InspectorTools;
using Jovian.Logger;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Nox.Game {
public interface IPerk {
Guid Id { get; }
string Name { get; }
ModifiersData Modifiers { get; }
}
public interface IPerkFactory {
IReadOnlyCollection<PerkDefinition> GetAll();
PerkDefinition GetById(Guid perkId);
IReadOnlyCollection<PerkDefinition> GetPerksFor(CharacterDefinition character);
bool TryAddPerk(CharacterDefinition character, Guid perkId);
IReadOnlyCollection<IPerk> GetAll();
IPerk GetById(Guid perkId);
IReadOnlyCollection<IPerk> GetPerksFor(IEntityDefinition character);
bool TryAddPerk(IEntityDefinition character, Guid perkId);
}
[Serializable]
public sealed class PerkDefinition {
public string name;
[ReadOnly]
public Guid id;
public ModifiersData modifiers = new ();
public sealed class PerkDefinition : IPerk {
[field: SerializeField] public string Name { get; set; }
[field: SerializeField] public ModifiersData Modifiers { get; set; }
public Guid Id { get; set; }
}
[Serializable]
@@ -26,7 +32,7 @@ namespace Nox.Game {
}
public sealed class PerkFactory : IPerkFactory {
private readonly Dictionary<Guid, PerkDefinition> perkPool = new ();
private readonly Dictionary<Guid, IPerk> perkPool = new ();
public PerkFactory(PerksRegistry perksRegistry) {
if(!perksRegistry) {
@@ -34,37 +40,41 @@ namespace Nox.Game {
}
var allAvailablePerks = perksRegistry.perksData;
foreach(var perk in allAvailablePerks.perks) {
perkPool.Add(perk.id, perk);
perkPool.Add(perk.Id, perk);
}
}
public IReadOnlyCollection<PerkDefinition> GetAll() {
public IReadOnlyCollection<IPerk> GetAll() {
return perkPool.Values.ToList();
}
public PerkDefinition GetById(Guid perkId) {
public IPerk GetById(Guid perkId) {
perkPool.TryGetValue(perkId, out var perk);
return perk;
}
public IReadOnlyCollection<PerkDefinition> GetPerksFor(CharacterDefinition character) {
public IReadOnlyCollection<IPerk> GetPerksFor(IEntityDefinition character) {
if(character == null) {
return perkPool.Values.ToList();
}
var ownedPerkIds = character.perksData.perks
.Select(p => p.id)
var ownedPerkIds = character.Perks.perks
.Select(p => p.Id)
.ToHashSet();
return perkPool.Values.Where(p => !ownedPerkIds.Contains(p.id)).ToList();
return perkPool.Values.Where(p => !ownedPerkIds.Contains(p.Id)).ToList();
}
public bool TryAddPerk(CharacterDefinition character, Guid perkId) {
public bool TryAddPerk(IEntityDefinition character, Guid perkId) {
if(character == null) {
return false;
}
if(character.perksData.perks.Any(p => p != null && p.id == perkId)) {
if(perkId == Guid.Empty) {
GlobalLogger.LogException("Cannot add a perk with an empty Id!", LogCategory.GameLogic);
}
if(character.Perks.perks.Any(p => p != null && p.Id == perkId)) {
return false;
}
@@ -72,10 +82,11 @@ namespace Nox.Game {
return false;
}
character.perksData.perks.Add(new PerkDefinition {
id = perk.id,
name = perk.name
character.Perks.perks.Add(new PerkDefinition {
Id = perk.Id,
Name = perk.Name
});
return true;
}
}