Unity/TopdownEngine

무기 변경

lipnus 2023. 1. 30. 22:44
반응형

UI Button을 눌렀을 때 무기가 바뀌는 원리

 

 

InputManager.cs

 

CharacterInventory.cs

protected override void HandleInput()
{
    if (!AbilityAuthorized
        || (_condition.CurrentState != CharacterStates.CharacterConditions.Normal))
    {
        return;
    }
    if (_inputManager.SwitchWeaponButton.State.CurrentState == MMInput.ButtonStates.ButtonDown)
    {
        SwitchWeapon ();
    }
}

InputManger클래스를 참조하고 있으면서 State에 따라 함수 호출

 

protected virtual void SwitchWeapon()
{
    // if there's no character handle weapon component, we can't switch weapon, we do nothing and exit
    if ((CharacterHandleWeapon == null) || (WeaponInventory == null))
    {
        return;
    }

    FillAvailableWeaponsLists ();

    // if we only have 0 or 1 weapon, there's nothing to switch, we do nothing and exit
    if (_availableWeaponsIDs.Count <= 0)
    {
        return;
    }

    DetermineNextWeaponName ();
    EquipWeapon (_nextWeaponID);
    PlayAbilityStartFeedbacks();
    PlayAbilityStartSfx();
}

 

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;
        }
    }
}

 

 

CharacterHandleWeapon.cs

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();
    }
}

 

반응형