/// <summary> /// Send object sync. /// </summary> /// <param name="objectID">The Object ID of the object.</param> /// <param name="setOwner">Set owner of the object.</param> public void SendObjectSync(int objectID, Vector3 pos, Quaternion rot, ObjectSyncManager.SyncTypes syncType, float[] syncedVariables, ObjectSyncManager.Flags flags) { if (NetManager.Instance.IsHost) { SendObjectSyncHost(objectID, pos, rot, syncType, syncedVariables, flags); return; } Network.Messages.ObjectSyncMessage msg = new Network.Messages.ObjectSyncMessage(); msg.objectID = objectID; msg.SyncType = (int)syncType; if (syncedVariables != null) { msg.SyncedVariables = syncedVariables; } switch (flags) { case ObjectSyncManager.Flags.Full: msg.Position = Utils.GameVec3ToNet(pos); msg.Rotation = Utils.GameQuatToNet(rot); break; case ObjectSyncManager.Flags.RotationOnly: msg.Rotation = Utils.GameQuatToNet(rot); break; case ObjectSyncManager.Flags.PositionOnly: msg.Position = Utils.GameVec3ToNet(pos); break; } NetManager.Instance.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); }
/// <summary> /// Handles remove owner messages on the host. /// </summary> public static void RemoveOwnerHandler(Network.Messages.ObjectSyncMessage msg, ObjectSyncComponent osc, Steamworks.CSteamID sender) { osc.Owner = null; osc.OwnerRemoved(); // Send updated ownership info to other clients. Network.Messages.ObjectSyncMessage msgBroadcast = new Network.Messages.ObjectSyncMessage(); msgBroadcast.objectID = msg.objectID; msgBroadcast.OwnerPlayerID = NetManager.Instance.GetPlayerIDBySteamID(sender); msgBroadcast.SyncType = (int)SyncTypes.RemoveOwner; NetManager.Instance.BroadcastMessage(msgBroadcast, Steamworks.EP2PSend.k_EP2PSendReliable); }
/// <summary> /// Handles sync taken by force messages on the host. /// </summary> public static void SyncTakenByForceHandler(Network.Messages.ObjectSyncMessage msg, ObjectSyncComponent osc, Steamworks.CSteamID sender) { if (osc.Owner == NetManager.Instance.GetLocalPlayer()) { osc.SyncTakenByForce(); osc.SyncEnabled = false; } osc.Owner = NetManager.Instance.GetPlayer(sender); if (osc.Owner == NetManager.Instance.GetLocalPlayer()) { osc.SyncEnabled = true; } // Send updated ownership info to other clients. Network.Messages.ObjectSyncMessage msgBroadcast = new Network.Messages.ObjectSyncMessage(); msgBroadcast.objectID = msg.objectID; msgBroadcast.OwnerPlayerID = NetManager.Instance.GetPlayerIDBySteamID(sender); msgBroadcast.SyncType = (int)SyncTypes.ForceSetOwner; NetManager.Instance.BroadcastMessage(msgBroadcast, Steamworks.EP2PSend.k_EP2PSendReliable); }
/// <summary> /// Handle set owner messages on the host. /// </summary> public static void SetOwnerHandler(Network.Messages.ObjectSyncMessage msg, ObjectSyncComponent osc, Steamworks.CSteamID sender) { NetPlayer player = NetManager.Instance.GetPlayer(sender); // No one owns the object, accept ownership request. if (osc.Owner == null || osc.Owner == player) { osc.Owner = player; osc.OwnerSetToRemote(NetManager.Instance.GetPlayer(sender)); SendSyncResponse(player, msg.objectID, true); // Send updated ownership info to other clients. Network.Messages.ObjectSyncMessage msgBroadcast = new Network.Messages.ObjectSyncMessage(); msgBroadcast.objectID = msg.objectID; msgBroadcast.OwnerPlayerID = NetManager.Instance.GetPlayerIDBySteamID(sender); msgBroadcast.SyncType = (int)SyncTypes.SetOwner; NetManager.Instance.BroadcastMessage(msgBroadcast, Steamworks.EP2PSend.k_EP2PSendReliable); } // Someone else owns the object, deny ownership request. else { SendSyncResponse(player, msg.objectID, false); } }
/// <summary> /// Send object sync as the host. /// </summary> public void SendObjectSyncHost(int objectID, Vector3 pos, Quaternion rot, ObjectSyncManager.SyncTypes syncType, float[] syncedVariables, ObjectSyncManager.Flags flags) { Network.Messages.ObjectSyncMessage msg = new Network.Messages.ObjectSyncMessage(); msg.objectID = objectID; msg.SyncType = (int)syncType; if (syncedVariables != null) { msg.SyncedVariables = syncedVariables; } switch (flags) { case ObjectSyncManager.Flags.Full: msg.Position = Utils.GameVec3ToNet(pos); msg.Rotation = Utils.GameQuatToNet(rot); break; case ObjectSyncManager.Flags.RotationOnly: msg.Rotation = Utils.GameQuatToNet(rot); break; case ObjectSyncManager.Flags.PositionOnly: msg.Position = Utils.GameVec3ToNet(pos); break; } if (syncType != ObjectSyncManager.SyncTypes.PeriodicSync && syncType != ObjectSyncManager.SyncTypes.GenericSync) { Network.Messages.ObjectSyncMessage msgBroadcast = new Network.Messages.ObjectSyncMessage(); // Set owner as host. if (syncType == ObjectSyncManager.SyncTypes.SetOwner) { if (Owner == null) { Owner = NetManager.Instance.GetLocalPlayer(); SyncEnabled = true; msgBroadcast.SyncType = (int)ObjectSyncManager.SyncTypes.SetOwner; } } // Remove owner as host. else if (syncType == ObjectSyncManager.SyncTypes.RemoveOwner) { Owner = null; OwnerRemoved(); msgBroadcast.SyncType = (int)ObjectSyncManager.SyncTypes.RemoveOwner; } // Force take sync control as host. else if (syncType == ObjectSyncManager.SyncTypes.ForceSetOwner) { SyncEnabled = true; msgBroadcast.SyncType = (int)ObjectSyncManager.SyncTypes.ForceSetOwner; } // Send updated ownership info to other clients. msgBroadcast.objectID = msg.objectID; msgBroadcast.OwnerPlayerID = NetManager.Instance.GetPlayerIDBySteamID(Steamworks.SteamUser.GetSteamID()); NetManager.Instance.BroadcastMessage(msgBroadcast, Steamworks.EP2PSend.k_EP2PSendReliable); } else { NetManager.Instance.BroadcastMessage(msg, Steamworks.EP2PSend.k_EP2PSendReliable); } }