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 BeardedManStudios.Forge.Networking.Unity;
- using BeardedManStudios.Forge.Networking;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- using EZCameraShake;
- public class PlayerController : PlayerBehavior
- {
- //Player stats
- [Header("Player Stats")]
- public int health = 100;
- public int respawnTime = 5;
- public string playerName = "";
- int fov = 90;
- float sens = 2.5f;
- //Weapon IDs
- [HideInInspector]
- public int primaryWeaponID = 0;
- [HideInInspector]
- public int secondaryWeaponID = 1;
- [HideInInspector]
- public int tertiaryWeaponID = 2;
- [Header("Transforms")]
- //Transform
- public Transform firePoint;
- public Transform dropPoint;
- public Transform weaponPivot;
- public Transform weaponParent;
- public Transform spectatorPivot;
- public Transform[] playerHitbox;
- [Header("UI")]
- //UI
- public Text healthText;
- public Text playerNameText;
- public Text killText;
- public Text onScreenText;
- public Text weaponAmmoText;
- public Text pingText;
- public Text playerCount;
- public Image reloadCircle;
- public Image crosshair;
- public GameObject playerScoreboard;
- public GameObject scoreBoard;
- GameObject playerScoreInstance;
- [Header("Gameobjects")]
- //GameObject
- public GameObject playerModels;
- public GameObject playerHats;
- public GameObject playerCamera;
- public GameObject playerCameraPivot;
- public GameObject hitMarkerSound;
- public GameObject headshotNoise;
- public GameObject[] hitGruntSounds;
- public GameObject dynamicCrosshair;
- //Uint
- [HideInInspector]
- public uint playerID;
- [HideInInspector]
- public uint enemyPlayer;
- [Header("Ints")]
- //Int
- public int currentSlot = 0;
- public int playerSkin = 0;
- public int playerHat = 0;
- [Header("Cameras")]
- //Camera
- public GameObject spectatorCam;
- [Header("Player Score")]
- public int killStreak = 0;
- public int playerKills;
- public int playerDeaths;
- public float spread;
- GameObject cac;
- bool networkReady = false;
- bool sceneLoaded = false;
- void Start()
- {
- scoreBoard = GameObject.Find("ScoreboardCanvas").transform.Find("Panel").gameObject;
- }
- protected override void NetworkStart()
- {
- base.NetworkStart();
- //NetworkObject.Flush(networkObject.Networker);
- networkReady = true;
- playerID = networkObject.NetworkId;
- if (!networkObject.IsOwner)
- return;
- localSyncPlayerStats();
- spectatorCam = GameObject.Find("KillCamera");
- cac = GameObject.Find("RespawnUI").GetComponent<RespawnUI>().gameObject;
- cac.GetComponent<RespawnUI>().player = gameObject;
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- var s = NetworkManager.Instance.InstantiatePlayerScoreboard(0);
- playerScoreInstance = s.gameObject;
- scoreBoard = GameObject.Find("ScoreboardCanvas").transform.Find("Panel").gameObject;
- updateScore();
- scoreBoard.SetActive(false);
- weaponParent.gameObject.AddComponent<CameraShaker>();
- weaponParent.GetComponent<CameraShaker>().DefaultPosInfluence = new Vector3(0, 0, 10);
- weaponParent.GetComponent<CameraShaker>().DefaultRotInfluence = new Vector3(0, 0, 0);
- if (GameObject.Find("Networking").transform.tag != "BR")
- networkObject.SendRpc(RPC_KILL_PLAYER, Receivers.AllBuffered);
- if (GameObject.Find("Networking").transform.tag == "BR")
- {
- playerCount.gameObject.SetActive(true);
- if (networkObject.IsServer)
- StartBRGame();
- else
- battleRoyaleSpawn();
- }
- currentSlot = 0;
- networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, 0);
- }
- void Update()
- {
- if (!networkObject.IsOwner || !networkReady)
- return;
- swapWeapon();
- checkDeath();
- hud();
- checkActiveWeapon();
- CheckForDroppedWeapon();
- networkObject.hp = health;
- if (Input.GetKey(KeyCode.Tab))
- scoreBoard.SetActive(true);
- else
- scoreBoard.SetActive(false);
- if (Input.GetKeyDown(KeyCode.P))
- {
- networkObject.SendRpc(RPC_SET_ENEMY, Receivers.AllBuffered, networkObject.NetworkId);
- healthUpdate(50);
- enemyPlayer = networkObject.NetworkId;
- }
- if (Input.GetKeyDown(KeyCode.Mouse1))
- ADS();
- else if (Input.GetKeyUp(KeyCode.Mouse1))
- unADS();
- }
- public void updateScore()
- {
- playerScoreInstance.GetComponent<PlayerScoreboard>().updateBoard(playerName, killStreak, playerKills, playerDeaths);
- }
- void CheckForDroppedWeapon()
- {
- RaycastHit hit;
- bool hoveringweapon = false;
- var fwd = firePoint.transform.forward;
- if (Physics.Raycast(firePoint.transform.position, fwd, out hit, 2.5f))
- {
- if (hit.transform.root == transform)
- return;
- if (hit.transform.tag == "Weapon")
- {
- //Debug.Log(hit.transform.name);
- hoveringweapon = true;
- }
- else
- hoveringweapon = false;
- if (hit.transform.tag == "Weapon" && Input.GetKeyDown(KeyCode.E))
- {
- networkObject.SendRpc(RPC_DESTROY_OBJECT, Receivers.AllBuffered, hit.transform.name);
- networkObject.SendRpc(RPC_DROP_WEAPON, Receivers.AllBuffered, currentSlot, (int)Random.Range(0, 99999), 25000, dropPoint.position);
- networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, hit.transform.GetComponent<WeaponStats>().weaponID, currentSlot);
- }
- }
- if (hoveringweapon)
- onScreenText.text = hit.transform.GetComponent<WeaponStats>().weaponName;
- else
- onScreenText.text = "";
- }
- public void localSyncPlayerStats()
- {
- var s = GameObject.Find("PlayerStats");
- playerName = s.GetComponent<PlayerStats>().playerName;
- if (playerName == "")
- playerName = "Player " + Random.Range(0, 999);
- playerSkin = s.GetComponent<PlayerStats>().playerSkin;
- playerHat = s.GetComponent<PlayerStats>().playerHat;
- networkObject.SendRpc(RPC_SYNC_STATS, Receivers.AllBuffered, playerName, playerSkin, playerHat);
- }
- public override void syncStats(RpcArgs args)
- {
- if (!networkReady)
- return;
- //References
- string p = args.GetNext<string>();
- int skinID = args.GetNext<int>();
- int hatID = args.GetNext<int>();
- //Disable old skin and set new one
- if (playerModels)
- {
- for (int i = 0; i < playerModels.transform.childCount; i++)
- {
- playerModels.transform.GetChild(i).gameObject.SetActive(false);
- }
- playerModels.transform.GetChild(skinID).gameObject.SetActive(true);
- }
- if (playerHats)
- {
- //Disable old hat and set new one
- for (int i = 0; i < playerHats.transform.childCount; i++)
- {
- playerHats.transform.GetChild(i).gameObject.SetActive(false);
- }
- playerHats.transform.GetChild(hatID).gameObject.SetActive(true);
- }
- //Update player name
- playerName = p;
- playerNameText.text = p;
- //Turn on player hitbox
- GetComponent<CharacterController>().detectCollisions = false;
- }
- //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- //Weapons
- void randomWeapon()
- {
- //Spawn in player weapons
- //0-11 Primary
- //12-18 Secondary
- primaryWeaponID = Random.Range(0, 12);
- secondaryWeaponID = Random.Range(12, 19);
- tertiaryWeaponID = 19;
- networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, primaryWeaponID, 3);
- networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, secondaryWeaponID, 3);
- networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, tertiaryWeaponID, 3);
- networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, 0);
- }
- void checkActiveWeapon()
- {
- if (weaponParent.childCount < 3)
- return;
- if (weaponParent.GetChild(0).gameObject.activeInHierarchy == false)
- if (weaponParent.GetChild(1).gameObject.activeInHierarchy == false)
- if (weaponParent.GetChild(2).gameObject.activeInHierarchy == false)
- weaponParent.GetChild(currentSlot).gameObject.SetActive(true);
- }
- public void setWeapon()
- {
- networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, primaryWeaponID, 3);
- networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, secondaryWeaponID, 3);
- networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, tertiaryWeaponID, 3);
- networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, 0);
- }
- void swapWeapon()
- {
- if(Input.GetKeyDown(KeyCode.L))
- {
- networkObject.SendRpc(RPC_DROP_WEAPON, Receivers.AllBuffered, currentSlot, (int)Random.Range(0, 99999), 25000, dropPoint.position);
- networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, 16, currentSlot);
- }
- if (Input.GetKeyDown(KeyCode.G))
- {
- networkObject.SendRpc(RPC_DROP_WEAPON, Receivers.AllBuffered, currentSlot, (int)Random.Range(0, 99999), 25000, dropPoint.position);
- networkObject.SendRpc(RPC_SPAWN_WEAPON, Receivers.AllBuffered, 19, currentSlot);
- reloadCircle.gameObject.SetActive(false);
- }
- if (Input.GetKeyDown(KeyCode.Alpha1))
- {
- unADS();
- currentSlot = 0;
- networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, 0);
- reloadCircle.gameObject.SetActive(false);
- }
- if (Input.GetKeyDown(KeyCode.Alpha2))
- {
- unADS();
- currentSlot = 1;
- networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, 1);
- reloadCircle.gameObject.SetActive(false);
- }
- if (Input.GetKeyDown(KeyCode.Alpha3))
- {
- unADS();
- currentSlot = 2;
- networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, 2);
- reloadCircle.gameObject.SetActive(false);
- }
- }
- void ADS()
- {
- dynamicCrosshair.gameObject.SetActive(false);
- playerCamera.GetComponent<Camera>().fieldOfView = 60;
- playerCamera.GetComponent<SimpleSmoothMouseLook>().sensitivityX = sens / 1.5F;
- playerCamera.GetComponent<SimpleSmoothMouseLook>().sensitivityY = sens / 1.5F;
- }
- void unADS()
- {
- dynamicCrosshair.gameObject.SetActive(true);
- playerCamera.GetComponent<Camera>().fieldOfView = fov;
- playerCamera.GetComponent<SimpleSmoothMouseLook>().sensitivityX = sens;
- playerCamera.GetComponent<SimpleSmoothMouseLook>().sensitivityY = sens;
- }
- public override void dropWeapon(RpcArgs args)
- {
- int i = args.GetNext<int>();
- int n = args.GetNext<int>();
- int s = args.GetNext<int>();
- Vector3 pos = args.GetNext<Vector3>();
- if (!weaponParent.GetChild(i))
- return;
- weaponParent.GetChild(i).tag = "Weapon";
- weaponParent.GetChild(i).name += " " + n;
- weaponParent.GetChild(i).GetComponent<WeaponStats>().Drop(pos, s);
- }
- public void localSyncWeapon(int w)
- {
- networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, w);
- }
- public override void syncWeapon(RpcArgs args)
- {
- int w = args.GetNext<int>();
- currentSlot = w;
- for (int i = 0; i < weaponParent.childCount; i++)
- {
- weaponParent.GetChild(i).gameObject.SetActive(false);
- }
- if(weaponParent.GetChild(w))
- weaponParent.GetChild(w).gameObject.SetActive(true);
- }
- public override void spawnWeapon(RpcArgs args)
- {
- int w = args.GetNext<int>();
- int c = args.GetNext<int>();
- var gm = GameObject.Find("GameManager").gameObject;
- var g = Instantiate(gm.GetComponent<WeaponReferenses>().weapons[w], weaponPivot.transform.position, weaponPivot.transform.rotation, weaponParent);
- g.GetComponent<WeaponStats>().setSlot(c);
- g.SetActive(false);
- }
- //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- //HUD
- void hud()
- {
- healthText.text = "" + health;
- }
- //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- //Health
- public void healthUpdate(int dmg)
- {
- networkObject.SendRpc(RPC_UPDATE_HEALTH, Receivers.AllBuffered, -dmg);
- }
- public override void updateHealth(RpcArgs args)
- {
- int hp = args.GetNext<int>();
- if (networkObject.IsOwner)
- {
- if (-hp > 0)
- {
- var s =
- Instantiate(transform.root.GetComponent<PlayerController>().hitGruntSounds[Random.Range(0, hitGruntSounds.Length)], transform.position, transform.rotation);
- Destroy(s, 2);
- }
- }
- health += hp;
- networkObject.hp = health;
- }
- public override void setHealth(RpcArgs args)
- {
- int hp = args.GetNext<int>();
- health = hp;
- networkObject.hp = hp;
- }
- //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- //Respawn
- public void destroyPlayer()
- {
- sceneLoaded = false;
- //networkObject.Destroy();
- }
- public override void respawnPlayer(RpcArgs args)
- {
- currentSlot = 3;
- gameObject.SetActive(true);
- currentSlot = 0;
- networkObject.SendRpc(RPC_SYNC_WEAPON, Receivers.AllBuffered, 0);
- }
- public void respawnPlayerLocal()
- {
- reloadCircle.gameObject.SetActive(false);
- networkObject.SendRpc(RPC_SET_HEALTH, Receivers.AllBuffered, 100);
- networkObject.SendRpc(RPC_RESPAWN_PLAYER, Receivers.AllBuffered);
- }
- void checkDeath()
- {
- if (health <= 0)
- {
- Vector3 tempPos = dropPoint.position;
- for (int i = 0; i < 3; i++)
- {
- networkObject.SendRpc(RPC_DROP_WEAPON, Receivers.AllBuffered, 0, (int)Random.Range(0, 99999), 0, tempPos);
- }
- health = 100;
- killStreak = 0;
- GameObject.Find("Zone(Clone)").GetComponent<Zone>().CancelInvoke("dealDamage");
- if(enemyPlayer != 0)
- FindByid(enemyPlayer).GetComponent<PlayerController>().networkObject.SendRpc(RPC_KILLED_PLAYER_TEXT, Receivers.Owner, playerName);
- networkObject.SendRpc(RPC_SET_HEALTH, Receivers.AllBuffered, 100);
- networkObject.SendRpc(RPC_KILL_PLAYER, Receivers.AllBuffered);
- playerDeaths++;
- updateScore();
- cac.SetActive(true);
- }
- }
- public override void killPlayer(RpcArgs args)
- {
- for (int i = 0; i < weaponParent.childCount; i++)
- {
- Destroy(weaponParent.GetChild(i).gameObject);
- }
- gameObject.SetActive(false);
- }
- //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- //Misc
- public GameObject FindByid(uint targetNetworkId)
- {
- NetworkBehavior networkBehavior = (NetworkBehavior)NetworkManager.Instance.Networker.NetworkObjects[(uint)targetNetworkId].AttachedBehavior;
- GameObject obj = networkBehavior.gameObject;
- return obj;
- }
- public void localSetEnemy(GameObject p)
- {
- p.GetComponent<PlayerController>().networkObject.SendRpc(RPC_SET_ENEMY, Receivers.Owner, networkObject.NetworkId);
- }
- public override void setEnemy(RpcArgs args)
- {
- uint id = args.GetNext<uint>();
- enemyPlayer = id;
- if(id != 0)
- spectatorCam.GetComponent<KillCamera>().target = FindByid(id).GetComponent<PlayerController>().spectatorPivot.gameObject;
- //Debug.Log("Hit by - " + FindByid(id).GetComponent<PlayerController>().playerName);
- }
- public override void killedPlayerText(RpcArgs args)
- {
- string pName = args.GetNext<string>();
- playerKilled(pName);
- }
- public void playerKilled(string p)
- {
- if (p != playerName)
- {
- killStreak++;
- playerKills++;
- updateScore();
- var n = Instantiate(killText, new Vector3(0, -300, 0), Quaternion.Euler(0, 0, 0), transform.Find("HUD"));
- n.GetComponent<Text>().text = "You killed " + p + "\n Killstreak: " + killStreak;
- //Debug.Log("You killed " + p + "\n Killstreak: " + killStreak);
- n.GetComponent<RectTransform>().localPosition = new Vector3(0, -(Screen.height / 3), 0);
- Destroy(n.gameObject, 4);
- }
- }
- public override void destroyObject(RpcArgs args)
- {
- string s = args.GetNext<string>();
- Destroy(GameObject.Find(s));
- }
- public void StartBRGame()
- {
- if (GameObject.Find("Networking").transform.tag == "BR")
- {
- battleRoyaleSpawn();
- if (networkObject.IsServer)
- {
- ((IServer)NetworkManager.Instance.Networker).StopAcceptingConnections();
- for (int i = 0; i < 50; i++)
- {
- GameObject.Find("GameManager").GetComponent<WeaponSpawner>().
- SpawnWeapons(Random.Range(0, 19), new Vector3(Random.Range(-50, 100), 5, Random.Range(-100, 40)));
- }
- }
- }
- NetworkManager.Instance.InstantiateZone
- (0, new Vector3(Random.Range(-45, 80), 0, Random.Range(-90, 30)), Quaternion.identity);
- }
- public void battleRoyaleSpawn()
- {
- var s = GameObject.Find("PlayerStats");
- //Set player weapons
- primaryWeaponID = 19;
- secondaryWeaponID = 19;
- tertiaryWeaponID = 19;
- setWeapon();
- //Respawn player and sync stats
- localSyncPlayerStats();
- GetComponent<WASD>().enabled = true;
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- GameObject.Find("RespawnUI").SetActive(false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement