-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredictedProjectile.cs
More file actions
187 lines (162 loc) · 4.55 KB
/
PredictedProjectile.cs
File metadata and controls
187 lines (162 loc) · 4.55 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using FishNet;
using FishNet.Object;
using FishNet.Object.Prediction;
using FishNet.Transporting;
using UnityEngine;
public class PredictedProjectile : NetworkBehaviour
{
private struct MoveData : IReplicateData
{
public Vector3 Force;
private uint tick;
public MoveData(Vector3 force)
{
Force = force;
tick = default;
}
public uint GetTick() => tick;
public void SetTick(uint value) => tick = value;
public void Dispose() { }
}
private struct ReconcileData : IReconcileData
{
public Vector3 Position;
public Quaternion Rotation;
private uint tick;
public uint GetTick() => tick;
public void SetTick(uint value) => tick = value;
public void Dispose() { }
public ReconcileData(Vector3 position, Quaternion rotation)
{
Position = position;
Rotation = rotation;
tick = default;
}
}
//public float MoveSpeed;
public int Damage;
public float Speed;
private bool AtMaxSpeed = false;
public float MaxSpeed;
private Rigidbody _rigidbody;
private bool subscribed = false;
private MoveData _moveData = new MoveData();
private void Awake()
{
_rigidbody = GetComponent<Rigidbody>();
}
private void SubscribeToTimeEvents(bool _subcribe)
{
if (base.TimeManager == null)
{
return;
}
if (_subcribe == subscribed)
{
return;
}
subscribed = _subcribe;
if (_subcribe)
{
base.TimeManager.OnTick += TimeManager_OnTick;
base.TimeManager.OnPostTick += TimeManager_OnPostTick;
}
else
{
base.TimeManager.OnTick -= TimeManager_OnTick;
base.TimeManager.OnPostTick -= TimeManager_OnPostTick;
}
}
private void Initialize(Vector3 _force)
{
_moveData.Force = _force;
SubscribeToTimeEvents(true);
StartCoroutine(AutoDestroy());
}
private IEnumerator AutoDestroy()
{
yield return new WaitForSeconds(3f);
InstanceFinder.ServerManager.Despawn(gameObject);
}
private void OnTriggerEnter(Collider other)
{
if (!base.HasAuthority)
{
return;
}
StopAllCoroutines();
if (!other.CompareTag("Map"))
{
other.GetComponent<ServerSideHealth>().Damage(Damage);
}
InstanceFinder.ServerManager.Despawn(gameObject);
}
public override void OnStartNetwork()
{
base.OnStartNetwork();
if (base.IsServerInitialized && !Owner.IsValid)
{
Initialize(transform.forward * Speed);
Debug.DrawRay(transform.position, transform.forward, Color.yellow, 3f);
}
else
{
SubscribeToTimeEvents(true);
}
}
private void OnDestroy()
{
SubscribeToTimeEvents(false);
}
private void TimeManager_OnTick()
{
if (base.IsOwner)
{
Reconciliation(default);
//Move(_moveData);
}
else if (base.IsServerInitialized)
{
if (AtMaxSpeed)
{
return;
}
Move(_moveData);
}
}
private void TimeManager_OnPostTick()
{
CreateReconcile();
}
public override void CreateReconcile()
{
ReconcileData data = new ReconcileData(
transform.position,
transform.rotation
);
Reconciliation(data);
}
[Replicate]
private void Move(MoveData data, ReplicateState state = ReplicateState.Invalid, Channel channel = Channel.Unreliable)
{
float currentSpeed = Vector3.SqrMagnitude(_rigidbody.velocity);
if (currentSpeed < MaxSpeed)
{
_rigidbody.AddForce(data.Force, ForceMode.VelocityChange);
}
else
{
AtMaxSpeed = true;
}
}
[Reconcile]
private void Reconciliation(ReconcileData data, Channel channel = Channel.Unreliable)
{
transform.position = data.Position;
transform.rotation = data.Rotation;
}
}