separated weapon class left to separate logic and implementation
This commit is contained in:
@@ -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>();
|
||||
}
|
||||
|
||||
13
Assets/Darkmatter/Code/Core/Contracts/Others/IWeapon.cs
Normal file
13
Assets/Darkmatter/Code/Core/Contracts/Others/IWeapon.cs
Normal 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; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9f82b92b1e5608845a9706ad20b155f8
|
||||
@@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Darkmatter.Core
|
||||
{
|
||||
public interface IAimProvider
|
||||
{
|
||||
public Vector3 AimDir { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ebad764a0da2b7b4d8273baf63dd5500
|
||||
8
Assets/Darkmatter/Code/Domain/Weapon.meta
Normal file
8
Assets/Darkmatter/Code/Domain/Weapon.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 156c6779124556c48b177104c19c29f9
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
19
Assets/Darkmatter/Code/Domain/Weapon/WeaponBase.cs
Normal file
19
Assets/Darkmatter/Code/Domain/Weapon/WeaponBase.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Darkmatter/Code/Domain/Weapon/WeaponBase.cs.meta
Normal file
2
Assets/Darkmatter/Code/Domain/Weapon/WeaponBase.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 87a4d2aa64c2bc1468b8e548d66764e1
|
||||
@@ -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);
|
||||
|
||||
8
Assets/Darkmatter/Code/Presentation/Weapons.meta
Normal file
8
Assets/Darkmatter/Code/Presentation/Weapons.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8bb38fc0897142643b14cbd6f6c2d9bb
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
54
Assets/Darkmatter/Code/Presentation/Weapons/GunWeapon.cs
Normal file
54
Assets/Darkmatter/Code/Presentation/Weapons/GunWeapon.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bf22e36737a853040b18d29e45fa59ee
|
||||
Reference in New Issue
Block a user