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的完整程式碼如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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<itemui> ().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);
    }
   }
  }
 }
}
</itemui>

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

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

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

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

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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<slot> ();
   coinText = GameObject.Find ("Coin").GetComponentInChildren<text>();
   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<itemui> ().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<canvas> ();
    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<textasset> ("Items");
   // 解析Json格式
   itemList = JsonUtility.FromJson <itemlist>(json.text);
  }
 }
}
</itemlist></textasset></canvas></itemui></text></slot>

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



留言