forked from Shardstone/trail-into-darkness
factored the character system - not funtional yet
This commit is contained in:
73
Assets/Code/GameState/Entities/PerkFactory.cs
Normal file
73
Assets/Code/GameState/Entities/PerkFactory.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user