90 lines
2.8 KiB
C#
90 lines
2.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using JetBrains.Annotations;
|
|
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(AudioSource))]
|
|
public class Shooting : MonoBehaviour
|
|
{
|
|
public GameObject raycast;
|
|
|
|
public GameObject firePoint;
|
|
|
|
public ParticleSystem projectilePrefab;
|
|
|
|
private float hSliderValue = 0.1f;
|
|
private float _fireCountdown = 0.1f;
|
|
|
|
public AudioSource audioSource;
|
|
private NPC _myNpc;
|
|
private MovementController _moveCtrl;
|
|
|
|
private void Awake()
|
|
{
|
|
audioSource = GetComponent<AudioSource>();
|
|
TryGetComponent(out _moveCtrl);
|
|
TryGetComponent(out _myNpc);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
HandleMouseButton();
|
|
}
|
|
|
|
private void HandleMouseButton()
|
|
{
|
|
if (Input.GetMouseButton(0) && _fireCountdown <= 0f)
|
|
{
|
|
_fireCountdown = 0;
|
|
_fireCountdown += hSliderValue;
|
|
PlayerShoot();
|
|
}
|
|
|
|
_fireCountdown -= Time.deltaTime;
|
|
}
|
|
|
|
private void PlayerShoot()
|
|
{
|
|
audioSource.Play();
|
|
Instantiate(projectilePrefab, firePoint.transform.position, firePoint.transform.rotation);
|
|
if (Physics.Raycast(raycast.transform.position,
|
|
raycast.transform.forward, out var hit,
|
|
SettingsReader.Instance.GetSettings.ViewDistance))
|
|
{
|
|
if (hit.transform.TryGetComponent<NPC>(out var target))
|
|
{
|
|
target.GetDamage(SettingsReader.Instance.GetSettings.RifleDamage);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void BotShoot()
|
|
{
|
|
if (Physics.Raycast(raycast.transform.position,
|
|
raycast.transform.forward, out var hit,
|
|
SettingsReader.Instance.GetSettings.ViewDistance))
|
|
{
|
|
if (hit.transform.TryGetComponent<ICharacter>(out var target))
|
|
{
|
|
audioSource.Play();
|
|
Instantiate(projectilePrefab, firePoint.transform.position, firePoint.transform.rotation);
|
|
var mySpeed = _moveCtrl.Velocity.magnitude;
|
|
var enemySpeed = 0f;
|
|
var inCover = false;
|
|
if (target.GetCharacter.TypeAi == TypeAI.HumanAI)
|
|
enemySpeed = hit.rigidbody.velocity.magnitude;
|
|
else
|
|
{
|
|
enemySpeed = hit.collider.GetComponent<MovementController>().Velocity.magnitude;
|
|
inCover = hit.collider.GetComponent<NPC>().NpcState.InCover;
|
|
}
|
|
|
|
var hitChance = (1 - 0.5 * mySpeed) * (1 - 0.5 * enemySpeed + 0.5*inCover.ToInt()) / 1.5f;
|
|
if (!(UnityEngine.Random.Range(0f, 1f) < hitChance)) return;
|
|
_myNpc.AddReward(0.05f);
|
|
target.GetDamage(SettingsReader.Instance.GetSettings.RifleDamage);
|
|
}
|
|
}
|
|
}
|
|
} |