initial weapons

This commit is contained in:
MrTorex committed 2026-02-23 20:16:07 +03:00
1 parent b87010d2e5
commit afcd8761e6
66 files changed
+6388 -49

No files matched your search

@@ -0,0 +1,13 @@
namespace Code.Gameplay.Weapons
{
public class AssaultRifle : WeaponBase
{
protected override void Awake()
{
base.Awake();
weaponType = WeaponType.AssaultRifle;
fireMode = FireMode.Automatic;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f211f8dc45884d2a9b2ff62f3e89da6f
timeCreated: 1771856305
+76
View File
@@ -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);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1cacd4ec13d3416db0148f5758966f0a
timeCreated: 1771856241
+9
View File
@@ -0,0 +1,9 @@
namespace Code.Gameplay.Weapons
{
public enum FireMode
{
SemiAutomatic,
Automatic,
BoltAction
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d68015d7f9d94843a7dcfb0d1ddc4d4f
timeCreated: 1771856216
+12
View File
@@ -0,0 +1,12 @@
namespace Code.Gameplay.Weapons
{
public class Pistol : WeaponBase
{
protected override void Awake()
{
base.Awake();
weaponType = WeaponType.Pistol;
fireMode = FireMode.SemiAutomatic;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a7a144ebf90447faadef43bde7844c84
timeCreated: 1771856293
+13
View File
@@ -0,0 +1,13 @@
namespace Code.Gameplay.Weapons
{
public class Rifle : WeaponBase
{
protected override void Awake()
{
base.Awake();
weaponType = WeaponType.Rifle;
fireMode = FireMode.BoltAction;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c181bf00d2e4431c8fd029e8a6bffd83
timeCreated: 1771856324
+177
View File
@@ -0,0 +1,177 @@
using System;
using System.Collections;
using Code.Gameplay.Camera;
using UnityEngine;
using Random = UnityEngine.Random;
namespace Code.Gameplay.Weapons
{
public abstract class WeaponBase : MonoBehaviour
{
[SerializeField] protected WeaponType weaponType;
[SerializeField] protected string weaponName;
[SerializeField] protected FireMode fireMode;
[SerializeField] protected float damage = 20f;
[SerializeField] protected float fireRate = 0.5f;
[SerializeField] protected float bulletSpeed = 50f;
[SerializeField] protected int magazineSize = 10;
[SerializeField] protected int currentAmmo;
[SerializeField] protected int reserveAmmo = 30;
[SerializeField] protected float reloadTime = 1.5f;
[SerializeField] protected float spreadAngle = 1f;
private Transform _firePoint;
[SerializeField] protected GameObject bulletPrefab;
[SerializeField] protected ParticleSystem muzzleFlash;
[SerializeField] protected AudioClip shootSound;
[SerializeField] protected AudioClip reloadSound;
[SerializeField] protected AudioClip emptySound;
protected AudioSource audioSource;
protected bool isShooting;
protected bool isReloading;
protected float lastShotTime;
public WeaponType Type => weaponType;
public int CurrentAmmo => currentAmmo;
public int ReserveAmmo => reserveAmmo;
public int MagazineSize => magazineSize;
public bool IsReloading => isReloading;
protected virtual void Awake()
{
audioSource = GetComponent<AudioSource>();
if (audioSource == null)
audioSource = gameObject.AddComponent<AudioSource>();
currentAmmo = magazineSize;
}
protected virtual void Start()
{
_firePoint = FindFirstObjectByType<CameraController>().transform;
}
public virtual void Shoot()
{
if (isReloading) return;
if (currentAmmo <= 0)
{
PlayEmptySound();
return;
}
if (Time.time - lastShotTime < fireRate)
return;
switch (fireMode)
{
case FireMode.SemiAutomatic:
if (!isShooting) return;
break;
case FireMode.Automatic:
break;
case FireMode.BoltAction:
if (!isShooting) return;
StartCoroutine(BoltActionDelay());
break;
default:
throw new ArgumentOutOfRangeException();
}
PerformShot();
}
protected virtual void PerformShot()
{
currentAmmo--;
lastShotTime = Time.time;
SpawnBullet();
if (muzzleFlash != null)
muzzleFlash.Play();
if (shootSound != null)
audioSource.PlayOneShot(shootSound);
OnShoot();
}
protected virtual void SpawnBullet()
{
if (bulletPrefab == null) return;
var spread = Random.insideUnitSphere * (spreadAngle / 100f);
var direction = (_firePoint.forward + spread).normalized;
var bullet = Instantiate(bulletPrefab, _firePoint.position, Quaternion.LookRotation(direction));
var bulletComponent = bullet.GetComponent<Bullet>();
if (bulletComponent != null)
{
bulletComponent.Initialize(bulletSpeed, damage, direction);
}
}
protected virtual void OnShoot()
{
}
public virtual void StartShooting()
{
isShooting = true;
Shoot();
}
public virtual void StopShooting()
{
isShooting = false;
}
public virtual void Reload()
{
if (isReloading) return;
if (reserveAmmo <= 0) return;
if (currentAmmo == magazineSize) return;
StartCoroutine(ReloadCoroutine());
}
protected virtual IEnumerator ReloadCoroutine()
{
isReloading = true;
if (reloadSound != null)
audioSource.PlayOneShot(reloadSound);
yield return new WaitForSeconds(reloadTime);
var ammoNeeded = magazineSize - currentAmmo;
var ammoToTake = Mathf.Min(ammoNeeded, reserveAmmo);
currentAmmo += ammoToTake;
reserveAmmo -= ammoToTake;
isReloading = false;
}
protected virtual void PlayEmptySound()
{
if (emptySound != null)
audioSource.PlayOneShot(emptySound);
}
public virtual void AddAmmo(int amount)
{
reserveAmmo += amount;
}
protected virtual IEnumerator BoltActionDelay()
{
yield return new WaitForSeconds(reloadTime);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ea8f25ad07a74b75b439e057339b1d7e
timeCreated: 1771856259
@@ -0,0 +1,9 @@
namespace Code.Gameplay.Weapons
{
public enum WeaponType
{
Pistol,
AssaultRifle,
Rifle
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 582d1a42f0d543ec92a00b8e20c19520
timeCreated: 1771856203