-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEntityFollower.cs
More file actions
300 lines (271 loc) · 12.2 KB
/
Copy pathEntityFollower.cs
File metadata and controls
300 lines (271 loc) · 12.2 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using Colossal.Entities;
using Colossal.Mathematics;
using Game.Citizens;
using Game.Common;
using Game.Creatures;
using Game.Events;
using Game.Prefabs;
using Game.Rendering;
using Game.Simulation;
using Unity.Entities;
using Unity.Mathematics;
namespace FirstPersonCamera
{
/// <summary>
/// Utility class to help with Entities and choosing a follow target
/// </summary>
internal class EntityFollower
{
private readonly CameraDataModel _model;
private readonly EntityManager _entityManager;
internal EntityFollower( CameraDataModel model )
{
_model = model;
_entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
}
/// <summary>
/// Filter the current entity
/// </summary>
private void Filter( )
{
// Check and update entity based on TargetElement buffer
if ( _entityManager.TryGetBuffer<TargetElement>( _model.FollowEntity, true, out var buffer ) && buffer.Length > 0 )
{
_model.FollowEntity = buffer[0].m_Entity;
}
// Check and update entity based on CurrentTransport component
if ( TryGetComponent<CurrentTransport>( out var transport ) )
{
_model.FollowEntity = transport.m_CurrentTransport;
}
// Further processing if entity has Unspawned component
if ( HasComponent<Game.Objects.Unspawned>( ) )
{
// Check and update entity based on CurrentVehicle component
if ( TryGetComponent<CurrentVehicle>( out var vehicle ) )
{
_model.FollowEntity = vehicle.m_Vehicle;
}
// Check and update entity based on Resident component and its CurrentBuilding
else if ( TryGetComponent<Game.Creatures.Resident>( out var residentComponent ) &&
TryGetComponent<CurrentBuilding>( residentComponent.m_Citizen, out var houseResident ) )
{
_model.FollowEntity = houseResident.m_CurrentBuilding;
}
// Check and update entity based on Pet component and its CurrentBuilding
else if ( TryGetComponent<Game.Creatures.Pet>( out var petComponent ) &&
TryGetComponent<CurrentBuilding>( petComponent.m_HouseholdPet, out var housePet ) )
{
_model.FollowEntity = housePet.m_CurrentBuilding;
}
}
}
/// <summary>
/// Shortcut for checking if a component exists
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
private bool HasComponent<T>( )
where T : unmanaged, IComponentData
{
return _entityManager.HasComponent<T>( _model.FollowEntity );
}
/// <summary>
/// Shortcut for getting a component
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="component"></param>
/// <returns></returns>
private bool TryGetComponent<T>( out T component )
where T : unmanaged, IComponentData
{
return _entityManager.TryGetComponent( _model.FollowEntity, out component );
}
/// <summary>
/// Shortcut for getting a component
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entity"></param>
/// <param name="component"></param>
/// <returns></returns>
private bool TryGetComponent<T>( Entity entity, out T component )
where T : unmanaged, IComponentData
{
return _entityManager.TryGetComponent( entity, out component );
}
/// <summary>
/// Get an object entity position
/// </summary>
/// <param name="entity"></param>
/// <param name="transform"></param>
/// <param name="bounds"></param>
/// <returns></returns>
private Game.Objects.Transform GetObjectPosition( Entity entity, Game.Objects.Transform transform, out Bounds3 bounds )
{
bounds = new Bounds3( transform.m_Position, transform.m_Position );
if ( _entityManager.TryGetComponent<PrefabRef>( entity, out var prefab ) &&
_entityManager.TryGetComponent<ObjectGeometryData>( prefab.m_Prefab, out var geometry ) )
bounds = Game.Objects.ObjectUtils.CalculateBounds( transform.m_Position, transform.m_Rotation, geometry );
return transform;
}
/// <summary>
/// Get the interpolated position of an entity
/// </summary>
/// <param name="entity"></param>
/// <param name="transformFrames"></param>
/// <param name="bounds"></param>
/// <returns></returns>
private Game.Objects.Transform GetInterpolatedPosition( Entity entity, DynamicBuffer<Game.Objects.TransformFrame> transformFrames, out Bounds3 bounds )
{
var systemManaged = _entityManager.World.GetOrCreateSystemManaged<RenderingSystem>( );
var sharedComponent = _entityManager.GetSharedComponent<UpdateFrame>( entity );
ObjectInterpolateSystem.CalculateUpdateFrames( systemManaged.frameIndex, systemManaged.frameTime, sharedComponent.m_Index, out var updateFrame1, out var updateFrame2, out var framePosition );
var interpolatedTransform = ObjectInterpolateSystem.CalculateTransform( transformFrames[( int ) updateFrame1], transformFrames[( int ) updateFrame2], framePosition);
return GetObjectPosition( entity, interpolatedTransform.ToTransform( ), out bounds );
}
/// <summary>
/// Get the relative position of an entity
/// </summary>
/// <param name="entity"></param>
/// <param name="relative"></param>
/// <param name="bounds"></param>
/// <returns></returns>
private Game.Objects.Transform GetRelativePosition( Entity entity, Game.Objects.Relative relative, out Bounds3 bounds )
{
var transform = _entityManager.GetComponentData<Game.Objects.Transform>( entity );
var entity1 = Entity.Null;
if ( TryGetComponent<CurrentVehicle>( entity, out var vehicle ) )
{
entity1 = vehicle.m_Vehicle;
}
else
{
if ( TryGetComponent<Owner>( entity, out var owner ) )
entity1 = owner.m_Owner;
}
if ( _entityManager.TryGetBuffer<Game.Objects.TransformFrame>( entity1, true, out var buffer ) )
{
transform = Game.Objects.ObjectUtils.LocalToWorld( GetInterpolatedPosition( entity1, buffer, out Bounds3 _ ), relative.ToTransform( ) );
}
else
{
if ( TryGetComponent<Game.Objects.Transform>( entity1, out var component3 ) )
transform = Game.Objects.ObjectUtils.LocalToWorld( component3, relative.ToTransform( ) );
}
return GetObjectPosition( entity, transform, out bounds );
}
/// <summary>
/// Get position, rotation and bounds of an entity
/// </summary>
/// <param name="position"></param>
/// <param name="bounds"></param>
/// <param name="rotation"></param>
/// <returns></returns>
public bool TryGetPosition( out float3 position, out Bounds3 bounds, out quaternion rotation )
{
position = default;
bounds = default;
rotation = default;
Filter();
if ( _entityManager.TryGetBuffer<Game.Objects.TransformFrame>( _model.FollowEntity, true, out var buffer1 ) )
{
var interpolatedPosition = GetInterpolatedPosition( _model.FollowEntity, buffer1, out bounds );
position = interpolatedPosition.m_Position;
rotation = interpolatedPosition.m_Rotation;
}
else
{
if ( _entityManager.TryGetComponent<Game.Objects.Relative>( _model.FollowEntity, out var component1 ) )
{
var relativePosition = GetRelativePosition( _model.FollowEntity, component1, out bounds );
position = relativePosition.m_Position;
rotation = relativePosition.m_Rotation;
}
else
{
if ( _entityManager.TryGetComponent<Game.Objects.Transform>( _model.FollowEntity, out var component2 ) )
{
var objectPosition = GetObjectPosition( _model.FollowEntity, component2, out bounds );
position = objectPosition.m_Position;
rotation = objectPosition.m_Rotation;
}
}
}
return true;
}
/// <summary>
/// Get an entity position
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public bool TryGetPosition( out float3 position )
{
position = default;
Filter( );
//if ( _entityManager.TryGetComponent<InterpolatedTransform>( _model.FollowEntity, out var interpolatedTransform ) )
//{
// position = interpolatedTransform.m_Position;
//}
if ( _entityManager.TryGetBuffer<Game.Objects.TransformFrame>( _model.FollowEntity, true, out var buffer1 ) )
{
var interpolatedPosition = GetInterpolatedPosition( _model.FollowEntity, buffer1, out _ );
position = interpolatedPosition.m_Position;
}
else
{
if ( _entityManager.TryGetComponent<Game.Objects.Relative>( _model.FollowEntity, out var component1 ) )
{
var relativePosition = GetRelativePosition( _model.FollowEntity, component1, out _ );
position = relativePosition.m_Position;
}
else
{
if ( _entityManager.TryGetComponent<Game.Objects.Transform>( _model.FollowEntity, out var component2 ) )
{
var objectPosition = GetObjectPosition( _model.FollowEntity, component2, out _ );
position = objectPosition.m_Position;
}
}
}
return true;
}
/// <summary>
/// Get an entity rotation
/// </summary>
/// <param name="rotation"></param>
/// <param name="entity"></param>
/// <returns></returns>
public bool TryGetRotation( out quaternion rotation, Entity entity = default )
{
rotation = default;
var workingEntity = entity != default ? entity : _model.FollowEntity;
Filter( );
if ( _entityManager.TryGetComponent<InterpolatedTransform>( _model.FollowEntity, out var interpolatedTransform ) )
{
rotation = interpolatedTransform.m_Rotation;
}
else if ( _entityManager.TryGetBuffer<Game.Objects.TransformFrame>( workingEntity, true, out var buffer1 ) )
{
var interpolatedPosition = GetInterpolatedPosition( workingEntity, buffer1, out _ );
rotation = interpolatedPosition.m_Rotation;
}
else
{
if ( _entityManager.TryGetComponent<Game.Objects.Relative>( workingEntity, out var component1 ) )
{
var relativePosition = GetRelativePosition( workingEntity, component1, out _ );
rotation = relativePosition.m_Rotation;
}
else
{
if ( _entityManager.TryGetComponent<Game.Objects.Transform>( workingEntity, out var component2 ) )
{
var objectPosition = GetObjectPosition( workingEntity, component2, out _ );
rotation = objectPosition.m_Rotation;
}
}
}
return true;
}
}
}