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 ClientPhysicsSimulation ClientPhysicsSimulation;
}
///
/// 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.
/// Defines physics simulation of the additional scene for clients.
/// Typically this will be Disabled (physics not simulated) or SimulateForward (if you want to simulate physics locally for non-networked objects such as rag dolls).
public void RegisterAdditionalScene(TPhysicsScene scene, ClientPhysicsSimulation clientPhysicsSimulation = ClientPhysicsSimulation.Disabled) {
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, ClientPhysicsSimulation = clientPhysicsSimulation});
}
///
/// 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);
}
}
protected override sealed bool AnySceneRequiresSyncTransform() {
if (_additionalScenes == null || _additionalScenes.Count == 0) {
return false;
}
foreach (var scene in _additionalScenes) {
if (RequiresSyncTransform(scene.ClientPhysicsSimulation))
return true;
}
return false;
}
}
}