-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormationAssignmentSystemAuthoring.cs
More file actions
96 lines (85 loc) · 3.43 KB
/
FormationAssignmentSystemAuthoring.cs
File metadata and controls
96 lines (85 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using Tayx.Graphy.Utils.NumString;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
using Utilities;
//public enum FormationFormType
//{
// SquareFormation = 0,
// CircleFormation // not implemented
//}
//This system reforms the formations on a certain rule set.
[CreateAssetMenu(fileName = "FormationAssignmentSystem", menuName = "ScriptableObjects/Systems/FormationAssignmentSystem", order = 1)]
public class FormationAssignmentSystemAuthoring : SystemAuthoringSO
{
[HideInInspector] [SerializeField] private SerializableGuid id;
public override SystemBaseSO GetSystem()
=> World.DefaultGameObjectInjectionWorld.GetExistingSystem<FormationAssignmentSystem>();
protected override void OnInit()
{
FormationAssignmentSystem assignmentSystem = World.DefaultGameObjectInjectionWorld.GetExistingSystem<FormationAssignmentSystem>();
}
private void OnValidate()
{
if (id.Value == string.Empty)
{
id = Guid.NewGuid();
}
}
}
public class FormationAssignmentSystem : SystemBaseSO
{
// ECBs for adding and removing of components, and destruction of Entities
private BeginSimulationEntityCommandBufferSystem beginECB;
private EndSimulationEntityCommandBufferSystem endECB;
protected override void OnCreate()
{
beginECB = World.GetOrCreateSystem<BeginSimulationEntityCommandBufferSystem>();
endECB = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var endSimEcb = endECB.CreateCommandBuffer().AsParallelWriter();
Entities
.WithAll<FormationExitEngageStateComponent>()
.WithNone<FormationGarrisoned>()
.ForEach(
(Entity entity, int entityInQueryIndex) =>
{
endSimEcb.AddComponent(entityInQueryIndex, entity, new FormationReformComponent());
})
.ScheduleParallel();
Entities
.WithNone<FormationDeathComponent, FormationEngageStateComponent, FormationGarrisoned>()
.WithAll<FormationReformComponent>()
.ForEach(
(Entity entity, int entityInQueryIndex,
ref DynamicBuffer<FormationUnitElement> formationBuffer,
in FormationComponent formation,
in Translation trans) =>
{
int loopIndexX = -formation.count.x / 2;
int loopIndexY = -formation.count.y / 2;
for (int i = 0; i < formationBuffer.Length; i++)
{
float newX = loopIndexX * formation.unitOffset.x;
float newY = loopIndexY * formation.unitOffset.y;
endSimEcb.SetComponent(entityInQueryIndex, formationBuffer[i].unit, new UnitFormationPosComponent()
{
formationPos = new float2(newX, newY)
});
loopIndexX++;
if (loopIndexX >= formation.count.x / 2)
{
loopIndexY++;
loopIndexX = -formation.count.x / 2;
}
}
endSimEcb.RemoveComponent<FormationReformComponent>(entityInQueryIndex, entity);
})
.ScheduleParallel();
endECB.AddJobHandleForProducer(Dependency);
}
}