separated logic for weapon code refactored

This commit is contained in:
Mausham
2025-12-30 12:43:44 -08:00
parent 37dd15d1b9
commit 1a61f3c506
19 changed files with 157 additions and 191 deletions

View File

@@ -1,5 +1,6 @@
using Darkmatter.Core;
using Darkmatter.Domain;
using System;
using UnityEngine;
using VContainer;
@@ -12,15 +13,17 @@ namespace Darkmatter.Presentation
public ParticleSystem BulletHitEffectParticle;
[Header("Weapon Data")]
public float fireRate = 1f;
public float fireRate = 0.1f;
public int ammoCount = 40;
private float lastUsedTime;
public GameObject BulletHole;
public override string WeaponName => "Rifel";
public override bool canAttack => Time.time >= lastUsedTime + fireRate;
public override bool canAttack => Time.time >= lastUsedTime + fireRate && ammoCount > 0;
[Inject] private IAimProvider aimProvider;
[Inject] private ITargetProvider targetProvider;
private RaycastHit hitPoint => targetProvider.hitPoint;
@@ -28,18 +31,38 @@ namespace Darkmatter.Presentation
{
lastUsedTime = Time.time;
ammoCount--;
PlayMuzzleFlash();
if (hitPoint.transform != null) PlayBulletHitEffectParticle();
Vector3 startPos = transform.position;
Vector3 direction = aimProvider.AimDir;
Debug.Log("Attack using Gun");
//use ray cast here
//after ray cast use damage logic too
if(ammoCount <= 0) //test reload
{
Reload();
}
}
private void ShowBulletHole(Vector3 spawnPos)
{
GameObject bulletHit = Instantiate(BulletHole, spawnPos, Quaternion.LookRotation(hitPoint.normal));
Destroy(bulletHit, 5f);
}
private void PlayBulletHitEffectParticle()
{
var damageable = hitPoint.transform.GetComponent<IDamageable>();
if (damageable != null)
{
damageable.TakeDamage(10f);
}
Vector3 spawnPos = hitPoint.point + hitPoint.normal * 0.01f;
BulletHitEffectParticle.transform.position = spawnPos;
BulletHitEffectParticle.transform.rotation = Quaternion.LookRotation(hitPoint.normal);
BulletHitEffectParticle.Play(true);
}
public override void Reload()
{
base.Reload();