You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.3 KiB
85 lines
2.3 KiB
using Player; |
|
using UnityEngine; |
|
|
|
namespace Ai |
|
{ |
|
public class Gun : MonoBehaviour |
|
{ |
|
|
|
[SerializeField] protected Bullet _bullet; |
|
[SerializeField] protected int _shotsPerBurst; |
|
[SerializeField] protected float _delay; |
|
[SerializeField] private bool _auto; |
|
[SerializeField] protected bool _toPlayer; |
|
[SerializeField] protected float _radius = 1; |
|
[SerializeField] protected GameObject _shootFx; |
|
[SerializeField] protected Vector3 _shootFxOffset; |
|
|
|
protected float _currentTime; |
|
private bool _canShoot; |
|
protected int _shotCount; |
|
|
|
public bool CanShoot |
|
{ |
|
get { return _auto || _canShoot; } |
|
set { _canShoot = value; } |
|
} |
|
|
|
private void OnEnable() |
|
{ |
|
_shotCount = 0; |
|
} |
|
|
|
protected virtual void Update() |
|
{ |
|
if (!CanShoot || GlobalsVar.gBoard != null && GlobalsVar.gBoard.DialogsCount > 0) return; |
|
_currentTime += Time.deltaTime; |
|
if (_currentTime >= _delay && (_shotCount < _shotsPerBurst || _shotsPerBurst == 0)) |
|
{ |
|
_currentTime = 0f; |
|
_shotCount++; |
|
Shoot(); |
|
} |
|
} |
|
|
|
protected virtual void Shoot() |
|
{ |
|
if (_shootFx) |
|
{ |
|
var fx = Instantiate(_shootFx); |
|
fx.transform.position = transform.position + _shootFxOffset; |
|
} |
|
|
|
var bullet = Instantiate(_bullet); |
|
string shootSound = GetShootSoundByBulletName(bullet.name); |
|
SoundsManager.Instance.PlaySound(shootSound); |
|
|
|
bullet.transform.position = transform.position; |
|
var direction = _toPlayer ? AiController.Instance.PlayerPosition : transform.forward; |
|
if (_toPlayer) |
|
{ |
|
var randomed = (Vector3) Random.insideUnitCircle; |
|
randomed.z = randomed.y; |
|
direction += randomed * _radius; |
|
} |
|
|
|
bullet.Init(direction, 0, 0, _toPlayer); |
|
bullet.transform.forward = transform.forward; |
|
} |
|
|
|
protected string GetShootSoundByBulletName(string bulletName) |
|
{ |
|
string shootSound = ""; |
|
if (bulletName.IndexOf("Bomb") != -1) |
|
shootSound = "grenade"; |
|
else if (bulletName.IndexOf("Shooter") != -1) |
|
shootSound = "shooter"; |
|
else if (bulletName.IndexOf("Ricochet") != -1) |
|
shootSound = "ricochet"; |
|
else if (bulletName.IndexOf("Flame") != -1) |
|
shootSound = "flame"; |
|
|
|
return shootSound; |
|
} |
|
} |
|
}
|
|
|