separated logic for weapon code refactored
This commit is contained in:
@@ -8,11 +8,11 @@ using VContainer;
|
||||
|
||||
namespace Darkmatter.Presentation
|
||||
{
|
||||
public class PlayerMotor : MonoBehaviour, IPlayerPawn, IAimProvider
|
||||
public class PlayerMotor : MonoBehaviour, IPlayerPawn
|
||||
{
|
||||
public TwoBoneIKConstraint IKConstraint;
|
||||
|
||||
[Header("LookSetting")]
|
||||
public Camera mainCamera { get; private set; }
|
||||
public Transform cinemachineFollowTarget;
|
||||
|
||||
[Header("MoveSetting")]
|
||||
@@ -22,8 +22,6 @@ namespace Darkmatter.Presentation
|
||||
private float verticalVelocity;
|
||||
public bool isGrounded => IsOnGround();
|
||||
|
||||
public Vector3 AimDir { get; set; }
|
||||
|
||||
[Header("GroundCheckSensorSetting")]
|
||||
public float groundOffset;
|
||||
public float groundCheckRadius;
|
||||
@@ -31,115 +29,13 @@ namespace Darkmatter.Presentation
|
||||
|
||||
[Header("TurnSetting")]
|
||||
public float turnSpeed = 5f;
|
||||
public float smoothing = 10f;
|
||||
[Header("AnimationSetting")]
|
||||
public Animator animator;
|
||||
|
||||
[Header("AimSetting")]
|
||||
public Transform aim;
|
||||
Vector3 mouseWorldPos = Vector3.zero;
|
||||
public Transform muzzlePos;
|
||||
public float fireRate = 0.1f;
|
||||
float nextFiretime=0;
|
||||
|
||||
public ParticleSystem muzzleFlashParticle;
|
||||
public ParticleSystem bulletHitParticle;
|
||||
public GameObject BulletHole;
|
||||
|
||||
|
||||
[Inject] private IInputReader inputReader;
|
||||
[Inject] private PlayerConfigSO playerConfig;
|
||||
[Inject] private IWeapon gunWeapon;
|
||||
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
if(animator==null) animator = GetComponent<Animator>();
|
||||
mainCamera = Camera.main;
|
||||
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
HandleAim();
|
||||
HandleShooting();
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
|
||||
}
|
||||
public int bulletAmount = 40;
|
||||
private void HandleShooting()
|
||||
{
|
||||
//float shootingWeight = animator.GetLayerWeight(1);
|
||||
//float targetWeight = inputReader.isShooting ? 1 : 0;
|
||||
//float setshootingWeight = Mathf.Lerp(shootingWeight, targetWeight, Time.deltaTime * 5);
|
||||
//animator.SetLayerWeight(1, setshootingWeight);
|
||||
//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(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));
|
||||
|
||||
// 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());
|
||||
// }
|
||||
//}
|
||||
gunWeapon.Attack();
|
||||
}
|
||||
|
||||
IEnumerator Reload()
|
||||
{
|
||||
IKConstraint.weight = 0f;
|
||||
animator.SetLayerWeight(1, 1);
|
||||
animator.SetTrigger("Reload");
|
||||
yield return new WaitForSeconds(3f);
|
||||
bulletAmount =40;
|
||||
animator.SetLayerWeight(1, 0);
|
||||
IKConstraint.weight = 1;
|
||||
}
|
||||
|
||||
private void HandleAim()
|
||||
{
|
||||
Vector2 screenPoint = new Vector2(Screen.width/2, Screen.height/2);
|
||||
Ray ray = mainCamera.ScreenPointToRay(screenPoint);
|
||||
if(Physics.Raycast(ray,out RaycastHit hitPoint, 100f,groundLayer))
|
||||
{
|
||||
mouseWorldPos= hitPoint.point;
|
||||
//aim.position = mouseWorldPos;
|
||||
aim.position = Vector3.Lerp(aim.position, hitPoint.point, Time.deltaTime * smoothing);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//state based functions
|
||||
public void Move(Vector3 motion)
|
||||
@@ -153,8 +49,7 @@ namespace Darkmatter.Presentation
|
||||
public void SetCameraRotation(float pitch, float yaw)
|
||||
{
|
||||
cinemachineFollowTarget.rotation = Quaternion.Euler(pitch, yaw, 0);
|
||||
|
||||
//transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0,yaw,0), Time.deltaTime*turnSpeed);
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0,yaw,0), Time.deltaTime*turnSpeed); //rotate player towards the camera forward axis
|
||||
}
|
||||
|
||||
public void Jump(float jumpForce)
|
||||
|
||||
Reference in New Issue
Block a user