using System; using System.Collections.Generic; using System.Linq; using Goap.Core; namespace Goap.Runtime { public class AgentType : IAgentType { public string Id { get; } public IAgentCollection Agents { get; } public IGoapConfig GoapConfig { get; } public ISensorRunner SensorRunner { get; } public IAgentTypeEvents Events { get; } = new AgentTypeEvents(); public IGlobalWorldData WorldData { get; } private List goals; private Dictionary goalsLookup; private List actions; public AgentType(string id, IGoapConfig config, List goals, List actions, ISensorRunner sensorRunner, IGlobalWorldData worldData) { this.Id = id; this.GoapConfig = config; this.SensorRunner = sensorRunner; this.goals = goals; this.goalsLookup = goals.ToDictionary(TypeReSolveHelper.ResolveType); this.actions = actions; this.WorldData = worldData; this.Agents = new AgentCollection(this); } public void Register(IMonoGoapActionProvider actionProvider) { this.Agents.Add(actionProvider); } public void Unregister(IMonoGoapActionProvider actionProvider) { this.Agents.Remove(actionProvider); } public TGoal ResolveGoal() where TGoal : IGoal { if (this.goalsLookup.TryGetValue(typeof(TGoal), out var result)) return (TGoal) result; throw new KeyNotFoundException($"No action found of type {typeof(TGoal)}"); } public IGoal ResolveGoal(Type _goalType) { if (this.goalsLookup.TryGetValue(_goalType, out var result)) return result; throw new KeyNotFoundException($"No action found of type {_goalType}"); } public bool AllConditionsMet(IGoapActionProvider actionProvider, IGoapAction action) { var conditionObserver = this.GoapConfig.ConditionObserver; conditionObserver.SetWorldData(actionProvider.WorldData); foreach (var condition in action.Conditions) { if (!conditionObserver.IsMet(condition)) return false; } return true; } public List GetAllNodes() => this.actions.Cast().Concat(this.goals).ToList(); public List GetActions() => this.actions; public List GetActions() where TAction : IGoapAction => this.actions.OfType().ToList(); public List GetGoals() => this.goals; } }