separated weapon class left to separate logic and implementation

This commit is contained in:
Mausham
2025-12-29 23:31:03 +05:45
parent eadfddf220
commit 37dd15d1b9
13 changed files with 211 additions and 35 deletions

View File

@@ -15,6 +15,7 @@ namespace Darkmatter.App
[SerializeField] private PlayerAnimController playerAnim;
[SerializeField] private PlayerConfigSO playerConfig;
[SerializeField] private CameraConfigSO cameraConfig;
[SerializeField] private GunWeapon gunWeapon;
protected override void Configure(IContainerBuilder builder)
{
builder.RegisterEntryPoint<PlayerController>(Lifetime.Scoped);
@@ -26,6 +27,7 @@ namespace Darkmatter.App
builder.RegisterComponent(playerConfig);
builder.RegisterComponent(cameraConfig);
builder.RegisterComponent<IWeapon>(gunWeapon);
builder.Register<PlayerStateMachine>(Lifetime.Scoped);
builder.RegisterComponentInHierarchy<CameraService>().As<ICameraService>();
}

View File

@@ -0,0 +1,13 @@
using UnityEngine;
namespace Darkmatter.Core
{
public interface IWeapon
{
public void Attack();
public void Reload();
bool canAttack { get; }
string WeaponName { get; }
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9f82b92b1e5608845a9706ad20b155f8

View File

@@ -0,0 +1,9 @@
using UnityEngine;
namespace Darkmatter.Core
{
public interface IAimProvider
{
public Vector3 AimDir { get; set; }
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ebad764a0da2b7b4d8273baf63dd5500

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 156c6779124556c48b177104c19c29f9
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,19 @@
using Darkmatter.Core;
using UnityEngine;
namespace Darkmatter.Domain
{
public abstract class WeaponBase : MonoBehaviour, IWeapon
{
public abstract bool canAttack { get; }
public abstract string WeaponName {get; }
public abstract void Attack();
public virtual void Reload()
{
Debug.Log("Reloading");
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 87a4d2aa64c2bc1468b8e548d66764e1

View File

@@ -8,7 +8,7 @@ using VContainer;
namespace Darkmatter.Presentation
{
public class PlayerMotor : MonoBehaviour, IPlayerPawn
public class PlayerMotor : MonoBehaviour, IPlayerPawn, IAimProvider
{
public TwoBoneIKConstraint IKConstraint;
[Header("LookSetting")]
@@ -22,6 +22,8 @@ namespace Darkmatter.Presentation
private float verticalVelocity;
public bool isGrounded => IsOnGround();
public Vector3 AimDir { get; set; }
[Header("GroundCheckSensorSetting")]
public float groundOffset;
public float groundCheckRadius;
@@ -47,6 +49,7 @@ namespace Darkmatter.Presentation
[Inject] private IInputReader inputReader;
[Inject] private PlayerConfigSO playerConfig;
[Inject] private IWeapon gunWeapon;
@@ -60,7 +63,7 @@ namespace Darkmatter.Presentation
private void Update()
{
HandleAim();
HandleShooting();
HandleShooting();
}
private void LateUpdate()
@@ -77,34 +80,35 @@ namespace Darkmatter.Presentation
//animator.SetBool("IsShooting", inputReader.isShooting);
if (!inputReader.isShooting) return;
if(Time.time>=nextFiretime && bulletAmount > 0)
{
bulletAmount--;
nextFiretime = Time.time + fireRate;
Vector2 screenPoint = new Vector2(Screen.width / 2, Screen.height / 2);
Ray ray = mainCamera.ScreenPointToRay(screenPoint);
RaycastHit hit;
Physics.Raycast(ray, out hit, 100f);
//if(Time.time>=nextFiretime && bulletAmount > 0)
//{
// bulletAmount--;
// nextFiretime = Time.time + fireRate;
// Vector2 screenPoint = new Vector2(Screen.width / 2, Screen.height / 2);
// Ray ray = mainCamera.ScreenPointToRay(screenPoint);
// RaycastHit hit;
// Physics.Raycast(ray, out hit, 100f);
if(hit.collider.GetComponent<IDamageable>() != null)
{
hit.collider.GetComponent<IDamageable>().TakeDamage(10f);
}
// if(hit.collider.GetComponent<IDamageable>() != null)
// {
// hit.collider.GetComponent<IDamageable>().TakeDamage(10f);
// }
Vector3 spawnPos = hit.point + hit.normal * 0.01f;
GameObject bulletHit = Instantiate(BulletHole, spawnPos, Quaternion.LookRotation(hit.normal));
// Vector3 spawnPos = hit.point + hit.normal * 0.01f;
// GameObject bulletHit = Instantiate(BulletHole, spawnPos, Quaternion.LookRotation(hit.normal));
bulletHitParticle.transform.position = spawnPos;
bulletHitParticle.transform.rotation = Quaternion.LookRotation(hit.normal);
bulletHitParticle.Play(true);
Destroy(bulletHit,5f);
muzzleFlashParticle.Play(true);
// bulletHitParticle.transform.position = spawnPos;
// bulletHitParticle.transform.rotation = Quaternion.LookRotation(hit.normal);
// bulletHitParticle.Play(true);
// Destroy(bulletHit,5f);
// muzzleFlashParticle.Play(true);
if(bulletAmount==0)
{
StartCoroutine(Reload());
}
}
// if(bulletAmount==0)
// {
// StartCoroutine(Reload());
// }
//}
gunWeapon.Attack();
}
IEnumerator Reload()
@@ -130,6 +134,7 @@ namespace Darkmatter.Presentation
}
Vector3 aimDir = (mouseWorldPos - transform.position).normalized;
AimDir = aimDir;
aimDir.y = 0; //
Quaternion targetRot = Quaternion.LookRotation(aimDir);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, Time.deltaTime * turnSpeed);

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8bb38fc0897142643b14cbd6f6c2d9bb
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,54 @@
using Darkmatter.Core;
using Darkmatter.Domain;
using UnityEngine;
using VContainer;
namespace Darkmatter.Presentation
{
public class GunWeapon : WeaponBase
{
[Header("VFX")]
public ParticleSystem MuzzleFlashParticle;
public ParticleSystem BulletHitEffectParticle;
[Header("Weapon Data")]
public float fireRate = 1f;
public int ammoCount = 40;
private float lastUsedTime;
public override string WeaponName => "Rifel";
public override bool canAttack => Time.time >= lastUsedTime + fireRate;
[Inject] private IAimProvider aimProvider;
public override void Attack()
{
lastUsedTime = Time.time;
ammoCount--;
PlayMuzzleFlash();
Vector3 startPos = transform.position;
Vector3 direction = aimProvider.AimDir;
Debug.Log("Attack using Gun");
//use ray cast here
//after ray cast use damage logic too
}
public override void Reload()
{
base.Reload();
ammoCount = 40;
}
private void PlayMuzzleFlash()
{
MuzzleFlashParticle.Play(true);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: bf22e36737a853040b18d29e45fa59ee