using System; using System.Collections.Generic; using UnityEngine; namespace Fusion.Addons.Physics { /// /// This base class exists to allow for additional Physics Scenes to be accounted for in Physics simulation, /// in addition to the Physics Scenes associated with the NetworkRunner. /// /// public abstract class RunnerSimulatePhysicsBase : RunnerSimulatePhysicsBase where TPhysicsScene : struct, IEquatable { /// /// Wrapper for physics scene reference. /// protected struct AdditionalScene { public TPhysicsScene PhysicsScene; public bool ForwardOnly; } /// /// List of additional physics scenes that should be simulated by Fusion. /// protected List _additionalScenes; /// /// Register a Physics Scene to be simulated by Fusion. /// /// The Physics Scene to include in simulation. /// Indicate if this additional scene should not resimulate. /// Typically this will be Forward, if you want to simulate physics locally for non-networked objects (such as rag dolls) public void RegisterAdditionalScene(TPhysicsScene scene, bool forwardOnly = false) { if (_additionalScenes == null) { _additionalScenes = new List(); } else { foreach (var entry in _additionalScenes) { if (entry.PhysicsScene.Equals(scene)) { Debug.LogWarning("Scene already registered."); return; } } } _additionalScenes.Add(new AdditionalScene(){PhysicsScene = scene, ForwardOnly = forwardOnly}); } /// /// Unregister a Physics Scene, and it will not longer have calls made to Simulate() by this component. /// /// public void UnregisterAdditionalScene(TPhysicsScene scene) { if (_additionalScenes == null) { Debug.LogWarning("Scene was never registered, cannot unregister."); return; } int? found = null; for (int i = 0; i < _additionalScenes.Count; i++) { if (_additionalScenes[i].PhysicsScene.Equals(scene)) { found = i; break; } } if (found.HasValue) { _additionalScenes.RemoveAt(found.Value); } } } }