forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRespawnSystem.cs
More file actions
115 lines (97 loc) · 4.81 KB
/
Copy pathRespawnSystem.cs
File metadata and controls
115 lines (97 loc) · 4.81 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
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.NetCode;
namespace Samples.HelloNetcode
{
readonly partial struct CharacterWithHealth : IAspect
{
readonly RefRW<AutoCommandTarget> m_AutoCommandTarget;
readonly RefRO<Health> m_Health;
readonly RefRO<GhostOwner> m_GhostOwner;
readonly RefRO<ConnectionOwner> m_ConnectionOwner;
public ref AutoCommandTarget AutoCommandTarget => ref m_AutoCommandTarget.ValueRW;
public GhostOwner GhostOwner => m_GhostOwner.ValueRO;
public ConnectionOwner ConnectionOwner => m_ConnectionOwner.ValueRO;
public bool IsAlive()
{
return m_Health.ValueRO.CurrentHitPoints > 0;
}
}
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
[RequireMatchingQueriesForUpdate]
[UpdateInGroup(typeof(HelloNetcodePredictedSystemGroup))]
[UpdateAfter(typeof(DamageSystem))]
public partial struct RespawnSystem : ISystem
{
Random m_Random;
public void OnCreate(ref SystemState state)
{
m_Random = new Random((uint)SystemAPI.Time.ElapsedTime + 1);
state.RequireForUpdate<Spawner>();
state.RequireForUpdate<Health>();
}
public void OnUpdate(ref SystemState state)
{
var playerPrefab = SystemAPI.GetSingleton<Spawner>().Player;
var ecb = new EntityCommandBuffer(Allocator.Temp);
var linkedEntityGroupFromEntity = SystemAPI.GetBufferLookup<LinkedEntityGroup>();
foreach (var (character, localTransform, entity) in SystemAPI.Query<CharacterWithHealth, RefRW<LocalTransform>>().WithEntityAccess())
{
if (character.IsAlive())
{
continue;
}
if (FallingDown(ref character.AutoCommandTarget, ref localTransform.ValueRW.Rotation, SystemAPI.Time.DeltaTime))
{
continue;
}
DestroyAndRespawnPlayer(ecb, entity, playerPrefab, character.GhostOwner, character.ConnectionOwner, linkedEntityGroupFromEntity);
}
ecb.Playback(state.EntityManager);
}
void DestroyAndRespawnPlayer(EntityCommandBuffer ecb, Entity entity, Entity playerPrefab, GhostOwner networkId,
ConnectionOwner connectionOwner, BufferLookup<LinkedEntityGroup> linkedEntityGroupFromEntity)
{
ecb.DestroyEntity(entity);
InitializeNewPlayer(ecb, entity, playerPrefab, networkId, connectionOwner, linkedEntityGroupFromEntity);
}
/// <summary>
/// Initialize new player at a random point within the plane. (Hardcoded to [-50;50]).
/// To patch up the network components we set CommandTarget as well as removing the destroyed entity
/// and adding the new player entity to the linked entity group of the connection entity.
/// </summary>
void InitializeNewPlayer(EntityCommandBuffer ecb, Entity destroyedPlayer, Entity newPlayer, GhostOwner networkId,
ConnectionOwner connectionOwner, BufferLookup<LinkedEntityGroup> linkedEntityGroupFromEntity)
{
var spawnedPlayer = ecb.Instantiate(newPlayer);
ecb.SetComponent(spawnedPlayer, networkId);
var newX = m_Random.NextInt(-40, 40);
var newZ = m_Random.NextInt(-40, 40);
ecb.SetComponent(spawnedPlayer, LocalTransform.FromPosition(new float3(newX, 1, newZ)));
ecb.SetComponent(connectionOwner.Entity, new CommandTarget() { targetEntity = spawnedPlayer });
ecb.AddComponent(spawnedPlayer, new ConnectionOwner { Entity = connectionOwner.Entity });
var linkedEntityGroups = linkedEntityGroupFromEntity[connectionOwner.Entity];
for (var index = 0; index < linkedEntityGroups.Length; index++)
{
var linkedEntityGroup = linkedEntityGroups[index];
if (linkedEntityGroup.Value == destroyedPlayer)
{
linkedEntityGroup.Value = newPlayer;
// linkedEntityGroups[index] = new LinkedEntityGroup { Value = spawnedPlayer };
// linkedEntityGroups.RemoveAtSwapBack(index);
// --index;
}
}
// ecb.AppendToBuffer(connectionOwner.Entity, new LinkedEntityGroup { Value = spawnedPlayer });
}
static bool FallingDown(ref AutoCommandTarget autoCommandTarget, ref quaternion rotation, float deltaTime)
{
autoCommandTarget.Enabled = false;
rotation = math.mul(rotation, quaternion.RotateZ(deltaTime));
var rotatedLessThan90Degrees = math.mul(rotation, math.up()).y > 0;
return rotatedLessThan90Degrees;
}
}
}