forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineSystem.cs
More file actions
93 lines (78 loc) · 3.87 KB
/
Copy pathLineSystem.cs
File metadata and controls
93 lines (78 loc) · 3.87 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
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
using Random = Unity.Mathematics.Random;
namespace Tutorials.Firefighters
{
public partial struct LineSystem : ISystem
{
private uint seed;
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<Config>();
state.RequireForUpdate<RepositionLine>();
state.RequireForUpdate<Heat>();
state.RequireForUpdate<Team>();
state.RequireForUpdate<ExecuteTeam>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var config = SystemAPI.GetSingleton<Config>();
var rand = new Random(123 +
seed++); // seed is incremented to get different random values in different frames
var pondQuery = SystemAPI.QueryBuilder().WithAll<Pond, LocalTransform>().Build();
var pondPositions = pondQuery.ToComponentDataArray<LocalTransform>(Allocator.Temp);
var heatBuffer = SystemAPI.GetSingletonBuffer<Heat>();
// EnabledRefRW gives us access to the enabled state of RepositionLine.
// The query will only match entities whose RepositionLine is enabled.
foreach (var (team, members, respositionLineState) in
SystemAPI.Query<RefRO<Team>, DynamicBuffer<TeamMember>, EnabledRefRW<RepositionLine>>())
{
respositionLineState.ValueRW = false; // disable RepositionLine
// set LinePos of the team's bots and set their bot state
{
var randomPondPos = pondPositions[rand.NextInt(pondPositions.Length)].Position.xz;
var nearestFirePos = HeatSystem.NearestFire(randomPondPos, heatBuffer,
config.GroundNumRows, config.GroundNumColumns, config.HeatDouseTargetMin);
var douserIdx = members.Length / 2;
var vec = nearestFirePos - randomPondPos;
var vecNorm = math.normalize(vec);
var offsetVec = new float2(-vecNorm.y, vecNorm.x);
for (int i = 1; i <= douserIdx; i++)
{
var ratio = (float)i / (douserIdx + 1);
var offset = math.sin(math.lerp(0, math.PI, ratio)) * offsetVec * config.LineMaxOffset;
var pos = math.lerp(randomPondPos, nearestFirePos, ratio);
var bot = SystemAPI.GetComponentRW<Bot>(members[i].Bot);
bot.ValueRW.State = BotState.MOVE_TO_LINE;
bot.ValueRW.LinePos = pos + offset;
if (bot.ValueRO.IsDouser)
{
bot.ValueRW.TargetPos = nearestFirePos;
}
var otherBot = SystemAPI.GetComponentRW<Bot>(members[^i].Bot);
otherBot.ValueRW.State = BotState.MOVE_TO_LINE;
otherBot.ValueRW.LinePos = pos - offset;
}
var filler = SystemAPI.GetComponentRW<Bot>(team.ValueRO.Filler);
filler.ValueRW.LinePos = randomPondPos;
var bucket = SystemAPI.GetComponentRW<Bucket>(team.ValueRO.Bucket);
if (bucket.ValueRO.IsCarried)
{
filler.ValueRW.State = BotState.MOVE_TO_LINE;
}
else
{
filler.ValueRW.TargetPos = SystemAPI.GetComponent<LocalTransform>(team.ValueRO.Bucket).Position.xz;
filler.ValueRW.State = BotState.MOVE_TO_BUCKET;
}
}
}
}
}
}