using System.Collections.Generic; using System.Linq; using Goap.Core; using UnityEditor; using UnityEngine; namespace Goap.Runtime { [CreateAssetMenu(menuName = "XR-MOD/Tools/Goap/CapabilityConfig")] public class CapabilityConfigScriptable : ScriptableCapabilityFactoryBase { public List goals = new(); public List actions = new(); public List worldSensors = new(); public List targetSensors = new(); public List multiSensors = new(); [SerializeField] public GeneratorScriptable generatorScriptable; public override ICapabilityConfig Create() { var generator = this.GetGenerator(); return new CapabilityConfig(this.name) { Goals = this.GetGoals(generator), Actions = this.GetActions(generator), WorldSensors = this.GetWorldSensors(generator), TargetSensors = this.GetTargetSensors(generator), MultiSensors = this.GetMultiSensors(generator), }; } public List GetActions(GeneratorScriptable generator) { if (generator == null) return new List(); var actionClasses = generator.GetActions(); var targetClasses = generator.GetTargetKeys(); return this.actions.Select(x => new ActionConfig { Name = x.action.Name, ClassType = x.action.GetScript(actionClasses).GetFullName(), BaseCost = x.baseCost, Target = x.target.GetScript(targetClasses).GetInstance(), StoppingDistance = x.stoppingDistance, ValidateTarget = x.validateTarget, RequiresTarget = x.requiresTarget, ValidateConditions = x.validateConditions, Conditions = x.conditions.Select(y => new Condition { WorldKey = y.worldKey.GetScript(generator.GetWorldKeys()).GetInstance(), Comparison = y.comparison, Amount = y.amount, }).Cast().ToArray(), Effects = x.effects.Select(y => new Effect { WorldKey = y.worldKey.GetScript(generator.GetWorldKeys()).GetInstance(), Increase = y.effect == EffectType.Increase, }).Cast().ToArray(), MoveMode = x.moveMode, Properties = x.properties, }).Cast().ToList(); } public List GetGoals(GeneratorScriptable generator) { if (generator == null) return new List(); var goalClasses = generator.GetGoals(); return this.goals.Select(x => new GoalConfig { Name = x.goal.Name, ClassType = x.goal.GetScript(goalClasses).GetFullName(), Conditions = x.conditions.Select(y => new Condition { WorldKey = y.worldKey.GetScript(generator.GetWorldKeys()).GetInstance(), Comparison = y.comparison, Amount = y.amount, }).Cast().ToList(), }).Cast().ToList(); } public List GetWorldSensors(GeneratorScriptable generator) { if (generator == null) return new List(); var sensorClasses = generator.GetWorldSensors(); return this.worldSensors.Select(x => new WorldSensorConfig { Name = x.sensor.Name, ClassType = x.sensor.GetScript(sensorClasses).GetFullName(), Key = x.worldKey.GetScript(generator.GetWorldKeys()).GetInstance(), }).Cast().ToList(); } public List GetTargetSensors(GeneratorScriptable generator) { if (generator == null) return new List(); var sensorClasses = generator.GetTargetSensors(); return this.targetSensors.Select(x => new TargetSensorConfig { Name = x.sensor.Name, ClassType = x.sensor.GetScript(sensorClasses).GetFullName(), Key = x.targetKey.GetScript(generator.GetTargetKeys()).GetInstance(), }).Cast().ToList(); } public List GetMultiSensors(GeneratorScriptable generator) { if (generator == null) return new List(); var sensorClasses = generator.GetMultiSensors(); return this.multiSensors.Select(x => new MultiSensorConfig { Name = x.sensor.Name, ClassType = x.sensor.GetScript(sensorClasses).GetFullName(), }).Cast().ToList(); } public GeneratorScriptable GetGenerator() { #if UNITY_EDITOR this.generatorScriptable = ClassScanner.GetGenerator(this); EditorUtility.SetDirty(this); #endif return this.generatorScriptable; } } }