SHARE
TWEET

Untitled

a guest Mar 26th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using BeardedManStudios.Forge.Networking.Generated;
  5. using BeardedManStudios.Forge.Networking.Unity;
  6. using BeardedManStudios.Forge.Networking;
  7. using UnityEngine.UI;
  8. using UnityEngine.SceneManagement;
  9. using EZCameraShake;
  10.  
  11. public class PlayerController : PlayerBehavior
  12. {
  13.     //Player stats
  14.     [Header("Player Stats")]
  15.     public int health = 100;
  16.     public int respawnTime = 5;
  17.     public string playerName = "";
  18.  
  19.     int fov = 90;
  20.     float sens = 2.5f;
  21.  
  22.     //Weapon IDs
  23.     [HideInInspector]
  24.     public int primaryWeaponID = 0;
  25.     [HideInInspector]
  26.     public int secondaryWeaponID = 1;
  27.     [HideInInspector]
  28.     public int tertiaryWeaponID = 2;
  29.  
  30.     [Header("Transforms")]
  31.     //Transform
  32.     public Transform firePoint;
  33.     public Transform dropPoint;
  34.     public Transform weaponPivot;
  35.     public Transform weaponParent;
  36.     public Transform spectatorPivot;
  37.     public Transform[] playerHitbox;
  38.  
  39.     [Header("UI")]
  40.     //UI
  41.     public Text healthText;
  42.     public Text playerNameText;
  43.     public Text killText;
  44.     public Text onScreenText;
  45.     public Text weaponAmmoText;
  46.     public Text pingText;
  47.     public Text playerCount;
  48.     public Image reloadCircle;
  49.     public Image crosshair;
  50.     public GameObject playerScoreboard;
  51.     public GameObject scoreBoard;
  52.     GameObject playerScoreInstance;
  53.  
  54.     [Header("Gameobjects")]
  55.     //GameObject
  56.     public GameObject playerModels;
  57.     public GameObject playerHats;
  58.     public GameObject playerCamera;
  59.     public GameObject playerCameraPivot;
  60.     public GameObject hitMarkerSound;
  61.     public GameObject headshotNoise;
  62.     public GameObject[] hitGruntSounds;
  63.     public GameObject dynamicCrosshair;
  64.  
  65.     //Uint
  66.     [HideInInspector]
  67.     public uint playerID;
  68.     [HideInInspector]
  69.     public uint enemyPlayer;
  70.  
  71.     [Header("Ints")]
  72.     //Int
  73.     public int currentSlot = 0;
  74.     public int playerSkin = 0;
  75.     public int playerHat = 0;
  76.    
  77.  
  78.     [Header("Cameras")]
  79.     //Camera
  80.     public GameObject spectatorCam;
  81.  
  82.     [Header("Player Score")]
  83.     public int killStreak = 0;
  84.     public int playerKills;
  85.     public int playerDeaths;
  86.  
  87.     public float spread;
  88.  
  89.     GameObject cac;
  90.  
  91.     bool networkReady = false;
  92.     bool sceneLoaded = false;
  93.  
  94.  
  95.     void Start()
  96.     {
  97.         scoreBoard = GameObject.Find("ScoreboardCanvas").transform.Find("Panel").gameObject;
  98.     }
  99.  
  100.     protected override void NetworkStart()
  101.     {
  102.         base.NetworkStart();
  103.  
  104.         //NetworkObject.Flush(networkObject.Networker);
  105.  
  106.         networkReady = true;
  107.         playerID = networkObject.NetworkId;
  108.  
  109.         if (!networkObject.IsOwner)
  110.             return;
  111.        
  112.         localSyncPlayerStats();
  113.         spectatorCam = GameObject.Find("KillCamera");
  114.  
  115.         cac = GameObject.Find("RespawnUI").GetComponent<RespawnUI>().gameObject;
  116.         cac.GetComponent<RespawnUI>().player = gameObject;
  117.  
  118.         Cursor.lockState = CursorLockMode.None;
  119.         Cursor.visible = true;
  120.  
  121.         var s = NetworkManager.Instance.InstantiatePlayerScoreboard(0);
  122.         playerScoreInstance = s.gameObject;
  123.         scoreBoard = GameObject.Find("ScoreboardCanvas").transform.Find("Panel").gameObject;
  124.         updateScore();
  125.         scoreBoard.SetActive(false);
  126.  
  127.         weaponParent.gameObject.AddComponent<CameraShaker>();
  128.         weaponParent.GetComponent<CameraShaker>().DefaultPosInfluence = new Vector3(0, 0, 10);
  129.         weaponParent.GetComponent<CameraShaker>().DefaultRotInfluence = new Vector3(0, 0, 0);
  130.  
  131.         if (GameObject.Find("Networking").transform.tag != "BR")
  132.             networkObject.SendRpc(RPC_KILL_PLAYER, Receivers.AllBuffered);
  133.  
  134.         if (GameObject.Find("Networking").transform.tag == "BR")
  135.         {
  136.             playerCount.gameObject.SetActive(true);
  137.             if (networkObject.IsServer)
  138.                 StartBRGame();
  139.             else
  140.                 battleRoyaleSpawn();
  141.         }
  142.  
  143.         currentSlot = 0;
  144.         networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, 0);
  145.     }
  146.  
  147.     void Update()
  148.     {
  149.         if (!networkObject.IsOwner || !networkReady)
  150.             return;
  151.         swapWeapon();
  152.         checkDeath();
  153.         hud();
  154.         checkActiveWeapon();
  155.         CheckForDroppedWeapon();
  156.  
  157.         networkObject.hp = health;
  158.  
  159.         if (Input.GetKey(KeyCode.Tab))
  160.             scoreBoard.SetActive(true);
  161.         else
  162.             scoreBoard.SetActive(false);
  163.  
  164.         if (Input.GetKeyDown(KeyCode.P))
  165.         {
  166.             networkObject.SendRpc(RPC_SET_ENEMY, Receivers.AllBuffered, networkObject.NetworkId);
  167.             healthUpdate(50);
  168.             enemyPlayer = networkObject.NetworkId;
  169.         }
  170.  
  171.         if (Input.GetKeyDown(KeyCode.Mouse1))
  172.             ADS();
  173.         else if (Input.GetKeyUp(KeyCode.Mouse1))
  174.             unADS();
  175.     }
  176.  
  177.     public void updateScore()
  178.     {
  179.         playerScoreInstance.GetComponent<PlayerScoreboard>().updateBoard(playerName, killStreak, playerKills, playerDeaths);
  180.     }
  181.  
  182.     void CheckForDroppedWeapon()
  183.     {
  184.         RaycastHit hit;
  185.         bool hoveringweapon = false;
  186.         var fwd = firePoint.transform.forward;
  187.         if (Physics.Raycast(firePoint.transform.position, fwd, out hit, 2.5f))
  188.         {
  189.             if (hit.transform.root == transform)
  190.                 return;
  191.  
  192.             if (hit.transform.tag == "Weapon")
  193.             {
  194.                 //Debug.Log(hit.transform.name);
  195.                 hoveringweapon = true;
  196.             }
  197.             else
  198.                 hoveringweapon = false;
  199.  
  200.             if (hit.transform.tag == "Weapon" && Input.GetKeyDown(KeyCode.E))
  201.             {
  202.                 networkObject.SendRpc(RPC_DESTROY_OBJECT, Receivers.AllBuffered, hit.transform.name);
  203.                 networkObject.SendRpc(RPC_DROP_WEAPON, Receivers.AllBuffered, currentSlot, (int)Random.Range(0, 99999), 25000, dropPoint.position);
  204.                 networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, hit.transform.GetComponent<WeaponStats>().weaponID, currentSlot);
  205.             }
  206.         }
  207.  
  208.         if (hoveringweapon)
  209.             onScreenText.text = hit.transform.GetComponent<WeaponStats>().weaponName;
  210.         else
  211.             onScreenText.text = "";
  212.  
  213.     }
  214.     public void localSyncPlayerStats()
  215.     {
  216.         var s = GameObject.Find("PlayerStats");
  217.         playerName = s.GetComponent<PlayerStats>().playerName;
  218.  
  219.         if (playerName == "")
  220.             playerName = "Player " + Random.Range(0, 999);
  221.  
  222.         playerSkin = s.GetComponent<PlayerStats>().playerSkin;
  223.         playerHat = s.GetComponent<PlayerStats>().playerHat;
  224.  
  225.         networkObject.SendRpc(RPC_SYNC_STATS, Receivers.AllBuffered, playerName, playerSkin, playerHat);
  226.     }
  227.  
  228.     public override void syncStats(RpcArgs args)
  229.     {
  230.         if (!networkReady)
  231.             return;
  232.  
  233.         //References
  234.         string p = args.GetNext<string>();
  235.         int skinID = args.GetNext<int>();
  236.         int hatID = args.GetNext<int>();
  237.  
  238.         //Disable old skin and set new one
  239.         if (playerModels)
  240.         {
  241.             for (int i = 0; i < playerModels.transform.childCount; i++)
  242.             {
  243.                 playerModels.transform.GetChild(i).gameObject.SetActive(false);
  244.             }
  245.             playerModels.transform.GetChild(skinID).gameObject.SetActive(true);
  246.         }
  247.         if (playerHats)
  248.         {
  249.             //Disable old hat and set new one
  250.             for (int i = 0; i < playerHats.transform.childCount; i++)
  251.             {
  252.                 playerHats.transform.GetChild(i).gameObject.SetActive(false);
  253.             }
  254.             playerHats.transform.GetChild(hatID).gameObject.SetActive(true);
  255.         }
  256.  
  257.         //Update player name
  258.         playerName = p;
  259.         playerNameText.text = p;
  260.  
  261.         //Turn on player hitbox
  262.         GetComponent<CharacterController>().detectCollisions = false;
  263.     }
  264.  
  265.     //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  266.     //Weapons
  267.  
  268.     void randomWeapon()
  269.     {
  270.         //Spawn in player weapons
  271.         //0-11 Primary
  272.         //12-18 Secondary
  273.  
  274.         primaryWeaponID = Random.Range(0, 12);
  275.         secondaryWeaponID = Random.Range(12, 19);
  276.         tertiaryWeaponID = 19;
  277.  
  278.         networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, primaryWeaponID, 3);
  279.         networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, secondaryWeaponID, 3);
  280.         networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, tertiaryWeaponID, 3);
  281.  
  282.         networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, 0);
  283.  
  284.     }
  285.  
  286.     void checkActiveWeapon()
  287.     {
  288.         if (weaponParent.childCount < 3)
  289.             return;
  290.  
  291.         if (weaponParent.GetChild(0).gameObject.activeInHierarchy == false)
  292.             if (weaponParent.GetChild(1).gameObject.activeInHierarchy == false)
  293.                 if (weaponParent.GetChild(2).gameObject.activeInHierarchy == false)
  294.                     weaponParent.GetChild(currentSlot).gameObject.SetActive(true);
  295.     }
  296.  
  297.     public void setWeapon()
  298.     {
  299.         networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, primaryWeaponID, 3);
  300.         networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, secondaryWeaponID, 3);
  301.         networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, tertiaryWeaponID, 3);
  302.  
  303.         networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, 0);
  304.     }
  305.  
  306.     void swapWeapon()
  307.     {
  308.        
  309.         if(Input.GetKeyDown(KeyCode.L))
  310.         {
  311.             networkObject.SendRpc(RPC_DROP_WEAPON, Receivers.AllBuffered, currentSlot, (int)Random.Range(0, 99999), 25000, dropPoint.position);  
  312.             networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, 16, currentSlot);
  313.         }
  314.        
  315.  
  316.         if (Input.GetKeyDown(KeyCode.G))
  317.         {
  318.             networkObject.SendRpc(RPC_DROP_WEAPON, Receivers.AllBuffered, currentSlot, (int)Random.Range(0, 99999), 25000, dropPoint.position);
  319.             networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, 19, currentSlot);
  320.             reloadCircle.gameObject.SetActive(false);
  321.         }
  322.         if (Input.GetKeyDown(KeyCode.Alpha1))
  323.         {
  324.             unADS();
  325.             currentSlot = 0;
  326.             networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, 0);
  327.             reloadCircle.gameObject.SetActive(false);
  328.         }
  329.         if (Input.GetKeyDown(KeyCode.Alpha2))
  330.         {
  331.             unADS();
  332.             currentSlot = 1;
  333.             networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, 1);
  334.             reloadCircle.gameObject.SetActive(false);
  335.         }
  336.         if (Input.GetKeyDown(KeyCode.Alpha3))
  337.         {
  338.             unADS();
  339.             currentSlot = 2;
  340.             networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, 2);
  341.             reloadCircle.gameObject.SetActive(false);
  342.         }
  343.  
  344.     }
  345.     void ADS()
  346.     {
  347.         dynamicCrosshair.gameObject.SetActive(false);
  348.         playerCamera.GetComponent<Camera>().fieldOfView = 60;
  349.         playerCamera.GetComponent<SimpleSmoothMouseLook>().sensitivityX = sens / 1.5F;
  350.         playerCamera.GetComponent<SimpleSmoothMouseLook>().sensitivityY = sens / 1.5F;
  351.     }
  352.     void unADS()
  353.     {
  354.         dynamicCrosshair.gameObject.SetActive(true);
  355.         playerCamera.GetComponent<Camera>().fieldOfView = fov;
  356.         playerCamera.GetComponent<SimpleSmoothMouseLook>().sensitivityX = sens;
  357.         playerCamera.GetComponent<SimpleSmoothMouseLook>().sensitivityY = sens;
  358.     }
  359.     public override void dropWeapon(RpcArgs args)
  360.     {
  361.         int i = args.GetNext<int>();
  362.         int n = args.GetNext<int>();
  363.         int s = args.GetNext<int>();
  364.  
  365.         Vector3 pos = args.GetNext<Vector3>();
  366.         if (!weaponParent.GetChild(i))
  367.             return;
  368.         weaponParent.GetChild(i).tag = "Weapon";
  369.         weaponParent.GetChild(i).name += " " + n;
  370.         weaponParent.GetChild(i).GetComponent<WeaponStats>().Drop(pos, s);
  371.     }
  372.     public void localSyncWeapon(int w)
  373.     {
  374.         networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, w);
  375.     }
  376.     public override void syncWeapon(RpcArgs args)
  377.     {
  378.         int w = args.GetNext<int>();
  379.         currentSlot = w;
  380.         for (int i = 0; i < weaponParent.childCount; i++)
  381.         {
  382.             weaponParent.GetChild(i).gameObject.SetActive(false);
  383.         }
  384.  
  385.         if(weaponParent.GetChild(w))
  386.             weaponParent.GetChild(w).gameObject.SetActive(true);
  387.     }
  388.  
  389.     public override void spawnWeapon(RpcArgs args)
  390.     {
  391.         int w = args.GetNext<int>();
  392.         int c = args.GetNext<int>();
  393.         var gm = GameObject.Find("GameManager").gameObject;
  394.         var g = Instantiate(gm.GetComponent<WeaponReferenses>().weapons[w], weaponPivot.transform.position, weaponPivot.transform.rotation, weaponParent);
  395.         g.GetComponent<WeaponStats>().setSlot(c);
  396.         g.SetActive(false);
  397.     }
  398.  
  399.     //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  400.     //HUD
  401.  
  402.     void hud()
  403.     {
  404.         healthText.text = "" + health;
  405.     }
  406.  
  407.     //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  408.     //Health
  409.  
  410.     public void healthUpdate(int dmg)
  411.     {
  412.         networkObject.SendRpc(RPC_UPDATE_HEALTH, Receivers.AllBuffered, -dmg);
  413.     }
  414.  
  415.     public override void updateHealth(RpcArgs args)
  416.     {
  417.         int hp = args.GetNext<int>();
  418.         if (networkObject.IsOwner)
  419.         {
  420.             if (-hp > 0)
  421.             {
  422.                 var s =
  423.                  Instantiate(transform.root.GetComponent<PlayerController>().hitGruntSounds[Random.Range(0, hitGruntSounds.Length)], transform.position, transform.rotation);
  424.                 Destroy(s, 2);
  425.             }
  426.         }
  427.         health += hp;
  428.         networkObject.hp = health;
  429.     }
  430.  
  431.     public override void setHealth(RpcArgs args)
  432.     {
  433.         int hp = args.GetNext<int>();
  434.         health = hp;
  435.         networkObject.hp = hp;
  436.     }
  437.  
  438.     //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  439.     //Respawn
  440.  
  441.     public void destroyPlayer()
  442.     {
  443.         sceneLoaded = false;
  444.         //networkObject.Destroy();
  445.     }
  446.     public override void respawnPlayer(RpcArgs args)
  447.     {
  448.         currentSlot = 3;
  449.         gameObject.SetActive(true);
  450.         currentSlot = 0;
  451.         networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, 0);
  452.     }
  453.  
  454.     public void respawnPlayerLocal()
  455.     {
  456.         reloadCircle.gameObject.SetActive(false);
  457.         networkObject.SendRpc(RPC_SET_HEALTH, Receivers.AllBuffered, 100);
  458.         networkObject.SendRpc(RPC_RESPAWN_PLAYER, Receivers.AllBuffered);
  459.     }
  460.  
  461.     void checkDeath()
  462.     {
  463.         if (health <= 0)
  464.         {
  465.             Vector3 tempPos = dropPoint.position;
  466.             for (int i = 0; i < 3; i++)
  467.             {
  468.                 networkObject.SendRpc(RPC_DROP_WEAPON, Receivers.AllBuffered, 0, (int)Random.Range(0, 99999), 0, tempPos);
  469.             }
  470.            
  471.             health = 100;
  472.             killStreak = 0;
  473.             GameObject.Find("Zone(Clone)").GetComponent<Zone>().CancelInvoke("dealDamage");
  474.  
  475.             if(enemyPlayer != 0)
  476.                 FindByid(enemyPlayer).GetComponent<PlayerController>().networkObject.SendRpc(RPC_KILLED_PLAYER_TEXT, Receivers.Owner, playerName);
  477.  
  478.             networkObject.SendRpc(RPC_SET_HEALTH, Receivers.AllBuffered, 100);    
  479.             networkObject.SendRpc(RPC_KILL_PLAYER, Receivers.AllBuffered);
  480.  
  481.             playerDeaths++;
  482.             updateScore();
  483.             cac.SetActive(true);
  484.         }
  485.     }
  486.     public override void killPlayer(RpcArgs args)
  487.     {
  488.         for (int i = 0; i < weaponParent.childCount; i++)
  489.         {
  490.             Destroy(weaponParent.GetChild(i).gameObject);
  491.         }
  492.         gameObject.SetActive(false);
  493.  
  494.     }
  495.  
  496.     //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  497.     //Misc
  498.  
  499.     public GameObject FindByid(uint targetNetworkId)
  500.     {
  501.         NetworkBehavior networkBehavior = (NetworkBehavior)NetworkManager.Instance.Networker.NetworkObjects[(uint)targetNetworkId].AttachedBehavior;
  502.         GameObject obj = networkBehavior.gameObject;
  503.         return obj;
  504.     }
  505.  
  506.     public void localSetEnemy(GameObject p)
  507.     {
  508.         p.GetComponent<PlayerController>().networkObject.SendRpc(RPC_SET_ENEMY, Receivers.Owner, networkObject.NetworkId);
  509.     }
  510.     public override void setEnemy(RpcArgs args)
  511.     {
  512.         uint id = args.GetNext<uint>();      
  513.         enemyPlayer = id;
  514.         if(id != 0)
  515.             spectatorCam.GetComponent<KillCamera>().target = FindByid(id).GetComponent<PlayerController>().spectatorPivot.gameObject;
  516.         //Debug.Log("Hit by - " + FindByid(id).GetComponent<PlayerController>().playerName);
  517.     }
  518.  
  519.     public override void killedPlayerText(RpcArgs args)
  520.     {
  521.         string pName = args.GetNext<string>();
  522.         playerKilled(pName);
  523.     }
  524.     public void playerKilled(string p)
  525.     {
  526.         if (p != playerName)
  527.         {
  528.             killStreak++;
  529.             playerKills++;
  530.             updateScore();
  531.             var n = Instantiate(killText, new Vector3(0, -300, 0), Quaternion.Euler(0, 0, 0), transform.Find("HUD"));
  532.             n.GetComponent<Text>().text = "You killed " + p + "\n Killstreak: " + killStreak;
  533.             //Debug.Log("You killed " + p + "\n Killstreak: " + killStreak);
  534.             n.GetComponent<RectTransform>().localPosition = new Vector3(0, -(Screen.height / 3), 0);
  535.             Destroy(n.gameObject, 4);
  536.         }
  537.     }
  538.  
  539.     public override void destroyObject(RpcArgs args)
  540.     {
  541.         string s = args.GetNext<string>();
  542.  
  543.         Destroy(GameObject.Find(s));
  544.     }
  545.  
  546.     public void StartBRGame()
  547.     {
  548.         if (GameObject.Find("Networking").transform.tag == "BR")
  549.         {
  550.             battleRoyaleSpawn();
  551.             if (networkObject.IsServer)
  552.             {
  553.                 ((IServer)NetworkManager.Instance.Networker).StopAcceptingConnections();
  554.                 for (int i = 0; i < 50; i++)
  555.                 {
  556.                     GameObject.Find("GameManager").GetComponent<WeaponSpawner>().
  557.                     SpawnWeapons(Random.Range(0, 19), new Vector3(Random.Range(-50, 100), 5, Random.Range(-100, 40)));
  558.                 }
  559.             }
  560.         }
  561.         NetworkManager.Instance.InstantiateZone
  562.             (0, new Vector3(Random.Range(-45, 80), 0, Random.Range(-90, 30)), Quaternion.identity);
  563.     }
  564.     public void battleRoyaleSpawn()
  565.     {
  566.         var s = GameObject.Find("PlayerStats");
  567.  
  568.         //Set player weapons
  569.         primaryWeaponID = 19;
  570.         secondaryWeaponID = 19;
  571.         tertiaryWeaponID = 19;
  572.         setWeapon();
  573.  
  574.  
  575.         //Respawn player and sync stats
  576.         localSyncPlayerStats();
  577.         GetComponent<WASD>().enabled = true;
  578.  
  579.         Cursor.lockState = CursorLockMode.Locked;
  580.         Cursor.visible = false;
  581.  
  582.         GameObject.Find("RespawnUI").SetActive(false);
  583.     }
  584. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top