Unity/TopdownEngine

Inventory에 아이템 집어넣기 & 초기 Weapon 설정

lipnus 2023. 1. 29. 23:57
반응형

ItemPicker.cs

public virtual void Pick(string targetInventoryName, string playerID = "Player1")
{
    FindTargetInventory(targetInventoryName, playerID);
    if (_targetInventory == null)
    {
        return;
    }

    if (!Pickable()) 
    {
        PickFail ();
        return;
    }

    DetermineMaxQuantity ();
    if (!Application.isPlaying)
    {
        if (!Item.ForceSlotIndex)
        {
            _targetInventory.AddItem(Item, 1);	
        }
        else
        {
            _targetInventory.AddItemAt(Item, 1, Item.TargetIndex);
        }
    }				
    else
    {
        MMInventoryEvent.Trigger(MMInventoryEventType.Pick, null, Item.TargetInventoryName, Item, _pickedQuantity, 0, playerID);
    }				
    if (Item.Pick(playerID))
    {
        RemainingQuantity = RemainingQuantity - _pickedQuantity;
        PickSuccess();
        DisableObjectIfNeeded();
    }			
}

_targetInventory.AddItemAt(Item, 1, Item.TargetIndex);

 

이 메소드를 이용하면 임의로 아이템을 컨트롤 할 수 있을 듯함

 

CharacterInventory.cs

초기 아이템이 장착되는 부분

 

protected virtual void Setup()
{
    if (InventoryTransform == null)
    {
        InventoryTransform = this.transform;
    }
    GrabInventories ();
    if (CharacterHandleWeapon == null)
    {
        CharacterHandleWeapon = _character?.FindAbility<CharacterHandleWeapon> ();	
    }
    FillAvailableWeaponsLists ();

    if (_initialized)
    {
        return;
    }

    bool mainInventoryEmpty = true;
    if (MainInventory != null)
    {
        mainInventoryEmpty = MainInventory.NumberOfFilledSlots == 0;
    }
    bool canAutoPick = !(AutoPickOnlyIfMainInventoryIsEmpty && !mainInventoryEmpty);
    bool canAutoEquip = !(AutoEquipOnlyIfMainInventoryIsEmpty && !mainInventoryEmpty);

    // we auto pick items if needed
    if ((AutoPickItems.Length > 0) && !_initialized && canAutoPick)
    {
        foreach (AutoPickItem item in AutoPickItems)
        {
            MMInventoryEvent.Trigger(MMInventoryEventType.Pick, null, item.Item.TargetInventoryName, item.Item, item.Quantity, 0, PlayerID);
        }
    }

    // we auto equip a weapon if needed
    if ((AutoEquipWeaponOnStart != null) && !_initialized && canAutoEquip)
    {
        MMInventoryEvent.Trigger(MMInventoryEventType.Pick, null, AutoEquipWeaponOnStart.TargetInventoryName, AutoEquipWeaponOnStart, 1, 0, PlayerID);
        EquipWeapon(AutoEquipWeaponOnStart.ItemID);
    }
    _initialized = true;
}

public virtual void EquipWeapon(string weaponID)
{
    if ((weaponID == _emptySlotWeaponName) && (CharacterHandleWeapon != null))
    {
        MMInventoryEvent.Trigger(MMInventoryEventType.UnEquipRequest, null, WeaponInventoryName, WeaponInventory.Content[0], 0, 0, PlayerID);
        CharacterHandleWeapon.ChangeWeapon(null, _emptySlotWeaponName, false);
        MMInventoryEvent.Trigger(MMInventoryEventType.Redraw, null, WeaponInventory.name, null, 0, 0, PlayerID);
        return;
    }

    if ((weaponID == _initialSlotWeaponName) && (CharacterHandleWeapon != null))
    {
        MMInventoryEvent.Trigger(MMInventoryEventType.UnEquipRequest, null, WeaponInventoryName, WeaponInventory.Content[0], 0, 0, PlayerID);
        CharacterHandleWeapon.ChangeWeapon(CharacterHandleWeapon.InitialWeapon, _initialSlotWeaponName, false);
        MMInventoryEvent.Trigger(MMInventoryEventType.Redraw, null, WeaponInventory.name, null, 0, 0, PlayerID);
        return;
    }

    for (int i = 0; i < MainInventory.Content.Length ; i++)
    {
        if (InventoryItem.IsNull(MainInventory.Content[i]))
        {
            continue;
        }
        if (MainInventory.Content[i].ItemID == weaponID)
        {
            MMInventoryEvent.Trigger(MMInventoryEventType.EquipRequest, null, MainInventory.name, MainInventory.Content[i], 0, i, PlayerID);
            break;
        }
    }
}
public virtual void EquipWeapon(string weaponID)

이거..

 

 

CharacterHandleWeapon.cs

여기서 설정한게 CharacterInventory보다 우선시 되는 듯

 

 

public virtual void Setup()
{
    _character = this.gameObject.GetComponentInParent<Character>();
    _characterGridMovement = _character?.FindAbility<CharacterGridMovement>();
    _weaponModels = new List<WeaponModel>();
    foreach (WeaponModel model in _character.gameObject.GetComponentsInChildren<WeaponModel>())
    {
        _weaponModels.Add(model);
    }
    CharacterAnimator = _animator;
    // filler if the WeaponAttachment has not been set
    if (WeaponAttachment == null)
    {
        WeaponAttachment = transform;
    }
    if ((_animator != null) && (AutoIK))
    {
        _weaponIK = _animator.GetComponent<WeaponIK>();
    }
    // we set the initial weapon
    if (InitialWeapon != null)
    {
        if (CurrentWeapon != null)
        {
            if (CurrentWeapon.name != InitialWeapon.name)
            {
                ChangeWeapon(InitialWeapon, InitialWeapon.WeaponName, false);    
            }
        }
        else
        {
            ChangeWeapon(InitialWeapon, InitialWeapon.WeaponName, false);    
        }
    }
}

public virtual void ChangeWeapon(Weapon newWeapon, string weaponID, bool combo = false)
{
    // if the character already has a weapon, we make it stop shooting
    if (CurrentWeapon != null)
    {
        CurrentWeapon.TurnWeaponOff();
        if (!combo)
        {
            ShootStop();
            if (_weaponAim != null) { _weaponAim.RemoveReticle(); }
            Destroy(CurrentWeapon.gameObject);
        }
    }

    if (newWeapon != null)
    {
        InstantiateWeapon(newWeapon, weaponID, combo);
    }
    else
    {
        CurrentWeapon = null;
    }

    if (OnWeaponChange != null)
    {
        OnWeaponChange();
    }
}

protected virtual void InstantiateWeapon(Weapon newWeapon, string weaponID, bool combo = false)
{
    if (!combo)
    {
        CurrentWeapon = (Weapon)Instantiate(newWeapon, WeaponAttachment.transform.position + newWeapon.WeaponAttachmentOffset, WeaponAttachment.transform.rotation);
    }

    CurrentWeapon.name = newWeapon.name;
    CurrentWeapon.transform.parent = WeaponAttachment.transform;
    CurrentWeapon.transform.localPosition = newWeapon.WeaponAttachmentOffset;
    CurrentWeapon.SetOwner(_character, this);
    CurrentWeapon.WeaponID = weaponID;
    CurrentWeapon.FlipWeapon();
    _weaponAim = CurrentWeapon.gameObject.MMGetComponentNoAlloc<WeaponAim>();

    HandleWeaponAim();

    // we handle (optional) inverse kinematics (IK) 
    HandleWeaponIK();

    // we handle the weapon model
    HandleWeaponModel(newWeapon, weaponID, combo, CurrentWeapon);

    // we turn off the gun's emitters.
    CurrentWeapon.Initialization();
    CurrentWeapon.InitializeComboWeapons();
    CurrentWeapon.InitializeAnimatorParameters();
    InitializeAnimatorParameters();
}

Setup() -> ChangeWeapon() -> InstantiateWeapon()

 

 

 

시작하자마자 아이템 장비하는 코드

/**
 * 초기아이템 바로 처먹기
 */

public class InitItem : ItemPicker
{
    protected override void Start()
    {
        base.Start();
        Pick(Item.TargetInventoryName, "Player1");
    }
}

오버라이드해서 구현

 

바닥에 있는거 주워먹는 ItemPicker랑 똑같음.

아무데나 넣어두면 자동작동

 

반응형

'Unity > TopdownEngine' 카테고리의 다른 글

DialogBox가 납작하게 나올 때 표시  (0) 2023.01.30
무기 변경  (0) 2023.01.30
MMEvnet 예제  (0) 2023.01.29
MMEvent 동작 원리  (0) 2023.01.29
데미지 숫자 표시  (0) 2023.01.28