Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using BeardedManStudios.Forge.Networking.Generated;
  5. using UnityEngine.Events;
  6. using BeardedManStudios.Forge.Networking;
  7. using System;
  8. using BeardedManStudios.Forge.Networking.Unity;
  9.  
  10. /// <summary>
  11. /// The main network object script
  12. /// Only thing to take notice of is that all RPC's are setup with events, so componenets can subscribe and the code will be split out across components
  13. /// </summary>
  14. public class playerNetworking : goldBehavior
  15. {
  16.  
  17.     //A toggle event, see the merry fragmas 3.0 (https://unity3d.com/learn/tutorials/topics/multiplayer-networking/merry-fragmas-30-multiplayer-fps-foundation)
  18.     //for a proper explanation
  19.     [System.Serializable]
  20.     public class ToggleEvent : UnityEvent<bool> { }
  21.  
  22.     //make an event for each RPC, so other scripts can acces them
  23.     public event System.Action<RpcArgs> ShootEvent;
  24.     public event System.Action<RpcArgs> DieEvent;
  25.     public event System.Action<RpcArgs> SwitchWeaponEvent;
  26.     public event System.Action<RpcArgs> TakeDamageEvent;
  27.     public event System.Action<RpcArgs> SetupPlayerEvent;
  28.     //also make an event for the network start as some components need that as well
  29.     public event System.Action NetworkStartEvent;
  30.  
  31.     //The spine of the player's rig
  32.     [SerializeField]
  33.     private GameObject spine;
  34.  
  35.     //the player model
  36.     [SerializeField]
  37.     private GameObject playerModel;
  38.  
  39.     //Use the toggle event class
  40.     //See the inspector for this
  41.     [SerializeField]
  42.     ToggleEvent ownerScripts;
  43.  
  44.     //the player's HUD canvas
  45.     [SerializeField]
  46.     private GameObject HUD;
  47.  
  48.     //The player's camera
  49.     private Camera playerCamera;
  50.  
  51.     public GameObject PlayerModel
  52.     {
  53.         get
  54.         {
  55.             return playerModel;
  56.         }
  57.     }
  58.     public Camera PlayerCamera
  59.     {
  60.         get
  61.         {
  62.             return playerCamera;
  63.         }
  64.     }
  65.  
  66.     /// <summary>
  67.     /// Called when the network object is ready and initialized
  68.     /// </summary>
  69.     protected override void NetworkStart()
  70.     {
  71.         base.NetworkStart();
  72.  
  73.         //make a dynamic bool depending on we are the owner or not, and define the logic in the inspector.
  74.         //eg some scripts should be disabled on non owners
  75.         ownerScripts.Invoke(networkObject.IsOwner);
  76.  
  77.         //Get the player camera
  78.         playerCamera = GetComponentInChildren<Camera>();
  79.  
  80.  
  81.         //Disable the camera if we aren't the owner
  82.         if (!networkObject.IsOwner)
  83.         {
  84.             playerCamera.gameObject.SetActive(false);
  85.         }
  86.         else if (networkObject.IsOwner)
  87.         {
  88.             //Enable the HUD if we are the owner (it's disabled in the prefab)
  89.             HUD.SetActive(true);
  90.             //call the network start event
  91.             if (NetworkStartEvent != null)
  92.             {
  93.                 NetworkStartEvent();
  94.             }
  95.         }
  96.  
  97.         if (NetworkManager.Instance.Networker is IServer)
  98.         {
  99.             //here you can also do some server specific code
  100.         }
  101.         else
  102.         {
  103.             //setup the disconnected event
  104.             NetworkManager.Instance.Networker.disconnected += DisconnectedFromServer;
  105.  
  106.         }
  107.     }
  108.  
  109.     /// <summary>
  110.     /// Called when a player disconnects
  111.     /// </summary>
  112.     /// <param name="sender"></param>
  113.     private void DisconnectedFromServer(NetWorker sender)
  114.     {
  115.         NetworkManager.Instance.Networker.disconnected -= DisconnectedFromServer;
  116.  
  117.         MainThreadManager.Run(() =>
  118.         {
  119.             //Loop through the network objects to see if the disconnected player is the host
  120.             foreach (var no in sender.NetworkObjectList)
  121.             {
  122.                 if (no.Owner.IsHost)
  123.                 {
  124.                     BMSLogger.Instance.Log("Server disconnected");
  125.                     //Should probably make some kind of "You disconnected" screen. ah well
  126.                     UnityEngine.SceneManagement.SceneManager.LoadScene(0);
  127.                 }
  128.             }
  129.  
  130.             NetworkManager.Instance.Disconnect();
  131.         });
  132.     }
  133.  
  134.  
  135.  
  136.     void FixedUpdate()
  137.     {
  138.         if (networkObject.IsOwner)
  139.         {
  140.             networkObject.position = transform.position;
  141.             networkObject.rotation = playerModel.transform.rotation;
  142.         }
  143.         else
  144.         {
  145.             transform.position = networkObject.position;
  146.             playerModel.transform.rotation = networkObject.rotation;
  147.         }
  148.  
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement