Last active
June 27, 2022 09:21
-
-
Save imerr/968a89c17715ed29b32e0f1171d4d801 to your computer and use it in GitHub Desktop.
DeterministicObject parent stuff (untested)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct DeterministicObjectSpawnState { | |
public byte PrefabIndex; | |
public Vector3 Position; | |
public Quaternion Rotation; | |
// ... | |
} | |
public class DeterministicObject : MonoBehaviour { | |
public void Init(DeterministicObjectSpawnState state) { | |
// ... do something ... | |
} | |
} | |
public class DeterministicObjectContainer : NetworkBehaviour { | |
public GameObject[] Prefabs; | |
public SyncList<DeterministicObjectSpawnState> Spawns = new SyncList<DeterministicObjectSpawnState>(); | |
private List<GameObject> _spawnedObjects = new List<GameObject>(); | |
private void Awake() { | |
Spawns.Callback += OnSpawnsChanged; | |
} | |
[Server] | |
public void ServerAdd(DeterministicObjectSpawnState state) { | |
Spawns.Add(state); | |
if (!NetworkClient.active) { | |
// if not hostmode, we need to call these ourselves | |
OnAdd(state); | |
} | |
} | |
[Server] | |
public void ServerRemove(int index) { | |
Spawns.RemoveAt(index); | |
if (!NetworkClient.active) { | |
// if not hostmode, we need to call these ourselves | |
OnRemoveAt(index); | |
} | |
} | |
// etc | |
private void OnSpawnsChanged(SyncList<DeterministicObjectSpawnState>.Operation op, int itemindex, DeterministicObjectSpawnState olditem, DeterministicObjectSpawnState newitem) { | |
switch (op) { | |
case SyncList<DeterministicObjectSpawnState>.Operation.OP_ADD: | |
OnAdd(newitem); | |
break; | |
case SyncList<DeterministicObjectSpawnState>.Operation.OP_CLEAR: | |
OnClear(); | |
break; | |
case SyncList<DeterministicObjectSpawnState>.Operation.OP_INSERT: | |
OnInsert(itemindex, newitem); | |
break; | |
case SyncList<DeterministicObjectSpawnState>.Operation.OP_REMOVEAT: | |
OnRemoveAt(itemindex); | |
break; | |
case SyncList<DeterministicObjectSpawnState>.Operation.OP_SET: | |
OnSet(itemindex, newitem); | |
break; | |
default: | |
throw new ArgumentOutOfRangeException(nameof(op), op, null); | |
} | |
} | |
private void OnSet(int itemindex, DeterministicObjectSpawnState state) { | |
Destroy(_spawnedObjects[itemindex]); | |
var c = Instantiate(state); | |
c.Init(state); | |
_spawnedObjects[itemindex] = c.gameObject; | |
} | |
private void OnRemoveAt(int itemindex) { | |
Destroy(_spawnedObjects[itemindex]); | |
_spawnedObjects.RemoveAt(itemindex); | |
} | |
private void OnInsert(int itemindex, DeterministicObjectSpawnState state) { | |
var c = Instantiate(state); | |
c.Init(state); | |
_spawnedObjects.Insert(itemindex, c.gameObject); | |
} | |
private DeterministicObject Instantiate(DeterministicObjectSpawnState state) { | |
var prefab = Prefabs[state.PrefabIndex]; | |
var go = Instantiate(prefab, state.Position, state.Rotation, transform); | |
return go.GetComponent<DeterministicObject>(); | |
} | |
private void OnAdd(DeterministicObjectSpawnState state) { | |
var c = Instantiate(state); | |
c.Init(state); | |
_spawnedObjects.Add(c.gameObject); | |
} | |
private void OnClear() { | |
foreach (var spawnedObject in _spawnedObjects) { | |
Destroy(spawnedObject); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment