149 lines
4.6 KiB
C#
149 lines
4.6 KiB
C#
using MLAPI.NetworkingManagerComponents;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace MLAPI
|
|
{
|
|
public class NetworkedObject : MonoBehaviour
|
|
{
|
|
[HideInInspector]
|
|
public uint NetworkId;
|
|
[HideInInspector]
|
|
public int OwnerClientId = -2;
|
|
[HideInInspector]
|
|
public int SpawnablePrefabIndex;
|
|
[HideInInspector]
|
|
public bool isPlayerObject = false;
|
|
public bool ServerOnly = false;
|
|
[HideInInspector]
|
|
public bool isPooledObject = false;
|
|
[HideInInspector]
|
|
public ushort PoolId;
|
|
public bool isLocalPlayer
|
|
{
|
|
get
|
|
{
|
|
return isPlayerObject && (OwnerClientId == NetworkingManager.singleton.MyClientId || (OwnerClientId == -1 && NetworkingManager.singleton.isHost));
|
|
}
|
|
}
|
|
|
|
public bool isOwner
|
|
{
|
|
get
|
|
{
|
|
return !isPlayerObject && (OwnerClientId == NetworkingManager.singleton.MyClientId || (OwnerClientId == -1 && NetworkingManager.singleton.isHost));
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
SpawnManager.OnDestroyObject(NetworkId, false);
|
|
}
|
|
|
|
internal bool isSpawned = false;
|
|
|
|
public void Spawn()
|
|
{
|
|
SpawnManager.OnSpawnObject(this);
|
|
}
|
|
|
|
public void SpawnWithOwnership(int clientId)
|
|
{
|
|
SpawnManager.OnSpawnObject(this, clientId);
|
|
}
|
|
|
|
public void RemoveOwnership()
|
|
{
|
|
SpawnManager.RemoveOwnership(NetworkId);
|
|
}
|
|
|
|
public void ChangeOwnership(int newOwnerClientId)
|
|
{
|
|
SpawnManager.ChangeOwnership(NetworkId, newOwnerClientId);
|
|
}
|
|
|
|
internal void InvokeBehaviourOnLostOwnership()
|
|
{
|
|
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
|
|
{
|
|
childNetworkedBehaviours[i].OnLostOwnership();
|
|
}
|
|
}
|
|
|
|
internal void InvokeBehaviourOnGainedOwnership()
|
|
{
|
|
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
|
|
{
|
|
childNetworkedBehaviours[i].OnGainedOwnership();
|
|
}
|
|
}
|
|
|
|
internal void InvokeBehaviourNetworkSpawn()
|
|
{
|
|
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
|
|
{
|
|
//We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject.
|
|
if(!childNetworkedBehaviours[i].networkedStartInvoked)
|
|
{
|
|
childNetworkedBehaviours[i].NetworkStart();
|
|
if (NetworkingManager.singleton.isServer)
|
|
childNetworkedBehaviours[i].SyncVarInit();
|
|
}
|
|
}
|
|
}
|
|
|
|
private List<NetworkedBehaviour> _childNetworkedBehaviours;
|
|
internal List<NetworkedBehaviour> childNetworkedBehaviours
|
|
{
|
|
get
|
|
{
|
|
if(_childNetworkedBehaviours == null)
|
|
{
|
|
_childNetworkedBehaviours = new List<NetworkedBehaviour>();
|
|
NetworkedBehaviour[] behaviours = GetComponentsInChildren<NetworkedBehaviour>();
|
|
for (int i = 0; i < behaviours.Length; i++)
|
|
{
|
|
if (behaviours[i].networkedObject == this)
|
|
_childNetworkedBehaviours.Add(behaviours[i]);
|
|
}
|
|
}
|
|
return _childNetworkedBehaviours;
|
|
}
|
|
}
|
|
|
|
internal static List<NetworkedBehaviour> NetworkedBehaviours = new List<NetworkedBehaviour>();
|
|
internal static void InvokeSyncvarUpdate()
|
|
{
|
|
for (int i = 0; i < NetworkedBehaviours.Count; i++)
|
|
{
|
|
NetworkedBehaviours[i].SyncVarUpdate();
|
|
}
|
|
}
|
|
|
|
//Flushes all syncVars to client
|
|
internal void FlushToClient(int clientId)
|
|
{
|
|
for (int i = 0; i < childNetworkedBehaviours.Count; i++)
|
|
{
|
|
childNetworkedBehaviours[i].FlushToClient(clientId);
|
|
}
|
|
}
|
|
|
|
internal ushort GetOrderIndex(NetworkedBehaviour instance)
|
|
{
|
|
for (ushort i = 0; i < childNetworkedBehaviours.Count; i++)
|
|
{
|
|
if (childNetworkedBehaviours[i] == instance)
|
|
return i;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
internal NetworkedBehaviour GetBehaviourAtOrderIndex(ushort index)
|
|
{
|
|
//TODO index out of bounds
|
|
return childNetworkedBehaviours[index];
|
|
}
|
|
}
|
|
}
|