forked from Unity-Technologies/ECS-Network-Racing-Sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cs
More file actions
217 lines (189 loc) · 6.44 KB
/
Copy pathPlayer.cs
File metadata and controls
217 lines (189 loc) · 6.44 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
using Unity.Collections;
using Unity.Mathematics;
using Unity.NetCode;
using Unity.Physics;
using Unity.Properties;
using Unity.Transforms;
namespace Unity.Entities.Racing.Common
{
/// <summary>
/// Added to a player who already has a name
/// </summary>
public struct PlayerNameTag : IComponentData
{
}
/// <summary>
/// Stores the player's name
/// </summary>
public struct PlayerName : IComponentData
{
[GhostField] public FixedString64Bytes Name;
}
/// <summary>
/// Access to player's name component
/// </summary>
public readonly partial struct PlayerNameAspect : IAspect
{
public readonly Entity Self;
readonly RefRO<PlayerName> m_PlayerName;
public FixedString64Bytes Name => m_PlayerName.ValueRO.Name;
}
/// <summary>
/// Tags the local player for filtering
/// </summary>
public struct LocalUser : IComponentData
{
}
public enum PlayerState
{
None,
Lobby,
ReadyToRace,
StartingRace,
Countdown,
Race,
CelebrationIdle,
Finished,
Leaderboard,
}
/// <summary>
/// Stores the initial player values
/// </summary>
public struct Reset : IComponentData
{
[GhostField] public float3 TargetPosition;
[GhostField] public quaternion TargetRotation;
[GhostField] public bool Wheels;
[GhostField] public bool Transform;
}
/// <summary>
/// Access the player's parameters in the race.
/// </summary>
public struct Player : IComponentData
{
[GhostField] public PlayerState State;
public bool StartingRace => State == PlayerState.StartingRace;
public bool InRace => State == PlayerState.Race;
public bool HasFinished => State == PlayerState.Finished;
public bool IsCelebrating => State == PlayerState.CelebrationIdle;
}
/// <summary>
/// Access the only the local player information
/// </summary>
public readonly partial struct LocalPlayerAspect : IAspect
{
public readonly RefRW<CarInput> CarInput;
private readonly TransformAspect m_Transform;
private readonly RefRO<LapProgress> m_LapProgress;
private readonly RefRW<Player> m_Player;
private readonly RefRW<LocalUser> m_LocalUser;
private readonly RefRO<LocalToWorld> m_LocalToWorld;
public Player Player => m_Player.ValueRO;
public LapProgress LapProgress => m_LapProgress.ValueRO;
public LocalToWorld LocalToWorld => m_LocalToWorld.ValueRO;
public bool HasAnyInput()
{
return math.abs(CarInput.ValueRO.Horizontal) > 0 || math.abs(CarInput.ValueRO.Vertical)>0;
}
public bool HasValidPosition()
{
var isFinite = math.isfinite(m_Transform.LocalPosition);
return isFinite.x && isFinite.y && isFinite.z;
}
}
/// <summary>
/// Access all players information
/// </summary>
public readonly partial struct PlayerAspect : IAspect
{
public readonly Entity Self;
public readonly TransformAspect Transform;
readonly RefRO<LocalToWorld> m_LocalToWorld;
readonly RefRO<GhostOwnerComponent> m_GhostOwner;
readonly RefRO<Rank> m_Rank;
readonly RefRO<PlayerName> m_Name;
readonly RefRW<Player> m_Player;
readonly RefRW<Skin> m_Skin;
readonly RefRW<LapProgress> m_LapProgress;
readonly RefRW<PhysicsVelocity> m_Velocity;
readonly RefRW<Reset> m_Reset;
public int NetworkId => m_GhostOwner.ValueRO.NetworkId;
public int Rank => m_Rank.ValueRO.Value;
public FixedString64Bytes Name => m_Name.ValueRO.Name;
public bool IsCelebrating => Player.IsCelebrating && LapProgress.CelebrationIdleDelay > 0;
public bool FinishedCelebration => Player.IsCelebrating && LapProgress.CelebrationIdleDelay <= 0;
public bool HasArrived => (Player.IsCelebrating || Player.HasFinished) && LapProgress.HasArrived;
[CreateProperty]
public LapProgress LapProgress => m_LapProgress.ValueRO;
public void AddedToLeaderboard()
{
m_LapProgress.ValueRW.AddedToLeaderboard = true;
}
public void ResetLapProgress()
{
m_LapProgress.ValueRW.Reset();
}
public void ResetCheckpoint()
{
m_LapProgress.ValueRW.CurrentCheckPoint = 0;
}
public void SetCheckPoint(float3 value)
{
m_LapProgress.ValueRW.LastCheckPointPosition = value;
}
public void IncreaseLapCount()
{
m_LapProgress.ValueRW.CurrentLap++;
}
public void RestLapCount()
{
m_LapProgress.ValueRW.CurrentLap = 0;
}
public void ReduceCelebrationTimer(float value)
{
m_LapProgress.ValueRW.CelebrationIdleDelay -= value;
}
public Player Player => m_Player.ValueRO;
public Skin Skin => m_Skin.ValueRO;
public PhysicsVelocity Velocity => m_Velocity.ValueRO;
public Reset Reset => m_Reset.ValueRO;
public LocalToWorld LocalToWorld => m_LocalToWorld.ValueRO;
public void UpdateSkin (bool value = false)
{
m_Skin.ValueRW.NeedUpdate = value;
}
public void SetCelebration(float time, double elapseTime)
{
m_Player.ValueRW.State = PlayerState.CelebrationIdle;
m_LapProgress.ValueRW.CelebrationIdleDelay = time;
m_LapProgress.ValueRW.ArrivalTime = elapseTime;
}
public void SetFinishedRace()
{
m_Player.ValueRW.State = PlayerState.Finished;
}
public void ResetVelocity()
{
m_Velocity.ValueRW.Linear = float3.zero;
m_Velocity.ValueRW.Angular = float3.zero;
}
public void ResetVehicle()
{
m_Reset.ValueRW.Transform = true;
m_Reset.ValueRW.Wheels = true;
}
public void SetTargetTransform(float3 position, quaternion rotation)
{
m_Reset.ValueRW.TargetPosition = position;
m_Reset.ValueRW.TargetRotation = rotation;
}
public void SetPlayerTransformReady()
{
m_Reset.ValueRW.Transform = false;
}
public void SetPlayerWheelsReady()
{
m_Reset.ValueRW.Wheels = false;
}
}
}