Skip to content

Commit 465b343

Browse files
firefighters: bot fixes
1 parent 2a6d66d commit 465b343

13 files changed

+56
-78
lines changed

EntitiesSamples/Assets/Tutorials/Firefighters/Step 1/BotAuthoring.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Unity.Entities;
22
using Unity.Mathematics;
33
using UnityEngine;
4-
using UnityEngine.Serialization;
54

65
namespace Tutorials.Firefighters
76
{

EntitiesSamples/Assets/Tutorials/Firefighters/Step 1/Components.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ namespace Tutorials.Firefighters
66
public struct Team : IComponentData
77
{
88
public Entity Filler;
9-
public Entity Douser;
10-
public bool HasBucket;
9+
public Entity Bucket;
1110
public int NumFiresDoused;
1211
}
1312

EntitiesSamples/Assets/Tutorials/Firefighters/Step 1/ConfigAuthoring.cs

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,41 @@
11
using Unity.Entities;
22
using Unity.Mathematics;
33
using UnityEngine;
4-
using UnityEngine.Serialization;
54

65
namespace Tutorials.Firefighters
76
{
87
public class ConfigAuthoring : MonoBehaviour
98
{
10-
[Header("Ponds")]
9+
[Header("Ponds")]
1110
public int NumPondsPerEdge;
1211

13-
[Header("Bots")]
12+
[Header("Bots")]
1413
public int NumTeams;
1514
public int NumPassersPerTeam;
1615
public int BotMoveSpeed = 3; // units per second
1716
public float LineMaxOffset = 4;
1817

19-
[Header("Buckets")]
18+
[Header("Buckets")]
2019
public float BucketFillRate;
2120
public int NumBuckets;
2221
public Color BucketEmptyColor;
2322
public Color BucketFullColor;
2423
public float BucketEmptyScale;
2524
public float BucketFullScale;
2625

27-
[Header("Ground")]
26+
[Header("Ground")]
2827
public int GroundNumColumns;
2928
public int GroundNumRows;
3029

31-
[Header("Heat")]
30+
[Header("Heat")]
3231
public Color MinHeatColor;
3332
public Color MaxHeatColor;
3433
public float HeatSpreadSpeed;
3534
public float HeatOscillationScale;
3635
public int NumInitialCellsOnFire;
3736
public float HeatDouseTargetMin;
3837

39-
[Header("Prefabs")]
38+
[Header("Prefabs")]
4039
public GameObject BotPrefab;
4140
public GameObject BucketPrefab;
4241
public GameObject PondPrefab;
@@ -48,18 +47,20 @@ class Baker : Baker<ConfigAuthoring>
4847
public override void Bake(ConfigAuthoring authoring)
4948
{
5049
var entity = GetEntity(authoring, TransformUsageFlags.None);
50+
var numTeams = math.max(authoring.NumTeams, 1);
5151
AddComponent(entity, new Config
5252
{
5353
GroundNumColumns = authoring.GroundNumColumns,
5454
GroundNumRows = authoring.GroundNumRows,
5555
NumPondsPerEdge = authoring.NumPondsPerEdge,
56-
NumTeams = math.max(authoring.NumTeams, 1),
56+
NumTeams = numTeams,
5757
NumPassersPerTeam =
5858
(math.max(authoring.NumPassersPerTeam, 4) / 2) *
5959
2, // round down to even number and set min to 4
6060
BotMoveSpeed = authoring.BotMoveSpeed,
6161
LineMaxOffset = authoring.LineMaxOffset,
62-
NumBuckets = authoring.NumBuckets,
62+
NumBuckets =
63+
math.max(authoring.NumBuckets, numTeams), // make sure there's at least one bucket per team
6364
BucketFillRate = authoring.BucketFillRate,
6465
MinHeatColor = (Vector4)authoring.MinHeatColor,
6566
MaxHeatColor = (Vector4)authoring.MaxHeatColor,
@@ -120,4 +121,4 @@ public class ConfigManaged : IComponentData
120121
public GameObject BotAnimatedPrefabGO;
121122
public UIController UIController;
122123
}
123-
}
124+
}

EntitiesSamples/Assets/Tutorials/Firefighters/Step 1/SpawnSystem.cs

+26-24
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,38 @@ public void OnUpdate(ref SystemState state)
2424
var config = SystemAPI.GetSingleton<Config>();
2525
var rand = new Random(123);
2626

27+
var bucketEntities = new NativeArray<Entity>(config.NumBuckets, Allocator.Temp);
28+
29+
// spawn buckets
30+
{
31+
// struct components are returned and passed by value (as copies)!
32+
var bucketTransform = state.EntityManager.GetComponentData<LocalTransform>(config.BucketPrefab);
33+
bucketTransform.Position.y = (bucketTransform.Scale / 2); // will be same for every bucket
34+
35+
for (int i = 0; i < config.NumBuckets; i++)
36+
{
37+
var bucketEntity = state.EntityManager.Instantiate(config.BucketPrefab);
38+
bucketEntities[i] = bucketEntity;
39+
40+
bucketTransform.Position.x = rand.NextFloat(0.5f, config.GroundNumColumns - 0.5f);
41+
bucketTransform.Position.z = rand.NextFloat(0.5f, config.GroundNumRows - 0.5f);
42+
bucketTransform.Scale = config.BucketEmptyScale;
43+
44+
state.EntityManager.SetComponentData(bucketEntity, bucketTransform);
45+
}
46+
}
47+
2748
// spawn teams
2849
{
2950
int numBotsPerTeam = config.NumPassersPerTeam + 1;
3051
int douserIdx = (config.NumPassersPerTeam / 2);
3152
for (int teamIdx = 0; teamIdx < config.NumTeams; teamIdx++)
3253
{
3354
var teamEntity = state.EntityManager.CreateEntity();
34-
var team = new Team();
55+
var team = new Team
56+
{
57+
Bucket = bucketEntities[teamIdx]
58+
};
3559
state.EntityManager.AddComponent<RepositionLine>(teamEntity);
3660
var memberBuffer = state.EntityManager.AddBuffer<TeamMember>(teamEntity);
3761
memberBuffer.Capacity = numBotsPerTeam;
@@ -51,15 +75,11 @@ public void OnUpdate(ref SystemState state)
5175
Value = teamColor
5276
});
5377

54-
// designate the filler and the douser
78+
// designate the filler
5579
if (botIdx == 0)
5680
{
5781
team.Filler = botEntity;
5882
}
59-
else if (botIdx == douserIdx)
60-
{
61-
team.Douser = botEntity;
62-
}
6383

6484
memberBuffer.Add(new TeamMember { Bot = botEntity });
6585
}
@@ -90,24 +110,6 @@ public void OnUpdate(ref SystemState state)
90110
}
91111
}
92112

93-
// spawn buckets
94-
{
95-
// struct components are returned and passed by value (as copies)!
96-
var bucketTransform = state.EntityManager.GetComponentData<LocalTransform>(config.BucketPrefab);
97-
bucketTransform.Position.y = (bucketTransform.Scale / 2); // will be same for every bucket
98-
99-
for (int i = 0; i < config.NumBuckets; i++)
100-
{
101-
var bucketEntity = state.EntityManager.Instantiate(config.BucketPrefab);
102-
103-
bucketTransform.Position.x = rand.NextFloat(0.5f, config.GroundNumColumns - 0.5f);
104-
bucketTransform.Position.z = rand.NextFloat(0.5f, config.GroundNumRows - 0.5f);
105-
bucketTransform.Scale = config.BucketEmptyScale;
106-
107-
state.EntityManager.SetComponentData(bucketEntity, bucketTransform);
108-
}
109-
}
110-
111113
// spawn ponds
112114
{
113115
var bounds = new NativeArray<float4>(4, Allocator.Temp);

EntitiesSamples/Assets/Tutorials/Firefighters/Step 3/BotSystem.cs

+6-28
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using Unity.Entities;
33
using Unity.Mathematics;
44
using Unity.Transforms;
5-
using Unity.VisualScripting;
6-
using UnityEngine;
75

86
namespace Tutorials.Firefighters
97
{
@@ -42,38 +40,18 @@ private void BotUpdate_MainThread(ref SystemState state, DynamicBuffer<Heat> hea
4240
{
4341
switch (bot.ValueRO.State)
4442
{
45-
case BotState.CLAIM_BUCKET:
46-
{
47-
// (only put the filler into this state if the team doesn't yet have a bucket)
48-
foreach (var (bucket, bucketTrans, bucketEntity) in
49-
SystemAPI.Query<RefRW<Bucket>, RefRO<LocalTransform>>()
50-
.WithEntityAccess())
51-
{
52-
// claim first unclaimed bucket
53-
if (bucket.ValueRO.CarryingBot == Entity.Null)
54-
{
55-
bucket.ValueRW.CarryingBot = botEntity;
56-
bot.ValueRW.Bucket = bucketEntity;
57-
bot.ValueRW.TargetPos = bucketTrans.ValueRO.Position.xz;
58-
bot.ValueRW.State = BotState.MOVE_TO_BUCKET;
59-
60-
var team = SystemAPI.GetComponentRW<Team>(bot.ValueRO.Team);
61-
team.ValueRW.HasBucket = true;
62-
63-
break;
64-
}
65-
}
66-
67-
break;
68-
}
6943
case BotState.MOVE_TO_BUCKET:
7044
{
7145
if (MoveToTarget(ref botTrans.ValueRW, bot.ValueRO.TargetPos, moveSpeed))
7246
{
73-
var bucket = SystemAPI.GetComponentRW<Bucket>(bot.ValueRO.Bucket);
47+
var team = SystemAPI.GetComponent<Team>(bot.ValueRO.Team);
48+
49+
var bucket = SystemAPI.GetComponentRW<Bucket>(team.Bucket);
50+
bucket.ValueRW.CarryingBot = botEntity;
7451
bucket.ValueRW.IsCarried = true;
52+
53+
bot.ValueRW.Bucket = team.Bucket;
7554
bot.ValueRW.IsCarrying = true;
76-
7755
bot.ValueRW.TargetPos = bot.ValueRO.LinePos; // was set in TeamSystem
7856
bot.ValueRW.State = BotState.MOVE_TO_LINE;
7957
}

EntitiesSamples/Assets/Tutorials/Firefighters/Step 3/BucketSystem.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Unity.Burst;
2-
using Unity.Collections;
32
using Unity.Entities;
43
using Unity.Mathematics;
54
using Unity.Rendering;

EntitiesSamples/Assets/Tutorials/Firefighters/Step 3/LineSystem.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,16 @@ public void OnUpdate(ref SystemState state)
7575

7676
var filler = SystemAPI.GetComponentRW<Bot>(team.ValueRO.Filler);
7777
filler.ValueRW.LinePos = randomPondPos;
78-
if (team.ValueRO.HasBucket)
78+
79+
var bucket = SystemAPI.GetComponentRW<Bucket>(team.ValueRO.Bucket);
80+
if (bucket.ValueRO.IsCarried)
7981
{
8082
filler.ValueRW.State = BotState.MOVE_TO_LINE;
8183
}
8284
else
8385
{
84-
filler.ValueRW.State = BotState.CLAIM_BUCKET;
86+
filler.ValueRW.TargetPos = SystemAPI.GetComponent<LocalTransform>(team.ValueRO.Bucket).Position.xz;
87+
filler.ValueRW.State = BotState.MOVE_TO_BUCKET;
8588
}
8689
}
8790
}

EntitiesSamples/Assets/Tutorials/Firefighters/Step 4/AnimationSystem.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using Unity.Burst;
22
using Unity.Collections;
33
using Unity.Entities;
4-
using Unity.Entities.Graphics;
5-
using Unity.Mathematics;
64
using Unity.Rendering;
75
using Unity.Transforms;
86
using UnityEngine;
@@ -16,12 +14,13 @@ public partial struct AnimationSystem : ISystem
1614
[BurstCompile]
1715
public void OnCreate(ref SystemState state)
1816
{
17+
state.RequireForUpdate<Config>();
1918
state.RequireForUpdate<Bot>();
2019
state.RequireForUpdate<ExecuteAnimation>();
2120
}
2221

2322
// Because this update accesses managed objects, it cannot be Burst compiled,
24-
// so we do not add the [BurstCompiled] attribute.
23+
// so we do not add the [BurstCompile] attribute.
2524
public void OnUpdate(ref SystemState state)
2625
{
2726
if (!isInitialized)
@@ -43,7 +42,7 @@ public void OnUpdate(ref SystemState state)
4342
botAnimation.AnimatedGO = go;
4443
go.transform.localPosition = (Vector3)transform.ValueRO.Position;
4544
ecb.AddComponent(entity, botAnimation);
46-
45+
4746
// disable rendering
4847
ecb.RemoveComponent<MaterialMeshInfo>(entity);
4948
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False">
2-
<ui:Label tabindex="-1" text="Number of fires doused: 0" parse-escape-sequences="true" display-tooltip-when-elided="true" name="DouseCounter" style="font-size: 28px; color: rgb(0, 0, 0);" />
3-
<ui:Button text="Reposition the bucket brigade lines" parse-escape-sequences="true" display-tooltip-when-elided="true" name="RepositionButton" style="font-size: 28px; -unity-text-align: middle-center; width: 40%; align-self: auto; align-items: flex-end;" />
2+
<ui:Label tabindex="-1" text="Number of fires doused: 0" parse-escape-sequences="true" display-tooltip-when-elided="true" name="DouseCounter" style="font-size: 24px; color: rgb(0, 0, 0);" />
3+
<ui:Button text="Reposition the bucket brigade lines" parse-escape-sequences="true" display-tooltip-when-elided="true" name="RepositionButton" style="font-size: 24px; -unity-text-align: middle-left; width: auto; align-self: auto; align-items: flex-end;" />
44
</ui:UXML>

EntitiesSamples/Assets/Tutorials/Firefighters/Step 4/UIController.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using UnityEditor.Compilation;
21
using UnityEngine;
32
using UnityEngine.UIElements;
43

EntitiesSamples/Assets/Tutorials/Firefighters/Step 4/UISystem.cs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Tutorials.Firefighters
66
{
7-
[UpdateBefore(typeof(LineSystem))]
87
public partial struct UISystem : ISystem
98
{
109
private bool initialized;

EntitiesSamples/Packages/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"com.unity.collab-proxy": "2.2.0",
44
"com.unity.entities.graphics": "1.0.10",
55
"com.unity.feature.development": "1.0.1",
6-
"com.unity.ide.rider": "3.0.27",
6+
"com.unity.ide.rider": "3.0.28",
77
"com.unity.ide.visualstudio": "2.0.22",
88
"com.unity.ide.vscode": "1.2.5",
99
"com.unity.render-pipelines.universal": "14.0.10",

EntitiesSamples/Packages/packages-lock.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
}
8787
},
8888
"com.unity.ide.rider": {
89-
"version": "3.0.27",
89+
"version": "3.0.28",
9090
"depth": 0,
9191
"source": "registry",
9292
"dependencies": {

0 commit comments

Comments
 (0)