我们的游戏世界(背包【仓库】,交易,任务,简单经济系统,装备)实现(基于仙剑demo聊聊游戏世界)第一篇谈谈交易
一直不知道應該把下一步我們要更新的代碼怎么歸類,在網上看了下這個分類,我覺得這個完全是針對玩家體驗的,比如說,裝備系統,(背包)倉庫系統,交易系統這些都是相輔相成的,分開真的好嗎?而這里的世界系統僅僅涉及一個場景跳轉,我想問,強化,倉庫,交易這些不都發生在游戲世界中嗎,為啥說他們是獨立的呢,就因為看到的不一樣,其實這些都是游戲世界中的一個部分,說白了,他們只是游戲經濟系統中的一個部分,因為這些東西都可以在游戲虛擬世界中貨幣化,要靠游戲中提供的經濟模型來支撐,當然單機游戲里面經濟模型較為簡單,因為不需要考慮貨幣通脹等等問題(這個要放大的話就要寫好多字,我大學主修的就是經濟學【第二學位修的計算機】,建立一個經濟數學模型,我自己沒有信心搞出來,所以我就不多說話了),我們就我們的仙劍demo,來簡單看看游戲中的這些東西代碼怎么寫吧,如果做網游的話,那么就讓數值策劃去擔心游戲經濟模型,我們這里就不考慮了。
首先,我們看看游戲中的交易代碼是怎么實現的。
先看看效果吧,看看游戲里是什么樣的。
對話結束后,進入實質的購買界面
可以看到左邊的商店物品列表
右邊是自身的包裹,這個是怎么實現的呢?我們看下李鐵匠身上綁定的腳本
我們看下這就是npc李鐵匠,上面的AI腳本,我們前面講過了,這里我們只需要看npcsetup和shopitemlist,npcsetup是一個設置人物npc交互的腳本
我們可以根據自己游戲的需要自己來修改,如左邊,這個本來就不是重點,重點我們看看交易過程是什么樣的
/// <summary> /// Npc shop. /// This script use to create a shop to sell item /// </summary>using UnityEngine; using System.Collections; using System.Collections.Generic;public class ShopItemlist : MonoBehaviour {public List<int> itemID = new List<int>();void Start(){if(this.gameObject.tag == "Untagged")this.gameObject.tag = "Npc_Shop";}}shopitemlist腳本我們一看,只有短短的這么幾行,那我們的交易是如何實現的呢?
我們看這個shopItemList的引用
在gui_menu.cs中
//Check on NPC Shopif(hit.collider.tag == "Npc_Shop" && !CheckHoverInventory() && !CheckHoverEquipment() && !CheckHoverSkillWindow() && CheckHoverSlotHotkey() == -1 && itemShop[0].openItemShop == false){OpenItemShopWindow();itemShop[0].itemID = hit.collider.GetComponent<ShopItemlist>().itemID;}我們看,hit.collider.tag這個我們前面鼠標點選的時候就說明了,這個攝像機發射一條射線,與物體相交的檢測,我們利用這個方法還做了‘水果忍者’游戲,其實這一句就是把我們在李鐵匠身上定義的物品列表賦值給我們真正的shop代碼的itemshop數組,那商店怎么畫出來的呢,我們看代碼 private void ShowItemShopWindow(){if(itemShop[0].openItemShop){if(useStyle){GUI.SetNextControlName("ItemShopWindow"); itemShop[0].windowItemShopRect = GUI.Window(itemShop[0].windowItemShopID, itemShop[0].windowItemShopRect, FunctionItemShopWindow, itemShop[0].nameItemShopWindow, guiStyleGroup[0].windowItemShopStyle);}else{GUI.SetNextControlName("ItemShopWindow");itemShop[0].windowItemShopRect = GUI.Window(itemShop[0].windowItemShopID, itemShop[0].windowItemShopRect, FunctionItemShopWindow, itemShop[0].nameItemShopWindow);}if(Input.GetKeyDown(KeyCode.Escape)){itemShop[0].openItemShop = false; }}}就是這么畫出來的,上面就是一個u3d提供的話窗體的方法,我們的商店就是這么畫出來的,當然畫出來,里面要有賣東西的列表,這個列表在哪呢?看FunctionItemShopWindow方法 [HideInInspector]public Vector2 scrollPositionShop;/*** Shopdraw*/private void FunctionItemShopWindow(int windowID){GUI.DragWindow(new Rect(0,0,250,60));itemShop[0].scrollViewRect.height = (((itemShop[0].itemID.Count-1) * itemShop[0].slotItemRect.height) * itemShop[0].space) + itemShop[0].offset.y + (itemShop[0].slotItemRect.height/2 + itemShop[0].offset.y/2);scrollPositionShop = GUI.BeginScrollView(itemShop[0].scrollViewPosition , scrollPositionShop, itemShop[0].scrollViewRect);for(int i = 0; i < itemShop[0].itemID.Count; i++){if(useStyle){GUI.Box(new Rect(itemShop[0].slotItemRect.x + itemShop[0].offset.x, itemShop[0].slotItemRect.y + ((i * itemShop[0].slotItemRect.height) * itemShop[0].space) + itemShop[0].offset.y, itemShop[0].slotItemRect.width, itemShop[0].slotItemRect.height), new GUIContent("","itemShopWindow"+(i).ToString()), guiStyleGroup[0].itemShopSlotStyle);GUI.Box(new Rect(itemShop[0].descriptionRect.x + itemShop[0].offset.x, itemShop[0].descriptionRect.y + ((i * itemShop[0].descriptionRect.height) * itemShop[0].space) + itemShop[0].offset.y, itemShop[0].descriptionRect.width, itemShop[0].descriptionRect.height), new GUIContent("","itemShopWindow"+(i).ToString()), guiStyleGroup[0].descriptionItemShopStyle);GUI.Box(new Rect(itemShop[0].itemNameRect.x + itemShop[0].offset.x, itemShop[0].itemNameRect.y + ((i * itemShop[0].itemNameRect.height) * itemShop[0].spaceItemName) + itemShop[0].offset.y, itemShop[0].itemNameRect.width, itemShop[0].itemNameRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).item_Name ,"itemShopWindow"+(i).ToString()), guiStyleGroup[0].itemShopItemNameStyle);GUI.Box(new Rect(itemShop[0].itemTypeRect.x + itemShop[0].offset.x, itemShop[0].itemTypeRect.y + ((i * itemShop[0].itemTypeRect.height) * itemShop[0].spaceItemType) + itemShop[0].offset.y, itemShop[0].itemTypeRect.width, itemShop[0].itemTypeRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).item_Type ,"itemShopWindow"+(i).ToString()), guiStyleGroup[0].itemShopItemTypeStyle);GUI.Box(new Rect(itemShop[0].itemDescriptionRect.x + itemShop[0].offset.x, itemShop[0].itemDescriptionRect.y + ((i * itemShop[0].itemDescriptionRect.height) * itemShop[0].spaceItemDescription) + itemShop[0].offset.y, itemShop[0].itemDescriptionRect.width, itemShop[0].itemDescriptionRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).description ,"itemShopWindow"+(i).ToString()), guiStyleGroup[0].itemShopDescriptionStyle);GUI.Box(new Rect(itemShop[0].priceRect.x + itemShop[0].offset.x, itemShop[0].priceRect.y + ((i * itemShop[0].priceRect.height) * itemShop[0].spacePrice) + itemShop[0].offset.y, itemShop[0].priceRect.width, itemShop[0].priceRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).price.ToString() ,"itemShopWindow"+(i).ToString()), guiStyleGroup[0].itemShopPriceStyle);if(GUI.Button(new Rect(itemShop[0].buttonBuyRect.x + itemShop[0].offset.x, itemShop[0].buttonBuyRect.y + ((i * itemShop[0].buttonBuyRect.height) * itemShop[0].spaceButtonBuy) + itemShop[0].offset.y, itemShop[0].buttonBuyRect.width, itemShop[0].buttonBuyRect.height), new GUIContent("BUY","itemShopWindow"+(i).ToString()), guiStyleGroup[0].itemShopButtonBuyStyle)){if(itemShop[0].itemID[i] != null && showDropAmount == false){item_pickup = new Bag();item_pickup.item_id = itemShop[0].itemID[i];item_pickup.isItem = true;item_pickup.equipmentType = Item_Data.instance.Get_Item(itemShop[0].itemID[i]).equipment_Type;isBuyItem = true;//amountItemDrop = "1";showDropAmount = true;}}}else{GUI.Box(new Rect(itemShop[0].slotItemRect.x + itemShop[0].offset.x, itemShop[0].slotItemRect.y + ((i * itemShop[0].slotItemRect.height) * itemShop[0].space) + itemShop[0].offset.y, itemShop[0].slotItemRect.width, itemShop[0].slotItemRect.height), new GUIContent("","itemShopWindow"+(i).ToString()));GUI.Box(new Rect(itemShop[0].descriptionRect.x + itemShop[0].offset.x, itemShop[0].descriptionRect.y + ((i * itemShop[0].descriptionRect.height) * itemShop[0].space) + itemShop[0].offset.y, itemShop[0].descriptionRect.width, itemShop[0].descriptionRect.height), new GUIContent("" ,"itemShopWindow"+(i).ToString()));GUI.Box(new Rect(itemShop[0].itemNameRect.x + itemShop[0].offset.x, itemShop[0].itemNameRect.y + ((i * itemShop[0].itemNameRect.height) * itemShop[0].spaceItemName) + itemShop[0].offset.y, itemShop[0].itemNameRect.width, itemShop[0].itemNameRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).item_Name ,"itemShopWindow"+(i).ToString()));GUI.Box(new Rect(itemShop[0].itemTypeRect.x + itemShop[0].offset.x, itemShop[0].itemTypeRect.y + ((i * itemShop[0].itemTypeRect.height) * itemShop[0].spaceItemType) + itemShop[0].offset.y, itemShop[0].itemTypeRect.width, itemShop[0].itemTypeRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).item_Type ,"itemShopWindow"+(i).ToString()));GUI.Box(new Rect(itemShop[0].itemDescriptionRect.x + itemShop[0].offset.x, itemShop[0].itemDescriptionRect.y + ((i * itemShop[0].itemDescriptionRect.height) * itemShop[0].spaceItemDescription) + itemShop[0].offset.y, itemShop[0].itemDescriptionRect.width, itemShop[0].itemDescriptionRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).description ,"itemShopWindow"+(i).ToString()));GUI.Box(new Rect(itemShop[0].priceRect.x + itemShop[0].offset.x, itemShop[0].priceRect.y + ((i * itemShop[0].priceRect.height) * itemShop[0].spacePrice) + itemShop[0].offset.y, itemShop[0].priceRect.width, itemShop[0].priceRect.height), new GUIContent(Item_Data.instance.Get_Item(itemShop[0].itemID[i]).price.ToString() ,"itemShopWindow"+(i).ToString()));if(GUI.Button(new Rect(itemShop[0].buttonBuyRect.x + itemShop[0].offset.x, itemShop[0].buttonBuyRect.y + ((i * itemShop[0].buttonBuyRect.height) * itemShop[0].spaceButtonBuy) + itemShop[0].offset.y, itemShop[0].buttonBuyRect.width, itemShop[0].buttonBuyRect.height), new GUIContent("BUY","itemShopWindow"+(i).ToString()))){if(itemShop[0].itemID[i] != null && showDropAmount == false){item_pickup = new Bag();item_pickup.item_id = itemShop[0].itemID[i];item_pickup.isItem = true;item_pickup.equipmentType = Item_Data.instance.Get_Item(itemShop[0].itemID[i]).equipment_Type;isBuyItem = true;//amountItemDrop = "1";showDropAmount = true;}}}if(Item_Data.instance.Get_Item(itemShop[0].itemID[i]) != null){GUI.DrawTexture(new Rect(itemShop[0].slotItemRect.x + itemShop[0].offset.x, itemShop[0].slotItemRect.y + ((i * itemShop[0].slotItemRect.height) * itemShop[0].space) + itemShop[0].offset.y, itemShop[0].slotItemRect.width, itemShop[0].slotItemRect.height), Item_Data.instance.Get_Item(itemShop[0].itemID[i]).item_Img); }}if(pickupStay){float maxSpacePos = (itemShop[0].scrollViewRect.height - scrollPositionShop.y) / (itemShop[0].slotItemRect.height * itemShop[0].space);Vector3 mousePos = Input.mousePosition;if(itemImg_Pickup != null){GUI.DrawTexture(new Rect((mousePos.x - itemShop[0].scrollViewPosition.x)-20-itemShop[0].windowItemShopRect.x, (Screen.height - (mousePos.y + itemShop[0].scrollViewPosition.y - itemShop[0].valueTuningIconFollowMouse))-20-itemShop[0].windowItemShopRect.y + (scrollPositionShop.y - maxSpacePos* (-1*(itemShop[0].slotItemRect.height * itemShop[0].space/100))), 40,40),itemImg_Pickup); }}GUI.EndScrollView();ShowDescriptionBag(itemShop[0].windowItemShopRect , id_Hover);ShowDescriptionBag(itemShop[0].windowItemShopRect , id_Hover);if(useStyle){GUI.Box(itemShop[0].moneyRect,money.ToString(),guiStyleGroup[0].goldStyle);if(GUI.Button(itemShop[0].buttonCancelRect,"",guiStyleGroup[0].itemShopCancel)){itemShop[0].openItemShop = false;}}else{GUI.Box(itemShop[0].moneyRect,money.ToString());if(GUI.Button(itemShop[0].buttonCancelRect,"X")){itemShop[0].openItemShop = false;} }GUI.DrawTexture(itemShop[0].coinIconRect, coinIcon);}哇,好多,代碼雖然多,其實不難理解,用u3d自帶api也好,用ngui編輯界面再畫也罷,實現效果其實都差不多,?itemShop這個是我們在李鐵匠身上設定的shop列表,根據這個列表,我們用for循環畫出賣的所有item,當買這個button確定之后,方法中觸發這樣一個方法 if(itemShop[0].itemID[i] != null && showDropAmount == false){item_pickup = new Bag();item_pickup.item_id = itemShop[0].itemID[i];item_pickup.isItem = true;item_pickup.equipmentType = Item_Data.instance.Get_Item(itemShop[0].itemID[i]).equipment_Type;isBuyItem = true;//amountItemDrop = "1";showDropAmount = true;}
就是我們的拾取物品代碼,我們可以直接以上面這幾行為例,綁在一個物品上,做場景中的礦石或草藥(我們在demo已經加了這方面野外物品拾取,不過做的方式類似于寶箱)獲取。就這樣,我們的shop就畫出來了。
總結
以上是生活随笔為你收集整理的我们的游戏世界(背包【仓库】,交易,任务,简单经济系统,装备)实现(基于仙剑demo聊聊游戏世界)第一篇谈谈交易的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 免费空间2015大主机免费的时代
- 下一篇: autobahn-python的使用——