-
Notifications
You must be signed in to change notification settings - Fork 1
/
PhysicsNetworkUpdater.cs
74 lines (57 loc) · 2.23 KB
/
PhysicsNetworkUpdater.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
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Serialization;
namespace SavageCodes.Networking.ClientSidePrediction
{
//TODO: this class need a huge clean up and maybe optimizations
public class PhysicsNetworkUpdater : MonoBehaviour
{
private static PhysicsNetworkUpdater _instance;
Dictionary<GameObject, Tuple<Scene, PhysicsScene>> _PhysicsScenes =
new Dictionary<GameObject, Tuple<Scene, PhysicsScene>>();
[FormerlySerializedAs("StaticWorld")] [SerializeField]
public GameObject _staticWorld;
public static PhysicsNetworkUpdater Instance => _instance;
void Awake()
{
if (_instance != null)
{
Destroy(_instance);
}
_instance = this;
}
public void CreatePhysicsSceneForGO(GameObject GO)
{
CreateSceneParameters csp = new CreateSceneParameters(LocalPhysicsMode.Physics3D);
Scene PhysicsScene =
SceneManager.CreateScene(
"PSFor: " + GO.name + " NetID " + GO.GetComponent<PredictedNetworkMovement>().netId.Value,
csp);
_PhysicsScenes.Add(GO, Tuple.Create(PhysicsScene, PhysicsScene.GetPhysicsScene()));
SceneManager.MoveGameObjectToScene(GetStaticWorldNoRenderer(), PhysicsScene);
SceneManager.MoveGameObjectToScene(GO, PhysicsScene);
}
public void DestroyPhysicsSceneOfGO(GameObject GO)
{
SceneManager.UnloadSceneAsync(_PhysicsScenes[GO].Item1);
}
GameObject GetStaticWorldNoRenderer()
{
var world = Instantiate(_staticWorld);
var renderers = world.GetComponentsInChildren<MeshRenderer>();
var meshes = world.GetComponentsInChildren<MeshFilter>();
for (int i = 0; i < renderers.Length; i++)
{
Destroy(renderers[i]);
Destroy(meshes[i]);
}
return world;
}
public void UpdatePhysics(PredictedNetworkMovement NM)
{
_PhysicsScenes[NM.gameObject].Item2.Simulate(Time.fixedDeltaTime);
}
}
}