Skip to content

Instantly share code, notes, and snippets.

@yuiwong
Last active July 31, 2018 14:59
Show Gist options
  • Select an option

  • Save yuiwong/b66839bb9dd2dc7fc04c223fc39e2da6 to your computer and use it in GitHub Desktop.

Select an option

Save yuiwong/b66839bb9dd2dc7fc04c223fc39e2da6 to your computer and use it in GitHub Desktop.

Revisions

  1. yuiwong revised this gist Jul 31, 2018. 2 changed files with 139 additions and 0 deletions.
    46 changes: 46 additions & 0 deletions Inventory.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    using System.Collections.Generic;

    public abstract class Inventory
    {
    protected Creature m_Owner;
    protected List<Item> m_Items;


    public virtual void Init(Creature owner)
    {
    m_Items = new List<Item>();
    m_Owner = owner;
    }

    public abstract bool CanHandleIncomingItem(Item item);

    public Creature GetOwner()
    {
    return m_Owner;
    }

    public virtual bool HandleIncomingItem(Item item)
    {
    if (!CanHandleIncomingItem(item))
    return false;

    m_Items.Add(item);
    item.AquiredByInventory(this);
    return true;
    }

    public List<UsableItem> GetItemWithFlag(int flag)
    {
    List<UsableItem> result = new List<UsableItem>();
    foreach (var item in m_Items)
    {
    var usableItem = item as UsableItem;
    if (usableItem != null && usableItem.itemConfig.itemFlags.Contains(flag))
    {
    result.Add(usableItem);
    }
    }
    return result;
    }

    }
    93 changes: 93 additions & 0 deletions ItemPickup.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    using UnityEngine;
    using UnityEngine.AI;

    // represent an object creature can pickup.
    public class ItemPickup : MonoBase, IInteract
    {
    protected Collider m_MyCollider;
    protected Transform m_DisplayTrans;
    protected UIElement m_MyHeader;
    protected Item m_StoredItem;
    protected NavMeshObstacle m_NavMeshObstacle;
    protected ItemForm m_ItemForm;

    protected override void AwakeImp()
    {
    base.AwakeImp();
    m_MyCollider = GetComponent<Collider>();
    m_NavMeshObstacle = transform.parent.GetComponent<NavMeshObstacle>();
    m_MyCollider.isTrigger = true;
    }

    protected override void StartImp()
    {
    base.StartImp();
    // UI Display
    m_MyHeader = UiMgr.instance.CreateUIElement<UIElement>(GetType().ToString());
    m_MyHeader.SetFollowObj(m_StoredItem.gameObject, BaseDataMgr.instance.commonConfig.pickupWorldOffset);
    m_MyHeader.AddText(m_StoredItem.transform.name, m_StoredItem.transform.name, new Vector2(200f, 32f), Vector2.zero);
    UpdateForm();
    }

    protected override void OnDestroyIml()
    {
    base.OnDestroyIml();
    m_MyHeader.SelfDestroy();
    }

    public void InitPickup(ItemForm itemForm, Item storedItem, Transform displayTrans)
    {
    m_ItemForm = itemForm;
    m_StoredItem = storedItem;
    m_DisplayTrans = displayTrans;
    }

    public void ChangeForm(ItemForm targetForm)
    {
    m_ItemForm = targetForm;
    UpdateForm();
    }

    protected void UpdateForm()
    {
    if (m_ItemForm == ItemForm.pickup)
    {
    m_MyHeader.Show();
    m_MyCollider.enabled = true;
    m_DisplayTrans.gameObject.SetActive(true);
    if (m_NavMeshObstacle != null)
    m_NavMeshObstacle.enabled = true;
    }
    else
    {
    m_MyHeader.Hide();
    m_MyCollider.enabled = false;
    m_DisplayTrans.gameObject.SetActive(false);
    if (m_NavMeshObstacle != null)
    m_NavMeshObstacle.enabled = false;
    }
    }

    #region interface
    public bool CanInteractNow()
    {
    return m_ItemForm == ItemForm.pickup;
    }

    public int GetInteractionType()
    {
    return E.pickup;
    }

    public void ProceedInteract(Creature creature)
    {
    if (m_ItemForm == ItemForm.inventory)
    {
    //if proceed get called twice maybe Generic Action only need one anchor for pick something up.
    Debug.LogWarning("This item is already in inventory, something is wrong! item name = " + m_StoredItem.GetType());
    return;
    }
    creature.inventory.AquireItem(m_StoredItem);
    }
    #endregion
    }
  2. yuiwong created this gist Jul 31, 2018.
    65 changes: 65 additions & 0 deletions UsableItem.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    //represent an item in creature's inventory.
    public abstract class UsableItem : Item, IListenToAction
    {
    protected ButtonState m_ButtonState;
    protected int m_CurrentActionIndex;

    public Action myAction { get; protected set; }

    protected override void StartImp()
    {
    base.StartImp();
    m_ActiveState = false;
    }

    #region Item state
    protected override void OnAquiredByInventory()
    {
    base.OnAquiredByInventory();
    myAction.SetOwner(owner);
    }

    protected override void OnDroppedByInventory()
    {
    base.OnDroppedByInventory();
    myAction.SetOwner(null);
    m_ButtonState = ButtonState.empty;
    }
    #endregion

    #region item trigger

    /// <summary>
    /// state represent button state, or something else. maybe used in certain item.
    /// for example, continues attack.
    /// </summary>
    public void UseAction(ButtonState buttonState, int actionIndex = 0)
    {
    if (m_ActiveState)
    {
    m_ButtonState = buttonState;
    m_CurrentActionIndex = actionIndex;
    InternalUseAction(actionIndex, m_ButtonState);
    }
    }

    protected abstract void InternalUseAction(int actionIndex, ButtonState buttonState);
    #endregion


    #region IUseAction Impl
    public virtual void OnActionBegin() { }

    public virtual void OnActionAbort(ActionAbortType actionAbortType) { }

    // @Note: all override method must use base.OnReachEvent(...)
    public virtual void OnReachEvent(int evtId, string param1, string param2, string param3)
    {
    if (evtId == E.costStamina)
    {
    owner.action.EmitConsumeStamina(param1.EvtToFloat());
    //Debug.Log("Emitting consume stamina signal!");
    }
    }
    #endregion
    }