Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using TMPro;
- using Photon.Pun;
- public class weaponScript : MonoBehaviour
- {
- public weaponSO weaponStats;
- public GameObject reloadingText;
- public TMP_Text ammoValue;
- public Transform firePoint;
- public GameObject bulletPrefab;
- public float bulletForce = 2f;
- public bool isShooting;
- public float pistolDamage;
- public int currentAmmoClip = 1;
- public int maxAmmoClip;
- public int currentReserve = 1;
- public int maxReserve;
- public float reloadTime = 2f;
- public bool isReloading = false;
- public int bulletsShot;
- public float startingDamage;
- PhotonView view;
- public void Start()
- {
- view = this.GetComponent<PhotonView>();
- WeaponStats();
- currentAmmoClip = maxAmmoClip;
- currentReserve = maxReserve;
- bulletsShot = maxAmmoClip - currentAmmoClip;
- isShooting = false;
- weaponStats.damage = startingDamage;
- }
- public void Update()
- {
- ammoValue.text = currentAmmoClip.ToString("0") + "/" + currentReserve.ToString("0");
- if (Input.GetKeyDown(KeyCode.R))
- {
- if(currentReserve <= 0)
- {
- return;
- }
- if (currentAmmoClip == maxAmmoClip && !isReloading)
- {
- return;
- }
- else
- {
- StartCoroutine(Reload());
- return;
- }
- }
- Debug.Log("this works");
- if(Input.GetButtonDown("Fire1") && !isReloading && currentAmmoClip >= 1 && view.IsMine)
- {
- Debug.Log("can shoot");
- Shoot();
- }
- if(isReloading)
- return;
- if(currentAmmoClip <= 0 && currentReserve >= 1)
- {
- StartCoroutine(Reload());
- return;
- }
- }
- IEnumerator Reload()
- {
- reloadingText.SetActive(true);
- isReloading = true;
- yield return new WaitForSeconds(reloadTime);
- if(currentReserve <= bulletsShot)
- {
- currentAmmoClip += currentReserve;
- currentReserve = 0;
- }
- else
- {
- currentReserve -= bulletsShot;
- currentAmmoClip = maxAmmoClip;
- }
- bulletsShot = 0;
- reloadingText.SetActive(false);
- isReloading = false;
- }
- [PunRPC]
- public void RPC_Shoot()
- {
- Debug.Log("RPC_Shoot works");
- GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
- Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
- rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
- }
- void Shoot()
- {
- Debug.Log("Shoot() works");
- bulletsShot += 1;
- currentAmmoClip -= 1;
- isShooting = true;
- FindObjectOfType<AudioManager>().Play("shot1");
- view.RPC("RPC_Shoot", RpcTarget.All);
- }
- public void WeaponStats()
- {
- pistolDamage = weaponStats.initialDamage;
- maxAmmoClip = weaponStats.maxAmmoClip;
- maxReserve = weaponStats.maxReserve;
- reloadTime = weaponStats.reloadTime;
- bulletForce = weaponStats.bulletForce;
- startingDamage = weaponStats.initialDamage;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement