-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerCharacterMovement.cs
285 lines (247 loc) · 9.58 KB
/
ServerCharacterMovement.cs
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
using System;
using Unity.BossRoom.Gameplay.Configuration;
using Unity.BossRoom.Navigation;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Assertions;
namespace Unity.BossRoom.Gameplay.GameplayObjects.Character
{
public enum MovementState
{
Idle = 0,
PathFollowing = 1,
Charging = 2,
Knockback = 3,
}
/// <summary>
/// Component responsible for moving a character on the server side based on inputs.
/// </summary>
/*[RequireComponent(typeof(NetworkCharacterState), typeof(NavMeshAgent), typeof(ServerCharacter)), RequireComponent(typeof(Rigidbody))]*/
public class ServerCharacterMovement : NetworkBehaviour
{
[SerializeField]
NavMeshAgent m_NavMeshAgent;
[SerializeField]
Rigidbody m_Rigidbody;
private NavigationSystem m_NavigationSystem;
private DynamicNavPath m_NavPath;
private MovementState m_MovementState;
MovementStatus m_PreviousState;
[SerializeField]
private ServerCharacter m_CharLogic;
// when we are in charging and knockback mode, we use these additional variables
private float m_ForcedSpeed;
private float m_SpecialModeDurationRemaining;
// this one is specific to knockback mode
private Vector3 m_KnockbackVector;
#if UNITY_EDITOR || DEVELOPMENT_BUILD
public bool TeleportModeActivated { get; set; }
const float k_CheatSpeed = 20;
public bool SpeedCheatActivated { get; set; }
#endif
void Awake()
{
// disable this NetworkBehavior until it is spawned
enabled = false;
}
public override void OnNetworkSpawn()
{
if (IsServer)
{
// Only enable server component on servers
enabled = true;
// On the server enable navMeshAgent and initialize
m_NavMeshAgent.enabled = true;
m_NavigationSystem = GameObject.FindGameObjectWithTag(NavigationSystem.NavigationSystemTag).GetComponent<NavigationSystem>();
m_NavPath = new DynamicNavPath(m_NavMeshAgent, m_NavigationSystem);
}
}
/// <summary>
/// Sets a movement target. We will path to this position, avoiding static obstacles.
/// </summary>
/// <param name="position">Position in world space to path to. </param>
public void SetMovementTarget(Vector3 position)
{
#if UNITY_EDITOR || DEVELOPMENT_BUILD
if (TeleportModeActivated)
{
Teleport(position);
return;
}
#endif
m_MovementState = MovementState.PathFollowing;
m_NavPath.SetTargetPosition(position);
}
public void StartForwardCharge(float speed, float duration)
{
m_NavPath.Clear();
m_MovementState = MovementState.Charging;
m_ForcedSpeed = speed;
m_SpecialModeDurationRemaining = duration;
}
public void StartKnockback(Vector3 knocker, float speed, float duration)
{
m_NavPath.Clear();
m_MovementState = MovementState.Knockback;
m_KnockbackVector = transform.position - knocker;
m_ForcedSpeed = speed;
m_SpecialModeDurationRemaining = duration;
}
/// <summary>
/// Follow the given transform until it is reached.
/// </summary>
/// <param name="followTransform">The transform to follow</param>
public void FollowTransform(Transform followTransform)
{
m_MovementState = MovementState.PathFollowing;
m_NavPath.FollowTransform(followTransform);
}
/// <summary>
/// Returns true if the current movement-mode is unabortable (e.g. a knockback effect)
/// </summary>
/// <returns></returns>
public bool IsPerformingForcedMovement()
{
return m_MovementState == MovementState.Knockback || m_MovementState == MovementState.Charging;
}
/// <summary>
/// Returns true if the character is actively moving, false otherwise.
/// </summary>
/// <returns></returns>
public bool IsMoving()
{
return m_MovementState != MovementState.Idle;
}
/// <summary>
/// Cancels any moves that are currently in progress.
/// </summary>
public void CancelMove()
{
if (m_NavPath != null)
{
m_NavPath.Clear();
}
m_MovementState = MovementState.Idle;
}
/// <summary>
/// Instantly moves the character to a new position. NOTE: this cancels any active movement operation!
/// This does not notify the client that the movement occurred due to teleportation, so that needs to
/// happen in some other way, such as with the custom action visualization in DashAttackActionFX. (Without
/// this, the clients will animate the character moving to the new destination spot, rather than instantly
/// appearing in the new spot.)
/// </summary>
/// <param name="newPosition">new coordinates the character should be at</param>
public void Teleport(Vector3 newPosition)
{
CancelMove();
if (!m_NavMeshAgent.Warp(newPosition))
{
// warping failed! We're off the navmesh somehow. Weird... but we can still teleport
Debug.LogWarning($"NavMeshAgent.Warp({newPosition}) failed!", gameObject);
transform.position = newPosition;
}
m_Rigidbody.position = transform.position;
m_Rigidbody.rotation = transform.rotation;
}
private void FixedUpdate()
{
PerformMovement();
var currentState = GetMovementStatus(m_MovementState);
if (m_PreviousState != currentState)
{
m_CharLogic.MovementStatus.Value = currentState;
m_PreviousState = currentState;
}
}
public override void OnNetworkDespawn()
{
if (m_NavPath != null)
{
m_NavPath.Dispose();
}
if (IsServer)
{
// Disable server components when despawning
enabled = false;
m_NavMeshAgent.enabled = false;
}
}
private void PerformMovement()
{
if (m_MovementState == MovementState.Idle)
return;
Vector3 movementVector;
if (m_MovementState == MovementState.Charging)
{
// if we're done charging, stop moving
m_SpecialModeDurationRemaining -= Time.fixedDeltaTime;
if (m_SpecialModeDurationRemaining <= 0)
{
m_MovementState = MovementState.Idle;
return;
}
var desiredMovementAmount = m_ForcedSpeed * Time.fixedDeltaTime;
movementVector = transform.forward * desiredMovementAmount;
}
else if (m_MovementState == MovementState.Knockback)
{
m_SpecialModeDurationRemaining -= Time.fixedDeltaTime;
if (m_SpecialModeDurationRemaining <= 0)
{
m_MovementState = MovementState.Idle;
return;
}
var desiredMovementAmount = m_ForcedSpeed * Time.fixedDeltaTime;
movementVector = m_KnockbackVector * desiredMovementAmount;
}
else
{
var desiredMovementAmount = GetBaseMovementSpeed() * Time.fixedDeltaTime;
movementVector = m_NavPath.MoveAlongPath(desiredMovementAmount);
// If we didn't move stop moving.
if (movementVector == Vector3.zero)
{
m_MovementState = MovementState.Idle;
return;
}
}
m_NavMeshAgent.Move(movementVector);
transform.rotation = Quaternion.LookRotation(movementVector);
// After moving adjust the position of the dynamic rigidbody.
m_Rigidbody.position = transform.position;
m_Rigidbody.rotation = transform.rotation;
}
/// <summary>
/// Retrieves the speed for this character's class.
/// </summary>
private float GetBaseMovementSpeed()
{
#if UNITY_EDITOR || DEVELOPMENT_BUILD
if (SpeedCheatActivated)
{
return k_CheatSpeed;
}
#endif
CharacterClass characterClass = GameDataSource.Instance.CharacterDataByType[m_CharLogic.CharacterType];
Assert.IsNotNull(characterClass, $"No CharacterClass data for character type {m_CharLogic.CharacterType}");
return characterClass.Speed;
}
/// <summary>
/// Determines the appropriate MovementStatus for the character. The
/// MovementStatus is used by the client code when animating the character.
/// </summary>
private MovementStatus GetMovementStatus(MovementState movementState)
{
switch (movementState)
{
case MovementState.Idle:
return MovementStatus.Idle;
case MovementState.Knockback:
return MovementStatus.Uncontrolled;
default:
return MovementStatus.Normal;
}
}
}
}