Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using BeardedManStudios.Forge.Networking.Generated;
- using UnityEngine.Events;
- using BeardedManStudios.Forge.Networking;
- using System;
- using BeardedManStudios.Forge.Networking.Unity;
- /// <summary>
- /// The main network object script
- /// 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
- /// </summary>
- public class playerNetworking : goldBehavior
- {
- //A toggle event, see the merry fragmas 3.0 (https://unity3d.com/learn/tutorials/topics/multiplayer-networking/merry-fragmas-30-multiplayer-fps-foundation)
- //for a proper explanation
- [System.Serializable]
- public class ToggleEvent : UnityEvent<bool> { }
- //make an event for each RPC, so other scripts can acces them
- public event System.Action<RpcArgs> ShootEvent;
- public event System.Action<RpcArgs> DieEvent;
- public event System.Action<RpcArgs> SwitchWeaponEvent;
- public event System.Action<RpcArgs> TakeDamageEvent;
- public event System.Action<RpcArgs> SetupPlayerEvent;
- //also make an event for the network start as some components need that as well
- public event System.Action NetworkStartEvent;
- //The spine of the player's rig
- [SerializeField]
- private GameObject spine;
- //the player model
- [SerializeField]
- private GameObject playerModel;
- //Use the toggle event class
- //See the inspector for this
- [SerializeField]
- ToggleEvent ownerScripts;
- //the player's HUD canvas
- [SerializeField]
- private GameObject HUD;
- //The player's camera
- private Camera playerCamera;
- public GameObject PlayerModel
- {
- get
- {
- return playerModel;
- }
- }
- public Camera PlayerCamera
- {
- get
- {
- return playerCamera;
- }
- }
- /// <summary>
- /// Called when the network object is ready and initialized
- /// </summary>
- protected override void NetworkStart()
- {
- base.NetworkStart();
- //make a dynamic bool depending on we are the owner or not, and define the logic in the inspector.
- //eg some scripts should be disabled on non owners
- ownerScripts.Invoke(networkObject.IsOwner);
- //Get the player camera
- playerCamera = GetComponentInChildren<Camera>();
- //Disable the camera if we aren't the owner
- if (!networkObject.IsOwner)
- {
- playerCamera.gameObject.SetActive(false);
- }
- else if (networkObject.IsOwner)
- {
- //Enable the HUD if we are the owner (it's disabled in the prefab)
- HUD.SetActive(true);
- //call the network start event
- if (NetworkStartEvent != null)
- {
- NetworkStartEvent();
- }
- }
- if (NetworkManager.Instance.Networker is IServer)
- {
- //here you can also do some server specific code
- }
- else
- {
- //setup the disconnected event
- NetworkManager.Instance.Networker.disconnected += DisconnectedFromServer;
- }
- }
- /// <summary>
- /// Called when a player disconnects
- /// </summary>
- /// <param name="sender"></param>
- private void DisconnectedFromServer(NetWorker sender)
- {
- NetworkManager.Instance.Networker.disconnected -= DisconnectedFromServer;
- MainThreadManager.Run(() =>
- {
- //Loop through the network objects to see if the disconnected player is the host
- foreach (var no in sender.NetworkObjectList)
- {
- if (no.Owner.IsHost)
- {
- BMSLogger.Instance.Log("Server disconnected");
- //Should probably make some kind of "You disconnected" screen. ah well
- UnityEngine.SceneManagement.SceneManager.LoadScene(0);
- }
- }
- NetworkManager.Instance.Disconnect();
- });
- }
- void FixedUpdate()
- {
- if (networkObject.IsOwner)
- {
- networkObject.position = transform.position;
- networkObject.rotation = playerModel.transform.rotation;
- }
- else
- {
- transform.position = networkObject.position;
- playerModel.transform.rotation = networkObject.rotation;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement