Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Photon.Pun;
- using Hashtable = ExitGames.Client.Photon.Hashtable;
- using Photon.Realtime;
- using UnityEngine.UI;
- public class PlayerController : MonoBehaviourPunCallbacks, IDamagable
- {
- [SerializeField] GameObject cameraHolder;
- [SerializeField] float mouseSensitivity, sprintSpeed, walkSpeed, jumpForce, smoothTime;
- [SerializeField] Image healthBar;
- [SerializeField] GameObject ui;
- [SerializeField] GameObject brains;
- float verticalLookRotation;
- bool grounded;
- Vector3 smoothMoveVelocity;
- Vector3 moveAmount;
- [SerializeField] Item[] items;
- int itemIndex;
- int previousItemIndex = -1;
- Rigidbody rb;
- PhotonView PV;
- const float maxHealth = 100f;
- float currentHealth = maxHealth;
- PlayerManager playerManager;
- void Awake()
- {
- rb = GetComponent<Rigidbody>();
- PV = GetComponent<PhotonView>();
- playerManager = PhotonView.Find((int)PV.InstantiationData[0]).GetComponent<PlayerManager>();
- }
- void Start()
- {
- Cursor.lockState = CursorLockMode.Locked;
- if (PV.IsMine)
- {
- EquipItem(0);
- }
- else
- {
- Destroy(GetComponentInChildren<Camera>().gameObject);
- Destroy(rb);
- Destroy(ui);
- }
- }
- IEnumerator waiter(float timeToWait)
- {
- yield return new WaitForSecondsRealtime(timeToWait);
- }
- void Update()
- {
- if (!PV.IsMine) return;
- Move();
- Look();
- if (Input.GetKeyDown(KeyCode.Escape))
- {
- Cursor.lockState = CursorLockMode.None;
- }
- else
- for(int i = 0; i < items.Length; i++)
- {
- if (Input.GetKeyDown((i + 1).ToString()))
- {
- EquipItem(i);
- break;
- }
- }
- if (Input.GetMouseButtonDown(0))
- {
- gunInfo = items[];
- float fireRate = gunInfo.fireRate;
- StartCoroutine(waiter(fireRate));
- items[itemIndex].Use();
- }
- if (transform.position.y < -10f)
- {
- Die();
- }
- }
- private void EquipItem(int _index)
- {
- if (_index == previousItemIndex)
- {
- return;
- }
- itemIndex = _index;
- items[itemIndex].itemGameObject.SetActive(true);
- if(previousItemIndex != -1)
- {
- items[previousItemIndex].itemGameObject.SetActive(false);
- }
- previousItemIndex = itemIndex;
- if(PV.IsMine)
- {
- Hashtable hash = new Hashtable();
- hash.Add("itemIndex", itemIndex);
- PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
- }
- }
- public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
- {
- if (changedProps.ContainsKey("itemIndex") && !PV.IsMine && targetPlayer == PV.Owner)
- {
- EquipItem((int)changedProps["itemIndex"]);
- }
- }
- private void Look()
- {
- transform.Rotate(Vector3.up * Input.GetAxisRaw("Mouse X") * mouseSensitivity);
- verticalLookRotation += Input.GetAxisRaw("Mouse Y") * mouseSensitivity;
- verticalLookRotation = Mathf.Clamp(verticalLookRotation, -90f, 90f);
- cameraHolder.transform.localEulerAngles = Vector3.left * verticalLookRotation;
- }
- private void Move()
- {
- Vector3 moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;
- moveAmount = Vector3.SmoothDamp(moveAmount, moveDir * (Input.GetKey(KeyCode.LeftShift) ? sprintSpeed : walkSpeed), ref smoothMoveVelocity, smoothTime);
- if (Input.GetKeyDown(KeyCode.Space) && grounded)
- {
- rb.AddForce(transform.up * jumpForce);
- }
- }
- public void SetGroundedState(bool _grounded)
- {
- grounded = _grounded;
- }
- void FixedUpdate()
- {
- if (!PV.IsMine) return;
- rb.MovePosition(rb.position + transform.TransformDirection(moveAmount) * Time.deltaTime);
- }
- public void TakeDamage(float damage)
- {
- PV.RPC((nameof(RPC_TakeDamage)), PV.Owner, damage);
- }
- [PunRPC]
- void RPC_TakeDamage(float damage, PhotonMessageInfo info)
- {
- currentHealth -= damage;
- healthBar.fillAmount = currentHealth / maxHealth;
- if (currentHealth <= 0)
- {
- Die();
- PlayerManager.Find(info.Sender).GetKill();
- }
- }
- void Die()
- {
- playerManager.Die();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment