84 lines
2.4 KiB
C#
84 lines
2.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using JetBrains.Annotations;
|
|
using UnityEngine;
|
|
|
|
namespace FastArena
|
|
{
|
|
public class Gun : Tool
|
|
{
|
|
public int Damage;
|
|
public float Accuracy;
|
|
public bool Auto;
|
|
public uint FireRate; //shots per minute
|
|
public uint BulletsPerShot;
|
|
public GameObject ImpactEffect;
|
|
public float ImpactForce;
|
|
|
|
private Player player;
|
|
private float shotLatency;
|
|
private bool ready = true;
|
|
|
|
[CanBeNull] private CameraZoom camZoom;
|
|
private bool zoomed;
|
|
|
|
public void Awake()
|
|
{
|
|
shotLatency = 60f / FireRate;
|
|
player = GetComponentInParent<Player>() ?? throw new Exception("can't find Player component");
|
|
camZoom = GetComponentInParent<CameraZoom>();
|
|
|
|
}
|
|
|
|
public override void Use0()
|
|
{
|
|
if (!ready) return;
|
|
StartCoroutine(ShootCoroutine());
|
|
}
|
|
|
|
IEnumerator ShootCoroutine()
|
|
{
|
|
//Debug.Log("shot");
|
|
ready = false;
|
|
var sound = GetComponent<AudioSource>();
|
|
if(sound != null)
|
|
sound.Play();
|
|
Vector3 shotVector = player.CameraPoint.transform.forward;
|
|
if (Physics.Raycast( player.CameraPoint.transform.position, shotVector, out RaycastHit hit, 1000f))
|
|
{
|
|
GameObject target = hit.collider.gameObject;
|
|
if (ImpactEffect != null)
|
|
Instantiate(ImpactEffect, hit.point, hit.transform.rotation);
|
|
if (hit.rigidbody != null)
|
|
hit.rigidbody.AddForce(shotVector * ImpactForce, ForceMode.Impulse);
|
|
if (target.TryGetComponent<Mortal>(out var mortal))
|
|
mortal.Damage(Damage);
|
|
}
|
|
|
|
yield return new WaitForSeconds(shotLatency);
|
|
ready = true;
|
|
}
|
|
|
|
public override void Use1()
|
|
{
|
|
if(camZoom == null)
|
|
return;
|
|
|
|
if (zoomed)
|
|
{
|
|
camZoom.Zoom(1);
|
|
zoomed = false;
|
|
}
|
|
else
|
|
{
|
|
camZoom.Zoom(2);
|
|
zoomed = true;
|
|
}
|
|
}
|
|
|
|
public override void Use2()
|
|
{
|
|
|
|
}
|
|
}
|
|
} |