initial weapons
This commit is contained in:
1 parent
b87010d2e5
commit
afcd8761e6
66 files changed
+6388
-49
No files matched your search
@@ -0,0 +1,76 @@
|
||||
using UnityEngine;
|
||||
using Code.Gameplay.Core;
|
||||
|
||||
namespace Code.Gameplay.Weapons
|
||||
{
|
||||
public class Bullet : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float lifeTime = 3f;
|
||||
[SerializeField] private LayerMask hitLayers = -1;
|
||||
[SerializeField] private GameObject impactEffect;
|
||||
[SerializeField] private AudioClip impactSound;
|
||||
|
||||
private float _speed;
|
||||
private float _damage;
|
||||
private Vector3 _direction;
|
||||
private bool _isInitialized;
|
||||
private AudioSource _audioSource;
|
||||
|
||||
public void Initialize(float speed, float damage, Vector3 direction)
|
||||
{
|
||||
_speed = speed;
|
||||
_damage = damage;
|
||||
_direction = direction.normalized;
|
||||
_isInitialized = true;
|
||||
|
||||
_audioSource = GetComponent<AudioSource>();
|
||||
|
||||
Destroy(gameObject, lifeTime);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!_isInitialized) return;
|
||||
|
||||
transform.position += _direction * (_speed * Time.deltaTime);
|
||||
|
||||
transform.rotation = Quaternion.LookRotation(_direction);
|
||||
|
||||
if (Physics.Raycast(transform.position, _direction, out var hit, _speed * Time.deltaTime, hitLayers))
|
||||
{
|
||||
HitTarget(hit);
|
||||
}
|
||||
}
|
||||
|
||||
private void HitTarget(RaycastHit hit)
|
||||
{
|
||||
var damageable = hit.collider.GetComponentInParent<IDamageable>();
|
||||
damageable?.TakeDamage(_damage);
|
||||
|
||||
if (impactEffect != null)
|
||||
{
|
||||
Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
|
||||
}
|
||||
|
||||
if (impactSound != null && _audioSource != null)
|
||||
{
|
||||
_audioSource.PlayOneShot(impactSound);
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
var damageable = other.GetComponentInParent<IDamageable>();
|
||||
damageable?.TakeDamage(_damage);
|
||||
|
||||
if (impactEffect != null)
|
||||
{
|
||||
Instantiate(impactEffect, transform.position, Quaternion.identity);
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user