Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using CrashKonijn.Goap.Core;
using UnityEngine;
using UnityEngine.Serialization;
Expand All @@ -16,17 +15,40 @@ public class AgentTypeScriptable : ScriptableObject

public IAgentTypeConfig Create()
{
var configs = this.capabilities
.Select(behaviour => behaviour.Create())
.ToList();
// Aggregate lists
var allGoals = new List<IGoalConfig>();
var allActions = new List<IActionConfig>();
var allWorldSensors = new List<IWorldSensorConfig>();
var allTargetSensors = new List<ITargetSensorConfig>();
var allMultiSensors = new List<IMultiSensorConfig>();

for (int i = 0; i < this.capabilities.Count; i++)
{
var capabilityConfig = this.capabilities[i].Create();

if (capabilityConfig.Goals != null)
allGoals.AddRange(capabilityConfig.Goals);

if (capabilityConfig.Actions != null)
allActions.AddRange(capabilityConfig.Actions);

if (capabilityConfig.WorldSensors != null)
allWorldSensors.AddRange(capabilityConfig.WorldSensors);

if (capabilityConfig.TargetSensors != null)
allTargetSensors.AddRange(capabilityConfig.TargetSensors);

if (capabilityConfig.MultiSensors != null)
allMultiSensors.AddRange(capabilityConfig.MultiSensors);
}

return new AgentTypeConfig(this.name)
{
Goals = configs.SelectMany(x => x.Goals).ToList(),
Actions = configs.SelectMany(x => x.Actions).ToList(),
WorldSensors = configs.SelectMany(x => x.WorldSensors).ToList(),
TargetSensors = configs.SelectMany(x => x.TargetSensors).ToList(),
MultiSensors = configs.SelectMany(x => x.MultiSensors).ToList(),
Goals = allGoals,
Actions = allActions,
WorldSensors = allWorldSensors,
TargetSensors = allTargetSensors,
MultiSensors = allMultiSensors,
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using CrashKonijn.Goap.Core;
using UnityEditor;
using UnityEngine;
Expand Down Expand Up @@ -39,31 +38,55 @@ public List<IActionConfig> GetActions(GeneratorScriptable generator)

var actionClasses = generator.GetActions();
var targetClasses = generator.GetTargetKeys();

return this.actions.Select(x => new ActionConfig
var worldKeys = generator.GetWorldKeys();

var configs = new List<IActionConfig>();

foreach (var x in this.actions)
{
Name = x.action.Name,
ClassType = x.action.GetScript(actionClasses).GetFullName(),
BaseCost = x.baseCost,
Target = x.target.GetScript(targetClasses).GetInstance<ITargetKey>(),
StoppingDistance = x.stoppingDistance,
ValidateTarget = x.validateTarget,
RequiresTarget = x.requiresTarget,
ValidateConditions = x.validateConditions,
Conditions = x.conditions.Select(y => new Condition
var conditions = new ICondition[x.conditions.Count];

for (var i = 0; i < x.conditions.Count; i++)
{
var condition = x.conditions[i];
conditions[i] = new Condition
{
WorldKey = condition.worldKey.GetScript(worldKeys).GetInstance<IWorldKey>(),
Comparison = condition.comparison,
Amount = condition.amount,
};
}

var effects = new IEffect[x.effects.Count];

for (var i = 0; i < x.effects.Count; i++)
{
WorldKey = y.worldKey.GetScript(generator.GetWorldKeys()).GetInstance<IWorldKey>(),
Comparison = y.comparison,
Amount = y.amount,
}).Cast<ICondition>().ToArray(),
Effects = x.effects.Select(y => new Effect
var effect = x.effects[i];
effects[i] = new Effect
{
WorldKey = effect.worldKey.GetScript(worldKeys).GetInstance<IWorldKey>(),
Increase = effect.effect == EffectType.Increase,
};
}

configs.Add(new ActionConfig
{
WorldKey = y.worldKey.GetScript(generator.GetWorldKeys()).GetInstance<IWorldKey>(),
Increase = y.effect == EffectType.Increase,
}).Cast<IEffect>().ToArray(),
MoveMode = x.moveMode,
Properties = x.properties,
}).Cast<IActionConfig>().ToList();
Name = x.action.Name,
ClassType = x.action.GetScript(actionClasses).GetFullName(),
BaseCost = x.baseCost,
Target = x.target.GetScript(targetClasses).GetInstance<ITargetKey>(),
StoppingDistance = x.stoppingDistance,
ValidateTarget = x.validateTarget,
RequiresTarget = x.requiresTarget,
ValidateConditions = x.validateConditions,
Conditions = conditions,
Effects = effects,
MoveMode = x.moveMode,
Properties = x.properties,
});
}

return configs;
}

public List<IGoalConfig> GetGoals(GeneratorScriptable generator)
Expand All @@ -72,18 +95,33 @@ public List<IGoalConfig> GetGoals(GeneratorScriptable generator)
return new List<IGoalConfig>();

var goalClasses = generator.GetGoals();

return this.goals.Select(x => new GoalConfig
var worldKeys = generator.GetWorldKeys();

var configs = new List<IGoalConfig>();

foreach (var goal in this.goals)
{
Name = x.goal.Name,
ClassType = x.goal.GetScript(goalClasses).GetFullName(),
Conditions = x.conditions.Select(y => new Condition
var conditions = new List<ICondition>();

foreach (var condition in goal.conditions)
{
conditions.Add(new Condition
{
WorldKey = condition.worldKey.GetScript(worldKeys).GetInstance<IWorldKey>(),
Comparison = condition.comparison,
Amount = condition.amount,
});
}

configs.Add(new GoalConfig
{
WorldKey = y.worldKey.GetScript(generator.GetWorldKeys()).GetInstance<IWorldKey>(),
Comparison = y.comparison,
Amount = y.amount,
}).Cast<ICondition>().ToList(),
}).Cast<IGoalConfig>().ToList();
Name = goal.goal.Name,
ClassType = goal.goal.GetScript(goalClasses).GetFullName(),
Conditions = conditions,
});
}

return configs;
}

public List<IWorldSensorConfig> GetWorldSensors(GeneratorScriptable generator)
Expand All @@ -92,13 +130,21 @@ public List<IWorldSensorConfig> GetWorldSensors(GeneratorScriptable generator)
return new List<IWorldSensorConfig>();

var sensorClasses = generator.GetWorldSensors();

return this.worldSensors.Select(x => new WorldSensorConfig
var worldKeys = generator.GetWorldKeys();

var configs = new List<IWorldSensorConfig>(this.worldSensors.Count);

foreach (var worldSensor in this.worldSensors)
{
Name = x.sensor.Name,
ClassType = x.sensor.GetScript(sensorClasses).GetFullName(),
Key = x.worldKey.GetScript(generator.GetWorldKeys()).GetInstance<IWorldKey>(),
}).Cast<IWorldSensorConfig>().ToList();
configs.Add(new WorldSensorConfig
{
Name = worldSensor.sensor.Name,
ClassType = worldSensor.sensor.GetScript(sensorClasses).GetFullName(),
Key = worldSensor.worldKey.GetScript(worldKeys).GetInstance<IWorldKey>(),
});
}

return configs;
}

public List<ITargetSensorConfig> GetTargetSensors(GeneratorScriptable generator)
Expand All @@ -107,13 +153,21 @@ public List<ITargetSensorConfig> GetTargetSensors(GeneratorScriptable generator)
return new List<ITargetSensorConfig>();

var sensorClasses = generator.GetTargetSensors();
var targetKeys = generator.GetTargetKeys();

var configs = new List<ITargetSensorConfig>(this.targetSensors.Count);

return this.targetSensors.Select(x => new TargetSensorConfig
foreach (var targetSensor in this.targetSensors)
{
Name = x.sensor.Name,
ClassType = x.sensor.GetScript(sensorClasses).GetFullName(),
Key = x.targetKey.GetScript(generator.GetTargetKeys()).GetInstance<ITargetKey>(),
}).Cast<ITargetSensorConfig>().ToList();
configs.Add(new TargetSensorConfig
{
Name = targetSensor.sensor.Name,
ClassType = targetSensor.sensor.GetScript(sensorClasses).GetFullName(),
Key = targetSensor.targetKey.GetScript(targetKeys).GetInstance<ITargetKey>(),
});
}

return configs;
}

public List<IMultiSensorConfig> GetMultiSensors(GeneratorScriptable generator)
Expand All @@ -122,12 +176,19 @@ public List<IMultiSensorConfig> GetMultiSensors(GeneratorScriptable generator)
return new List<IMultiSensorConfig>();

var sensorClasses = generator.GetMultiSensors();

var configs = new List<IMultiSensorConfig>(this.multiSensors.Count);

return this.multiSensors.Select(x => new MultiSensorConfig
foreach (var multiSensor in this.multiSensors)
{
Name = x.sensor.Name,
ClassType = x.sensor.GetScript(sensorClasses).GetFullName(),
}).Cast<IMultiSensorConfig>().ToList();
configs.Add(new MultiSensorConfig
{
Name = multiSensor.sensor.Name,
ClassType = multiSensor.sensor.GetScript(sensorClasses).GetFullName(),
});
}

return configs;
}

public GeneratorScriptable GetGenerator()
Expand Down