using System; using System.Collections.Generic; using Goap.Core; using UnityEngine; namespace Goap.Runtime { public class GoalBuilder : GoalBuilder where T : IGoal { public GoalBuilder(WorldKeyBuilder worldKeyBuilder) : base(typeof(T), worldKeyBuilder) { } /// /// Sets the base cost for the goal. /// /// The base cost. /// The current instance of . public GoalBuilder SetBaseCost(float baseCost) { this.config.BaseCost = baseCost; return this; } /// /// Adds a condition to the goal. /// /// The type of the world key. /// The comparison type. /// The amount for the condition. /// The current instance of . public GoalBuilder AddCondition(Comparison comparison, int amount) where TWorldKey : IWorldKey { this.conditions.Add(new Condition(this.worldKeyBuilder.GetKey(), comparison, amount)); return this; } /// /// Sets the callback for the goal. This will be called when the goal is created. /// /// The callback action. /// The current instance of . public GoalBuilder SetCallback(Action callback) { this.config.Callback = (obj) => callback((T) obj); return this; } } public class GoalBuilder { protected readonly GoalConfig config; protected readonly List conditions = new(); protected readonly WorldKeyBuilder worldKeyBuilder; public GoalBuilder(Type _type, WorldKeyBuilder _worldKeyBuilder) { this.worldKeyBuilder = _worldKeyBuilder; this.config = new GoalConfig(_type) { BaseCost = 1, ClassType = _type.AssemblyQualifiedName, }; } /// /// Builds the goal configuration. /// /// The built . public IGoalConfig Build() { this.config.Conditions = this.conditions; return this.config; } public static GoalBuilder Create(WorldKeyBuilder _worldKeyBuilder) where TGoal : IGoal { return new GoalBuilder(_worldKeyBuilder); } public static GoalBuilder Create(Type _goal, WorldKeyBuilder _worldKeyBuilder) { return new GoalBuilder(_goal, _worldKeyBuilder); } public GoalBuilder SetBaseCost(float _baseCost) { this.config.BaseCost = _baseCost; return this; } public GoalBuilder AddCondition(Type _worldKeyType, Comparison _comparison, int _amount) { this.conditions.Add(new Condition(this.worldKeyBuilder.GetKey(_worldKeyType), _comparison, _amount)); return this; } public GoalBuilder SetCallback(Action _callback) { this.config.Callback = _callback; return this; } } }