Skip to content
Open
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
18 changes: 11 additions & 7 deletions Assets/Battle/AI/Aggression/AgentCategoryProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@

namespace Battle.AI
{
public class AgentCategoryProxy : MonoBehaviour, IConvertGameObjectToEntity
public class AgentCategoryProxy : MonoBehaviour
{
[Tooltip("Type of this entity.")]
public AgentCategory.eType AgentType;

public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
var category = new AgentCategory { Type = AgentType };
dstManager.AddComponentData(entity, category);
}

#if UNITY_EDITOR
void OnGUI()
{
AgentType = (AgentCategory.eType)EditorGUILayout.EnumFlagsField("Type of this entity", AgentType);
}
#endif
}

public class AgentCategoryBaker : Baker<AgentCategoryProxy>
{
public override void Bake(AgentCategoryProxy authoring)
{
var entity = GetEntity(TransformUsageFlags.None);
var category = new AgentCategory { Type = authoring.AgentType };
AddComponent(entity, category);
}
}
}
16 changes: 10 additions & 6 deletions Assets/Battle/AI/Aggression/AggroProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@

namespace Battle.AI
{
public class AggroProxy : MonoBehaviour, IConvertGameObjectToEntity
public class AggroProxy : MonoBehaviour
{
public float Radius = 10f;

[Tooltip("Time in seconds between retargetting. 0 to disable.")]
public float RetargetTime = 0.0f;
}

public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
public class AggroBaker : Baker<AggroProxy>
{
public override void Bake(AggroProxy authoring)
{
dstManager.AddComponentData(entity, new AggroRadius { Value = Radius });
dstManager.AddComponentData(entity, new AggroLocation());
if (RetargetTime > 0.0f)
dstManager.AddComponentData(entity, new RetargetBehaviour { Interval = RetargetTime, RemainingTime = RetargetTime });
var entity = GetEntity(TransformUsageFlags.None);
AddComponent(entity, new AggroRadius { Value = authoring.Radius });
AddComponent(entity, new AggroLocation());
if (authoring.RetargetTime > 0.0f)
AddComponent(entity, new RetargetBehaviour { Interval = authoring.RetargetTime, RemainingTime = authoring.RetargetTime });
}
}

Expand Down
1 change: 0 additions & 1 deletion Assets/Battle/AI/Aggression/SelectTargetsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Battle.AI
{
[AlwaysUpdateSystem]
[UpdateInGroup(typeof(AISystemGroup))]
public class SelectTargetsSystem : SystemBase
{
Expand Down
18 changes: 11 additions & 7 deletions Assets/Battle/AI/Aggression/TargetingOrdersProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Battle.AI
{
public class TargetingOrdersProxy : MonoBehaviour, IConvertGameObjectToEntity
public class TargetingOrdersProxy : MonoBehaviour
{
[Tooltip("Types of entity we are encouraged to target.")]
public AgentCategory.eType PreferredTargets;
Expand All @@ -15,12 +15,6 @@ public class TargetingOrdersProxy : MonoBehaviour, IConvertGameObjectToEntity

public bool TargetSameTeam = false;

public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
var targetOrders = new TargetingOrders { Discouraged = DiscouragedTargets, Preferred = PreferredTargets, TargetSameTeam = TargetSameTeam };
dstManager.AddComponentData(entity, targetOrders);
}

#if UNITY_EDITOR
void OnGUI()
{
Expand All @@ -29,4 +23,14 @@ void OnGUI()
}
#endif
}

public class TargetingOrdersBaker : Baker<TargetingOrdersProxy>
{
public override void Bake(TargetingOrdersProxy authoring)
{
var entity = GetEntity(TransformUsageFlags.None);
var targetOrders = new TargetingOrders { Discouraged = authoring.DiscouragedTargets, Preferred = authoring.PreferredTargets, TargetSameTeam = authoring.TargetSameTeam };
AddComponent(entity, targetOrders);
}
}
}
8 changes: 3 additions & 5 deletions Assets/Battle/AI/Aggression/UpdateAggressionSourceSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@ protected override void OnUpdate ()
.WithBurst()
.ScheduleParallel();

var ltwData = GetComponentDataFromEntity<LocalToWorld>( isReadOnly:true );
Entities
.WithReadOnly(ltwData)
.ForEach( ( Entity e , ref AggroLocation source , in GuardBehaviour guard , in Target target ) =>
{
if (target.Value == Entity.Null)
return;

if (!ltwData.HasComponent(guard.Target))
if (!SystemAPI.HasComponent<LocalToWorld>(guard.Target))
{
source.Position = ltwData[e].Position;
source.Position = SystemAPI.GetComponent<LocalToWorld>(e).Position;
return;
}

source.Position = ltwData[guard.Target].Position;
source.Position = SystemAPI.GetComponent<LocalToWorld>(guard.Target).Position;
} )
.WithBurst()
.ScheduleParallel();
Expand Down
7 changes: 2 additions & 5 deletions Assets/Battle/AI/Idle/EscortSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ public class EscortSystem : SystemBase
{
protected override void OnUpdate()
{
var ltwData = GetComponentDataFromEntity<LocalToWorld>( isReadOnly:true );

Entities
.WithReadOnly(ltwData)
.ForEach( ( ref RandomWalkBehaviour walk , in Escort escort ) =>
{
if( ltwData.HasComponent(escort.Target) )
walk.Centre = ltwData[escort.Target].Position;
if( SystemAPI.HasComponent<LocalToWorld>(escort.Target) )
walk.Centre = SystemAPI.GetComponent<LocalToWorld>(escort.Target).Position;
} )
.WithBurst()
.ScheduleParallel();
Expand Down
11 changes: 8 additions & 3 deletions Assets/Battle/AI/Idle/IdleBehaviourProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@

namespace Battle.AI
{
public class IdleBehaviourProxy : MonoBehaviour, IConvertGameObjectToEntity
public class IdleBehaviourProxy : MonoBehaviour
{
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
}

public class IdleBehaviourBaker : Baker<IdleBehaviourProxy>
{
public override void Bake(IdleBehaviourProxy authoring)
{
dstManager.AddComponentData(entity, new IdleBehaviour { });
var entity = GetEntity(TransformUsageFlags.None);
AddComponent(entity, new IdleBehaviour { });
}
}
}
13 changes: 3 additions & 10 deletions Assets/Battle/AI/Idle/IdleToCombatSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,10 @@ namespace Battle.AI
[UpdateInGroup(typeof(AISystemGroup))]
public class IdleToCombatSystem : SystemBase
{

AIStateChangeBufferSystem _commandBufferSystem;

protected override void OnCreate()
{
_commandBufferSystem = World.GetOrCreateSystem<AIStateChangeBufferSystem>();
}

protected override void OnUpdate ()
{
var commands = _commandBufferSystem.CreateCommandBuffer().AsParallelWriter();
var commandBufferSystem = World.GetOrCreateSystemManaged<AIStateChangeBufferSystem>();
var commands = commandBufferSystem.CreateCommandBuffer().AsParallelWriter();

Entities
.WithAll<IdleBehaviour>()
Expand All @@ -38,7 +31,7 @@ protected override void OnUpdate ()
.WithBurst()
.ScheduleParallel();

_commandBufferSystem.AddJobHandleForProducer( Dependency );
commandBufferSystem.AddJobHandleForProducer( Dependency );
}

}
Expand Down
10 changes: 7 additions & 3 deletions Assets/Battle/AI/Idle/TurretBehaviourProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@

namespace Battle.AI
{
public class TurretBehaviourProxy : MonoBehaviour, IConvertGameObjectToEntity
public class TurretBehaviourProxy : MonoBehaviour
{
public float Range;
}

public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
public class TurretBehaviourBaker : Baker<TurretBehaviourProxy>
{
public override void Bake(TurretBehaviourProxy authoring)
{
dstManager.AddComponentData(entity, new TurretBehaviour { Range = Range });
var entity = GetEntity(TransformUsageFlags.None);
AddComponent(entity, new TurretBehaviour { Range = authoring.Range });
}
}
}
7 changes: 2 additions & 5 deletions Assets/Battle/AI/Idle/TurretBehaviourSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ public class TurretBehaviourSystem : SystemBase
{
protected override void OnUpdate()
{
var ltwData = GetComponentDataFromEntity<LocalToWorld>( isReadOnly:true );

Entities
.ForEach( ( ref TurretBehaviour behaviour, ref TargettedTool tool ) => behaviour.Range = tool.Range )
.WithBurst()
.ScheduleParallel();

Entities
.WithReadOnly(ltwData)
.ForEach( (
Entity entity ,
int entityInQueryIndex ,
Expand All @@ -38,13 +35,13 @@ in LocalToWorld localToWorld
{
if( target.Value==Entity.Null )
return;
if( !ltwData.HasComponent(target.Value) )
if( !SystemAPI.HasComponent<LocalToWorld>(target.Value) )
{
target.Value = Entity.Null;
return;
}

var targetPos = ltwData[target.Value].Position;
var targetPos = SystemAPI.GetComponent<LocalToWorld>(target.Value).Position;

// Disengage if target is outside range.
if (math.lengthsq(targetPos - localToWorld.Position) > behaviour.Range * behaviour.Range)
Expand Down
18 changes: 5 additions & 13 deletions Assets/Battle/AI/Movement/PerformPeelManoeuvreSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,10 @@ public class PerformPeelManoeuvreSystem : SystemBase
{
public const float ENGAGEMENT_RADIUS = 10f;

private AIStateChangeBufferSystem m_AIStateBuffer;

protected override void OnCreate()
{
m_AIStateBuffer = World.GetOrCreateSystem<AIStateChangeBufferSystem>();
}

protected override void OnUpdate()
{
var positions = GetComponentDataFromEntity<Translation>(true);
var buffer = m_AIStateBuffer.CreateCommandBuffer().AsParallelWriter();
var aiStateBuffer = World.GetOrCreateSystemManaged<AIStateChangeBufferSystem>();
var buffer = aiStateBuffer.CreateCommandBuffer().AsParallelWriter();

Entities
.WithAll<PeelManoeuvre>()
Expand All @@ -41,7 +34,7 @@ protected override void OnUpdate()
in MaxTurnSpeed maxTurnSpeed
) =>
{
if (target.Value == Entity.Null || !positions.HasComponent(target.Value))
if (target.Value == Entity.Null || !SystemAPI.HasComponent<Translation>(target.Value))
{
buffer.RemoveComponent<PeelManoeuvre>(entityInQueryIndex, e);
buffer.AddComponent(entityInQueryIndex, e, new IdleBehaviour());
Expand All @@ -50,7 +43,7 @@ in MaxTurnSpeed maxTurnSpeed
}

//Target position
var targetPos = positions[target.Value];
var targetPos = SystemAPI.GetComponent<Translation>(target.Value);

// Turn away from the enemy.
float angleDiff = MathUtil.GetAngleDifference(MathUtil.GetHeadingToPoint(targetPos.Value - pos.Value), heading.Value);
Expand All @@ -68,9 +61,8 @@ in MaxTurnSpeed maxTurnSpeed
}
}
)
.WithReadOnly(positions)
.ScheduleParallel();
m_AIStateBuffer.AddJobHandleForProducer(Dependency);
aiStateBuffer.AddJobHandleForProducer(Dependency);
}
}
}
18 changes: 5 additions & 13 deletions Assets/Battle/AI/Movement/PursueBehaviourSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,10 @@ public class PursueBehaviourSystem : SystemBase
{
public const float PROXIMITY_RADIUS = 4f;

private AIStateChangeBufferSystem m_AIStateBuffer;

protected override void OnCreate()
{
m_AIStateBuffer = World.GetOrCreateSystem<AIStateChangeBufferSystem>();
}

protected override void OnUpdate()
{
var positions = GetComponentDataFromEntity<Translation>(true);
var buffer = m_AIStateBuffer.CreateCommandBuffer().AsParallelWriter();
var aiStateBuffer = World.GetOrCreateSystemManaged<AIStateChangeBufferSystem>();
var buffer = aiStateBuffer.CreateCommandBuffer().AsParallelWriter();

Entities
.ForEach(
Expand All @@ -38,7 +31,7 @@ protected override void OnUpdate()
in Translation pos
) =>
{
if (target.Value == Entity.Null || !positions.HasComponent(target.Value))
if (target.Value == Entity.Null || !SystemAPI.HasComponent<Translation>(target.Value))
{
// Go to idle state
buffer.RemoveComponent<PursueBehaviour>(entityInQueryIndex, e);
Expand All @@ -47,7 +40,7 @@ in Translation pos
}

// Set entity destination to target position
destination.Destination = positions[target.Value].Value;
destination.Destination = SystemAPI.GetComponent<Translation>(target.Value).Value;

// if too close to target, evasive manoeuvre
if (math.lengthsq(destination.Destination - pos.Value) < PROXIMITY_RADIUS * PROXIMITY_RADIUS)
Expand All @@ -58,10 +51,9 @@ in Translation pos
}
}
)
.WithReadOnly(positions)
.ScheduleParallel();

m_AIStateBuffer.AddJobHandleForProducer(Dependency);
aiStateBuffer.AddJobHandleForProducer(Dependency);
}
}
}
10 changes: 7 additions & 3 deletions Assets/Battle/AI/Movement/TurnToDestinationProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

namespace Battle.AI
{
public class TurnToDestinationProxy : MonoBehaviour, IConvertGameObjectToEntity
public class TurnToDestinationProxy : MonoBehaviour
{
public float3 Destination;
}

public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
public class TurnToDestinationBaker : Baker<TurnToDestinationProxy>
{
public override void Bake(TurnToDestinationProxy authoring)
{
dstManager.AddComponentData(entity, new TurnToDestinationBehaviour { Destination = Destination });
var entity = GetEntity(TransformUsageFlags.None);
AddComponent(entity, new TurnToDestinationBehaviour { Destination = authoring.Destination });
}
}
}
Loading