Related Posts Plugin for WordPress, Blogger...

5-17 Vendor Panel

今天要來製作商店功能嘍!玩家要購買物品時,可以用滑鼠右鍵點擊商店的物品,也可以將背包的物品丟到商店內,藉以賣掉物品。首先,我們來複製Chest Panel當作Vendor Panel吧。


然後將多餘的Slot刪除,因為我們需要建立新的Vendor Slot,是專屬給商店用的Slot。

然後,將原本的Slot.cs刪除。

改成新的VendorSlot.cs。這個Slot的修改模式和EquipmentSlot很相似,大家也可以參考以前的文章唷:
5-15 Create Character Panel And Wear Equipment

VendorSlot.cs的完整程式碼如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace RPG.Inventory{
 public class VendorSlot : Slot {
  public override void OnPointerDown(PointerEventData eventData){
   if (eventData.button == PointerEventData.InputButton.Right
       && inventorySystem.isPickedItem == false && transform.childCount > 0) {
    Item clickedItem = transform.GetChild (0).GetComponent ().Item;
    inventorySystem.BuyItem (clickedItem);

   } else if (eventData.button == PointerEventData.InputButton.Left
            && inventorySystem.isPickedItem == true) {
    if (Input.GetKey (KeyCode.Z)) {
     // 壓住Z鍵時,一次只販售一個
     inventorySystem.SellItem (1);
    } else {
     // 販售全部物品
     inventorySystem.SellItem (inventorySystem.GetPickedItem().Amount);
    }
   }
  }
 }
}

接著再請大家將新的VendorSlot依序排好,如下圖。

接著要在背包內加入金錢的機制,在Knapsack Panel新增UI/Image,取名為Coin。

Coin的話請大家自己找圖片唷,然後在Coin底下新增UI/Text。

介面的調整大致上如下圖。

然後來修改一下InventorySystem.cs,讓他有處理買賣商店物品的功能,同時也要初始化商店的物品。
InventorySystem.cs:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI;
using UnityEngine.EventSystems;

namespace RPG.Inventory{
 public class InventorySystem : MonoBehaviour {

  [SerializeField] ToolTip toolTip;
  [SerializeField] ItemUI pickedItem;
  [SerializeField] CharacterProperty characterProperty;

  public bool isPickedItem = false;
  ItemList itemList;
  Slot[] slotList;

  int coinAmount = 100;
  Text coinText;

  void Start () {
   ParseItemsJson ();
   slotList = GetComponentsInChildren ();
   coinText = GameObject.Find ("Coin").GetComponentInChildren();
   coinText.text = coinAmount.ToString();
   InitialVendorItem ();
  }

  void Update(){
   PressGToAddItem ();
   MovePickedItemByMousePosition ();
   DiscardPickedItem ();
  }

  private bool ConsumeCoin(int amount){
   if (coinAmount > 0) {
    coinAmount -= amount;
    coinText.text = coinAmount.ToString ();
    return true;
   }
   return false;
  }

  private void EarnCoin(int amount){
   coinAmount += amount;
   coinText.text = coinAmount.ToString ();
  }

  public void BuyItem(Item item){
   bool isSuccess = ConsumeCoin (item.BuyPrice);
   if (isSuccess) {
    StoreItem (item);
   }
  }

  public void SellItem(int sellAmount){
   int sellPrice = pickedItem.Item.SellPrice * sellAmount;
   ReducePickedItem (sellAmount);
   EarnCoin (sellPrice);
  }

  public void PickupItem(Item item, int amount){
   pickedItem.SetItem (item, amount);
   pickedItem.Show ();
   toolTip.Hide ();
   isPickedItem = true;
  }

  public void ReducePickedItem(int amount){
   pickedItem.ReduceAmount(amount);
   if (pickedItem.Amount <= 0) {
    isPickedItem = false;
    pickedItem.Hide ();
   }
  }

  public void WearingEquipment(ItemUI equipmentToWear){
   if ((equipmentToWear.Item is Equipment) == false) {
    return;
   }

   foreach(Slot slot in slotList){
    EquipmentSlot equipmentSlot = (slot as EquipmentSlot);
    if(equipmentSlot && equipmentSlot.EquipmentTypeIsEqual (equipmentToWear.Item)){
     if (equipmentSlot.transform.childCount > 0) {
      // 人物已配戴裝備了,將裝備與背包的裝備交換
      equipmentSlot.GetComponentInChildren ().ExchangeItem(equipmentToWear);
     } else {
      // 人物未配戴裝備,直接穿上
      equipmentSlot.StoreItem (equipmentToWear.Item);
      // 銷毀背包內的裝備
      Destroy (equipmentToWear.gameObject);
     }
     break;
    }
   }
   UpdatePropertyText ();
  }

  public ItemUI GetPickedItem(){
   return pickedItem;
  }

  public Slot[] GetSlotList(){
   return slotList;
  }

  public void ShowToolTip(string content){
   toolTip.Show (content);
  }

  public void HideToolTip(){
   toolTip.Hide ();
  }

  public void UpdatePropertyText(){
   characterProperty.UpdatePropertyText ();
  }

  public bool StoreItem(int id){
   Item item = GetItemByID (id);
   return StoreItem (item);
  }

  public bool StoreItem(Item item){
   if (item == null) {
    return false;
   }

   // 若物品的儲存容量為1,則將該物品直接放進空的Slot
   if (item.Capacity == 1) {
    return StoreItemInEmptySlot (item);
   }
   // 將相同的Item放在同一個Slot
   return StoreItemInSameSlot(item);
  }

  private bool StoreItemInSameSlot(Item item){
   foreach (Slot slot in slotList) {
    // 避開VendorSlot
    if (slot is VendorSlot) {
     continue;
    }
    if (slot.transform.childCount >= 1 && slot.GetItemID () == item.ID &&
     slot.IsFilled () == false) {
     // 將新Item與同一個Item放在一起
     slot.StoreItem (item);
     return true;
    }
   }
   // 若背包內不存在相同的Item,則放進空的Slot
   return StoreItemInEmptySlot (item);
  }

  private bool StoreItemInEmptySlot(Item item){
   foreach (Slot slot in slotList) {
    // 避開VendorSlot
    if (slot is VendorSlot) {
     continue;
    }
    if (slot.transform.childCount == 0) {
     // 將Item存進該Slot
     slot.StoreItem (item);
     return true;
    }
   }
   Debug.LogError ("No Empty Slot.");
   return false;
  }

  private void PressGToAddItem(){
   // 測試程式碼,手動生成物品
   if (Input.GetKeyDown (KeyCode.G)) {
    StoreItem (Random.Range(1,17));
   }
  }

  private void InitialVendorItem(){
   Item[] vendorItemList = new Item[10];
   // 手動初始化要放入商店的物品
   vendorItemList [0] = GetItemByID (1);
   vendorItemList [1] = GetItemByID (2);
   vendorItemList [2] = GetItemByID (3);
   vendorItemList [3] = GetItemByID (4);
   vendorItemList [4] = GetItemByID (5);
   vendorItemList [5] = GetItemByID (6);
   vendorItemList [6] = GetItemByID (7);
   vendorItemList [7] = GetItemByID (8);
   vendorItemList [8] = GetItemByID (1);
   vendorItemList [9] = GetItemByID (2);
   foreach (Item item in vendorItemList) {
    foreach (Slot slot in slotList) {
     // 限定Slot類型為VendorSlot
     VendorSlot vendorSlot = (slot as VendorSlot);
     // 如果同類型物品疊加在一起
     if (vendorSlot && slot.transform.childCount >= 1 
      && slot.GetItemID () == item.ID &&
      slot.IsFilled () == false) {
      slot.StoreItem (item);
      break;
     } // 如果沒有同類物品,則放入空Slot
     else if (vendorSlot && slot.transform.childCount == 0) {
      slot.StoreItem (item);
      break;
     }
    }
   }
  }

  private void MovePickedItemByMousePosition(){
   if (isPickedItem) {
    // 將滑鼠座標轉換成Canvas上的座標
    Vector2 position;
    Canvas canvas = GetComponentInParent ();
    RectTransformUtility.ScreenPointToLocalPointInRectangle (
     canvas.transform as RectTransform,
     Input.mousePosition,
     null,
     out position);
    pickedItem.SetLocalPosition (position);
   }
  }

  private void DiscardPickedItem(){
   // 處理物品丟棄
   // IsPointerOverGameObject(-1)判斷滑鼠左鍵是否有碰到GameObject
   if (pickedItem && Input.GetMouseButtonDown (0) 
    && EventSystem.current.IsPointerOverGameObject(-1) == false) {
    // TODO 跟Slot.cs的PickupAllSlotItem相衝突,若用DestroyImmediate會導致此方法被呼叫
    isPickedItem = false;
    pickedItem.Hide ();
   }
  }

  private Item GetItemByID(int id){
   foreach (Item entity in itemList.ConsumableEntityList) {
    if (entity.ID == id) {
     return entity;
    } 
   }
   foreach (Item entity in itemList.EquipmentEntityList) {
    if (entity.ID == id) {
     return entity;
    } 
   }
   foreach (Item entity in itemList.MaterialEntityList) {
    if (entity.ID == id) {
     return entity;
    } 
   }
   return null;
  }

  private void ParseItemsJson(){
   // 從Resource資料夾中讀取Items.json
   TextAsset json = Resources.Load ("Items");
   // 解析Json格式
   itemList = JsonUtility.FromJson (json.text);
  }
 }
}

最後,執行遊戲看看吧,可以在商店中買賣物品了,金錢也會跟著變化。



留言