2026-05-19 무기추가
진행중인 사항 - InputManager + .inputactions: WeaponSlot1/2/3 액션 추가, 키 1/2/3 매핑 PlayerController 통합: Player에 PlayerWeaponInventory 컴포넌트 자동 부착 OnWeaponChanged 구독 → idle/walk State 이름 동적 교체 OnPunchInput 분기: 무장 시 weapon.AttackRootNode 사용 WeaponSlot 입력 핸들러 3개 추가 (EquipUnarmed / EquipSlot(0) / EquipSlot(1))
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
// 공격 판정 영역의 도형. Circle은 원거리 균등 판정, Box는 길쭉한 직선/넓은 부채꼴에 적합.
|
||||
public enum HitShape
|
||||
{
|
||||
Circle,
|
||||
Box
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// ActionData
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -44,8 +51,10 @@ public class ActionData : ScriptableObject
|
||||
// ─── 공격 판정 (HasHit=true일 때 적용) ─────────────────────────────
|
||||
[Header("Hit")]
|
||||
public bool HasHit = true; // 이 액션이 데미지를 주는지
|
||||
public HitShape Shape = HitShape.Circle; // Circle = Radius 사용, Box = HitSize 사용
|
||||
public Vector2 Offset = new Vector2(0.5f, 0f); // 캐릭터 기준 hit 영역 중심 (X는 facing 방향)
|
||||
public float Radius = 0.5f; // hit 영역 반경 (AttackHitbox.CircleCollider2D)
|
||||
public float Radius = 0.5f; // Circle일 때 사용하는 반경
|
||||
public Vector2 HitSize = new Vector2(1f, 0.5f); // Box일 때 사용하는 가로/세로 크기
|
||||
public int Damage = 10; // 데미지 양
|
||||
public float HitTiming = 0.15f; // 액션 시작 후 hit 발동까지 시간 (선딜)
|
||||
public float HitDuration = 0f; // hit 영역이 활성 상태로 유지되는 시간 (0이면 단발)
|
||||
|
||||
@@ -20,7 +20,11 @@ public class AttackHitbox : MonoBehaviour
|
||||
// PlayerController가 구독해서 "방금 hit한 적" 추적용 (잡기 타겟 우선 등에 활용).
|
||||
public event System.Action<IDamageable> OnHit;
|
||||
|
||||
private CircleCollider2D _collider;
|
||||
private CircleCollider2D _circleCollider; // Circle 모양 판정용
|
||||
private BoxCollider2D _boxCollider; // Box 모양 판정용 (Awake에서 자동 생성)
|
||||
private HitShape _activeShape; // 현재 활성 도형 (ScanImmediateOverlap에서 분기)
|
||||
private float _activeRadius; // Circle일 때 사용하는 반경 (스캔/Gizmo용 백업)
|
||||
private Vector2 _activeHitSize; // Box일 때 사용하는 크기 백업
|
||||
|
||||
// ─── 현재 활성 액션의 데미지/효과 데이터 (Activate에서 세팅) ─────────
|
||||
private int _damage;
|
||||
@@ -39,18 +43,43 @@ public class AttackHitbox : MonoBehaviour
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_collider = GetComponent<CircleCollider2D>();
|
||||
// 플레이어 몸체는 적과 물리 충돌하지 않으므로, 공격 판정은 이 트리거만 사용한다.
|
||||
_collider.isTrigger = true;
|
||||
_collider.enabled = false;
|
||||
_circleCollider = GetComponent<CircleCollider2D>();
|
||||
// 플레이어 몸체는 적과 물리 충돌하지 않으므로, 공격 판정은 트리거만 사용.
|
||||
_circleCollider.isTrigger = true;
|
||||
_circleCollider.enabled = false;
|
||||
|
||||
// BoxCollider2D는 없으면 자동 추가. 기존 프리팹과의 호환성 유지.
|
||||
_boxCollider = GetComponent<BoxCollider2D>();
|
||||
if (_boxCollider == null)
|
||||
_boxCollider = gameObject.AddComponent<BoxCollider2D>();
|
||||
_boxCollider.isTrigger = true;
|
||||
_boxCollider.enabled = false;
|
||||
}
|
||||
|
||||
// 액션 시작 시 호출. 위치/반경/데미지 등 모든 파라미터 세팅 후 콜라이더 활성화.
|
||||
// 액션 시작 시 호출. 도형/위치/데미지 세팅 후 해당 콜라이더만 활성화.
|
||||
// _alreadyHit를 클리어해서 새 공격으로 다시 hit 가능하게 함.
|
||||
public void Activate(ActionData data, Vector2 localPosition, Vector2 hitVelocity, Vector2 sourcePosition, Vector2? hitTargetPosition, bool correctHitTargetY, int hitPositionSolidMask, LayerMask targetLayer)
|
||||
{
|
||||
transform.localPosition = localPosition;
|
||||
_collider.radius = data.Radius;
|
||||
|
||||
// 도형에 맞는 콜라이더만 활성화. 다른 도형 콜라이더는 비활성으로 보장.
|
||||
_activeShape = data.Shape;
|
||||
if (data.Shape == HitShape.Box)
|
||||
{
|
||||
_boxCollider.size = data.HitSize;
|
||||
_boxCollider.offset = Vector2.zero;
|
||||
_boxCollider.enabled = true;
|
||||
_circleCollider.enabled = false;
|
||||
_activeHitSize = data.HitSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
_circleCollider.radius = data.Radius;
|
||||
_circleCollider.enabled = true;
|
||||
_boxCollider.enabled = false;
|
||||
_activeRadius = data.Radius;
|
||||
}
|
||||
|
||||
_damage = data.Damage;
|
||||
_hitVelocity = hitVelocity;
|
||||
_hitSourcePosition = sourcePosition;
|
||||
@@ -62,24 +91,27 @@ public void Activate(ActionData data, Vector2 localPosition, Vector2 hitVelocity
|
||||
_hitReactionState = data.HitReactionAnimationState;
|
||||
_targetLayer = targetLayer;
|
||||
_alreadyHit.Clear();
|
||||
_collider.enabled = true;
|
||||
|
||||
// 판정이 켜진 순간 이미 범위 안에 있던 적도 같은 프레임에 잡아낸다.
|
||||
ScanImmediateOverlap();
|
||||
}
|
||||
|
||||
// 액션의 HitDuration이 끝나면 호출. 콜라이더 비활성화 + hit 기록 초기화.
|
||||
// 액션의 HitDuration이 끝나면 호출. 모든 콜라이더 비활성화 + hit 기록 초기화.
|
||||
public void Deactivate()
|
||||
{
|
||||
_collider.enabled = false;
|
||||
_circleCollider.enabled = false;
|
||||
_boxCollider.enabled = false;
|
||||
_alreadyHit.Clear();
|
||||
}
|
||||
|
||||
// 활성 순간 즉시 검사: Physics2D.OverlapCircleAll로 현재 겹친 콜라이더를 모두 가져와 TryDamage.
|
||||
// 활성 순간 즉시 검사: 도형에 맞는 OverlapAll로 현재 겹친 콜라이더를 모두 가져와 TryDamage.
|
||||
// 이게 없으면 짧은 hit window (예: HitDuration=0.02)에 OnTriggerEnter가 못 따라옴.
|
||||
private void ScanImmediateOverlap()
|
||||
{
|
||||
Collider2D[] hits = Physics2D.OverlapCircleAll(transform.position, _collider.radius, _targetLayer);
|
||||
Collider2D[] hits = _activeShape == HitShape.Box
|
||||
? Physics2D.OverlapBoxAll(transform.position, _activeHitSize, 0f, _targetLayer)
|
||||
: Physics2D.OverlapCircleAll(transform.position, _activeRadius, _targetLayer);
|
||||
|
||||
foreach (var hit in hits)
|
||||
TryDamage(hit);
|
||||
}
|
||||
|
||||
78
Assets/02_Scripts/Combat/PlayerWeaponInventory.cs
Normal file
78
Assets/02_Scripts/Combat/PlayerWeaponInventory.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
// ============================================================================
|
||||
// PlayerWeaponInventory
|
||||
// ----------------------------------------------------------------------------
|
||||
// 플레이어가 보유한 무기 목록 + 현재 장착 무기 관리.
|
||||
// PlayerController가 OnWeaponChanged 이벤트를 구독해서 애니메이션/공격 콤보 교체.
|
||||
//
|
||||
// 슬롯 매핑 (외부 코드의 EquipSlot 인덱스):
|
||||
// -1 → 맨손 (CurrentWeapon == null)
|
||||
// 0 → 첫 번째로 픽업한 무기
|
||||
// 1 → 두 번째로 픽업한 무기
|
||||
// ...
|
||||
//
|
||||
// 입력 키 매핑 (PlayerController에서 처리):
|
||||
// Key 1 → EquipUnarmed()
|
||||
// Key 2 → EquipSlot(0)
|
||||
// Key 3 → EquipSlot(1)
|
||||
// ============================================================================
|
||||
public class PlayerWeaponInventory : MonoBehaviour
|
||||
{
|
||||
private readonly List<WeaponData> _weapons = new();
|
||||
private int _currentIndex = -1; // -1 = 맨손
|
||||
|
||||
// 무기 교체 시 발화. null이면 맨손.
|
||||
public event Action<WeaponData> OnWeaponChanged;
|
||||
|
||||
public WeaponData CurrentWeapon =>
|
||||
_currentIndex >= 0 && _currentIndex < _weapons.Count
|
||||
? _weapons[_currentIndex]
|
||||
: null;
|
||||
public bool IsArmed => CurrentWeapon != null;
|
||||
public int Count => _weapons.Count;
|
||||
|
||||
// 무기 추가. 이미 보유 중이면 false 반환 (중복 픽업 방지).
|
||||
// 첫 픽업 시 자동 장착할지는 호출자가 OnWeaponChanged를 보고 결정.
|
||||
public bool Pickup(WeaponData weapon)
|
||||
{
|
||||
if (weapon == null) return false;
|
||||
if (_weapons.Contains(weapon)) return false;
|
||||
|
||||
_weapons.Add(weapon);
|
||||
|
||||
// 첫 픽업이면 자동으로 장착해주면 사용자 편의 ↑.
|
||||
if (_weapons.Count == 1 && _currentIndex == -1)
|
||||
{
|
||||
_currentIndex = 0;
|
||||
OnWeaponChanged?.Invoke(_weapons[0]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 맨손 상태로 전환.
|
||||
public void EquipUnarmed()
|
||||
{
|
||||
if (_currentIndex == -1) return;
|
||||
_currentIndex = -1;
|
||||
OnWeaponChanged?.Invoke(null);
|
||||
}
|
||||
|
||||
// 슬롯 번호로 직접 장착. 슬롯이 비어있으면 무시.
|
||||
public void EquipSlot(int slotIndex)
|
||||
{
|
||||
if (slotIndex < 0 || slotIndex >= _weapons.Count) return;
|
||||
if (_currentIndex == slotIndex) return;
|
||||
_currentIndex = slotIndex;
|
||||
OnWeaponChanged?.Invoke(_weapons[slotIndex]);
|
||||
}
|
||||
|
||||
// 디버그용: 슬롯에 해당 무기가 있는지 확인.
|
||||
public bool HasWeaponInSlot(int slotIndex)
|
||||
{
|
||||
return slotIndex >= 0 && slotIndex < _weapons.Count && _weapons[slotIndex] != null;
|
||||
}
|
||||
}
|
||||
2
Assets/02_Scripts/Combat/PlayerWeaponInventory.cs.meta
Normal file
2
Assets/02_Scripts/Combat/PlayerWeaponInventory.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b8fc250baa41ff840a2b5d6a0b308081
|
||||
39
Assets/02_Scripts/Combat/WeaponData.cs
Normal file
39
Assets/02_Scripts/Combat/WeaponData.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using UnityEngine;
|
||||
|
||||
// ============================================================================
|
||||
// WeaponType
|
||||
// ----------------------------------------------------------------------------
|
||||
// 무기 분류. 향후 다른 시스템(예: 데미지 면역, 약점)에서 분기용으로 사용.
|
||||
// ============================================================================
|
||||
public enum WeaponType
|
||||
{
|
||||
Sword,
|
||||
Gun
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// WeaponData
|
||||
// ----------------------------------------------------------------------------
|
||||
// 무기 한 종류의 정의를 담은 ScriptableObject.
|
||||
// .asset 파일로 만들어 적의 드랍 슬롯과 픽업 프리팹에 할당.
|
||||
//
|
||||
// 효과:
|
||||
// - 장착 시 _idleAnimationState / _walkAnimationState를 이 무기 버전으로 교체
|
||||
// - Punch 입력 시 _punchRootNode 대신 AttackRootNode 사용
|
||||
// ============================================================================
|
||||
[CreateAssetMenu(fileName = "WeaponData", menuName = "Combat/WeaponData")]
|
||||
public class WeaponData : ScriptableObject
|
||||
{
|
||||
public string DisplayName; // UI 표시/디버그용 이름
|
||||
public WeaponType Type = WeaponType.Sword; // 분류 (시스템 분기용)
|
||||
|
||||
[Header("Equipped Animations")]
|
||||
public string IdleAnimationState; // 장착 시 idle 애니메이션 (예: "Idle_Sword")
|
||||
public string WalkAnimationState; // 장착 시 walk 애니메이션 (예: "Walk_Sword")
|
||||
|
||||
[Header("Attack")]
|
||||
public ComboNode AttackRootNode; // Punch 입력 시 사용할 콤보 root (단일 노드 가능)
|
||||
|
||||
[Header("Pickup Visual")]
|
||||
public Sprite PickupSprite; // 월드에 떨어뜨려진 모습
|
||||
}
|
||||
2
Assets/02_Scripts/Combat/WeaponData.cs.meta
Normal file
2
Assets/02_Scripts/Combat/WeaponData.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 45b80750b5f29714e8386d1330b97550
|
||||
58
Assets/02_Scripts/Combat/WeaponPickup.cs
Normal file
58
Assets/02_Scripts/Combat/WeaponPickup.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using UnityEngine;
|
||||
|
||||
// ============================================================================
|
||||
// WeaponPickup
|
||||
// ----------------------------------------------------------------------------
|
||||
// 월드에 떨어진 무기. 플레이어가 트리거 영역에 들어오면 자동으로 인벤토리에 추가되고 사라짐.
|
||||
//
|
||||
// 두 가지 사용 패턴:
|
||||
// 1) 씬에 미리 배치 → Inspector에서 _weapon 할당 → 자동 표시
|
||||
// 2) 코드로 동적 스폰 → Initialize(WeaponData)로 무기 주입 (Enemy.HandleDeath)
|
||||
// ============================================================================
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
[RequireComponent(typeof(SpriteRenderer))]
|
||||
public class WeaponPickup : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private WeaponData _weapon;
|
||||
|
||||
private SpriteRenderer _spriteRenderer;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
|
||||
// 픽업은 물리 충돌 아닌 트리거. 플레이어가 통과하면서 줍게.
|
||||
Collider2D col = GetComponent<Collider2D>();
|
||||
col.isTrigger = true;
|
||||
|
||||
ApplyVisual();
|
||||
}
|
||||
|
||||
// 코드로 동적 스폰 시 호출. Enemy.HandleDeath에서 사용.
|
||||
public void Initialize(WeaponData weapon)
|
||||
{
|
||||
_weapon = weapon;
|
||||
ApplyVisual();
|
||||
}
|
||||
|
||||
private void ApplyVisual()
|
||||
{
|
||||
if (_spriteRenderer == null || _weapon == null) return;
|
||||
if (_weapon.PickupSprite != null)
|
||||
_spriteRenderer.sprite = _weapon.PickupSprite;
|
||||
}
|
||||
|
||||
// 플레이어가 트리거 영역에 진입 시 발화.
|
||||
// Player(또는 자식)에 PlayerWeaponInventory 컴포넌트가 있어야 픽업 됨.
|
||||
// 이미 보유 중이면 Pickup이 false 반환 → pickup 오브젝트 그대로 유지.
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
if (_weapon == null) return;
|
||||
|
||||
PlayerWeaponInventory inventory = other.GetComponentInParent<PlayerWeaponInventory>();
|
||||
if (inventory == null) return;
|
||||
|
||||
if (inventory.Pickup(_weapon))
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
2
Assets/02_Scripts/Combat/WeaponPickup.cs.meta
Normal file
2
Assets/02_Scripts/Combat/WeaponPickup.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2ccfada8b880c0045804198a4754cf2f
|
||||
Reference in New Issue
Block a user