Guest User

Untitled

a guest
Jan 16th, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.71 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Photon.Pun;
  5. using Hashtable = ExitGames.Client.Photon.Hashtable;
  6. using Photon.Realtime;
  7. using UnityEngine.UI;
  8.  
  9. public class PlayerController : MonoBehaviourPunCallbacks, IDamagable
  10. {
  11.     [SerializeField] GameObject cameraHolder;
  12.     [SerializeField] float mouseSensitivity, sprintSpeed, walkSpeed, jumpForce, smoothTime;
  13.     [SerializeField] Image healthBar;
  14.     [SerializeField] GameObject ui;
  15.     [SerializeField] GameObject brains;
  16.  
  17.     float verticalLookRotation;
  18.     bool grounded;
  19.  
  20.     Vector3 smoothMoveVelocity;
  21.     Vector3 moveAmount;
  22.  
  23.     [SerializeField] Item[] items;
  24.  
  25.     int itemIndex;
  26.     int previousItemIndex = -1;
  27.  
  28.     Rigidbody rb;
  29.  
  30.     PhotonView PV;
  31.  
  32.     const float maxHealth = 100f;
  33.     float currentHealth = maxHealth;
  34.  
  35.     PlayerManager playerManager;
  36.  
  37.     void Awake()
  38.     {
  39.         rb = GetComponent<Rigidbody>();
  40.         PV = GetComponent<PhotonView>();
  41.  
  42.         playerManager = PhotonView.Find((int)PV.InstantiationData[0]).GetComponent<PlayerManager>();
  43.     }
  44.  
  45.     void Start()
  46.     {
  47.         Cursor.lockState = CursorLockMode.Locked;
  48.         if (PV.IsMine)
  49.         {
  50.             EquipItem(0);
  51.         }
  52.         else
  53.         {
  54.             Destroy(GetComponentInChildren<Camera>().gameObject);
  55.             Destroy(rb);
  56.             Destroy(ui);
  57.         }
  58.     }
  59.  
  60.     IEnumerator waiter(float timeToWait)
  61.     {
  62.         yield return new WaitForSecondsRealtime(timeToWait);
  63.     }
  64.  
  65.     void Update()
  66.     {
  67.         if (!PV.IsMine) return;
  68.         Move();
  69.         Look();
  70.  
  71.         if (Input.GetKeyDown(KeyCode.Escape))
  72.         {
  73.             Cursor.lockState = CursorLockMode.None;
  74.         }
  75.         else
  76.  
  77.         for(int i = 0; i < items.Length; i++)
  78.         {
  79.             if (Input.GetKeyDown((i + 1).ToString()))
  80.             {
  81.                 EquipItem(i);
  82.                 break;
  83.             }
  84.         }
  85.  
  86.  
  87.         if (Input.GetMouseButtonDown(0))
  88.         {
  89.             gunInfo = items[];
  90.             float fireRate = gunInfo.fireRate;
  91.  
  92.             StartCoroutine(waiter(fireRate));
  93.             items[itemIndex].Use();
  94.         }
  95.  
  96.         if (transform.position.y < -10f)
  97.         {
  98.             Die();
  99.         }
  100.     }
  101.  
  102.     private void EquipItem(int _index)
  103.     {
  104.         if (_index == previousItemIndex)
  105.         {
  106.             return;
  107.         }
  108.  
  109.         itemIndex = _index;
  110.  
  111.         items[itemIndex].itemGameObject.SetActive(true);
  112.  
  113.         if(previousItemIndex != -1)
  114.         {
  115.             items[previousItemIndex].itemGameObject.SetActive(false);
  116.         }
  117.  
  118.         previousItemIndex = itemIndex;
  119.  
  120.         if(PV.IsMine)
  121.         {
  122.             Hashtable hash = new Hashtable();
  123.             hash.Add("itemIndex", itemIndex);
  124.             PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
  125.         }
  126.     }
  127.  
  128.     public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
  129.     {
  130.         if (changedProps.ContainsKey("itemIndex") && !PV.IsMine && targetPlayer == PV.Owner)
  131.         {
  132.             EquipItem((int)changedProps["itemIndex"]);
  133.         }
  134.     }
  135.  
  136.     private void Look()
  137.     {
  138.         transform.Rotate(Vector3.up * Input.GetAxisRaw("Mouse X") * mouseSensitivity);
  139.  
  140.         verticalLookRotation += Input.GetAxisRaw("Mouse Y") * mouseSensitivity;
  141.         verticalLookRotation = Mathf.Clamp(verticalLookRotation, -90f, 90f);
  142.  
  143.         cameraHolder.transform.localEulerAngles = Vector3.left * verticalLookRotation;
  144.     }
  145.  
  146.     private void Move()
  147.     {
  148.         Vector3 moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;
  149.  
  150.         moveAmount = Vector3.SmoothDamp(moveAmount, moveDir * (Input.GetKey(KeyCode.LeftShift) ? sprintSpeed : walkSpeed), ref smoothMoveVelocity, smoothTime);
  151.  
  152.         if (Input.GetKeyDown(KeyCode.Space) && grounded)
  153.         {
  154.             rb.AddForce(transform.up * jumpForce);
  155.         }
  156.     }
  157.  
  158.     public void SetGroundedState(bool _grounded)
  159.     {
  160.         grounded = _grounded;
  161.     }
  162.  
  163.     void FixedUpdate()
  164.     {
  165.         if (!PV.IsMine) return;
  166.         rb.MovePosition(rb.position + transform.TransformDirection(moveAmount) * Time.deltaTime);
  167.     }
  168.  
  169.     public void TakeDamage(float damage)
  170.     {
  171.         PV.RPC((nameof(RPC_TakeDamage)), PV.Owner, damage);
  172.     }
  173.  
  174.     [PunRPC]
  175.     void RPC_TakeDamage(float damage, PhotonMessageInfo info)
  176.     {
  177.         currentHealth -= damage;
  178.         healthBar.fillAmount = currentHealth / maxHealth;
  179.         if (currentHealth <= 0)
  180.         {
  181.             Die();
  182.             PlayerManager.Find(info.Sender).GetKill();
  183.         }
  184.     }
  185.  
  186.     void Die()
  187.     {
  188.         playerManager.Die();
  189.     }
  190.  
  191. }
  192.  
Advertisement
Add Comment
Please, Sign In to add comment