TIL 49 심층분석

qkrrlcks00·2024년 11월 5일

public void OnAttackInput(InputAction.CallbackContext context)
{
    if (context.phase == InputActionPhase.Performed && curEquip != null && controller.canLook)
    {
        
        curEquip.OnAttackInput();
        EquipTool equipTool = curEquip as EquipTool;
        Ray ray = camera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2));
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit,   equipTool.attackDistance, targetMask))
        {
            hit.collider.GetComponent<IDamagable>().GetDamage( equipTool.damage);

        }
       
    }
}

public void EquipNew(ItemData data)
{
    UnEquip();
    curEquip = Instantiate(data.equipPrefab, equipParent).GetComponent<Equip>();
    ;
}

public void UnEquip()
{
    if (curEquip != null)
    {
        Destroy(curEquip.gameObject);
        curEquip = null;
    }
}

Equipment :
인보크 유니티시스템을 이용한 인풋이벤트를 사용하여
키가 눌리고 있고 장비를 장착하고있으며 화면이 잠기지않은경우
아이템을 장착합니다
UnEquip은 최근낀 장비가 있다면 삭제시켜주고 curEquip 정보를 비워줍니다
EquipNew은 Unequip으로 장비해제를 해주고
equipParent 위치에 아이템 데이터를 복재하고 Equip 컴포넌트를 얻은 정보를
curEquip에 넣어줍니다

 public override void OnAttackInput()
 {
     if (!attacking)
     {
         if (CharacterManager.Instance.Player.condition.UseStamina(useStamina))

         {
             attacking = true;
             animator.SetTrigger("Attack");  
             Invoke("OnCanAttack", attackRate);
         }
     }
 }

 void OnCanAttack()
 {
     attacking = false;
 }
 public void OnHit()
 {
     Ray ray = camera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));

     RaycastHit hit;

     if (Physics.Raycast(ray, out hit, attackDistance))

     {
         if (doesGatherResources && hit.collider.TryGetComponent(out Resource resource))

         {
             resource.Gather(hit.point, hit.normal);

         }

         if (doesDealDamage && hit.collider.TryGetComponent(out IDamagable damagable))

         {
             damagable.TakePhysicalDamage(damage);

         }
     }
 }

EquipTool :
OnAttackInput()은 스태미나 사용중이라면 어택 애니메이션을 작동시키고
attackRate만큼 attacking을 false로 해주어 어택딜레이를 만듭니다
OnHit()은 스크린 절반위치에 레이를 쏘고
attackDistance 거리의 레이정보를 hit에 저장합니다
그리고 doesGatherResources가 true라면 레이에 맞은 오브젝트의 리소스의
컴포넌트를 가져오려고 시도하고 아이템을 얻습니다(Gather)

public void Gather(Vector3 hitPoint, Vector3 hitNormal)
 
{
    for (int i = 0; i < quantityPerHit; i++)
    {
        if (capacity <= 0) break;

        capacity -= 1;
        Instantiate(itemToGive.dropPrefab, hitPoint + Vector3.up, Quaternion.LookRotation(hitNormal, Vector3.up));

    }

    if (capacity <= 0)
    {
        Destroy(gameObject); /
    }
}

Gather :
hitPoint : 충돌한 위치를 나타냄 hitNormal 충돌한 표면의 수직인방향을나타냄
hitPoint와 hitNormal의 Vector3값을 매개변수로 받고
dropPrefab에 넣은 아이템을 충돌한 위치에 Y를 조금올린 위치에 생성합니다
(낑김방지?) 그리고 수량을 하나 빼줍니다
수량이 없다면 오브젝트를 파괴합니다

 public void TakePhysicalDamage(int damageAmount)
 {
     health.Subtract(damageAmount);
     onTakeDamage?.Invoke();
 }
 
 public void Subtract(float amount)
{
    curValue = Mathf.Max(curValue - amount, 0.0f); 
}

doesDealDamage가 true라면 레이에 맞은 오브젝트의 Idamagable의 컴포넌트를
가져고려고 시도하고 데미지를 줍니다 (TakePhysicalDamage)
TakePhysicalDamage : damageAmount값을 매개변수로 받아
체력을 Subtract 해줍니다
Subtract : Mathf.Max를 이용하여 최근값 - 매개변수값 과 0중
0이하가 되지 않도록 큰값을 구합니다

0개의 댓글