NPC商戶¶
*** Welcome to ye Old Sword shop! ***
Things for sale (choose 1-3 to inspect, quit to exit):
_________________________________________________________
1. A rusty sword (5 gold)
2. A sword with a leather handle (10 gold)
3. Excalibur (100 gold)
這將引入 NPC 能夠出售東西。實際上,這意味著當您與他們互動時,您會看到一個_選單_選擇。 Evennia 提供 EvMenu 實用程式來輕鬆建立遊戲內選單。
我們會將商家的所有商品存放在他們的庫存中。這意味著他們可能站在實際的商店房間、市場或漫步在路上。 我們還將使用“黃金”作為示例貨幣。
要進入商店,你只需要站在同一個房間並使用buy/shop指令。
打造商人階級¶
商家將在他們面前回應您發出 shop 或 buy 指令。
# in for example mygame/typeclasses/merchants.py
from typeclasses.objects import Object
from evennia import Command, CmdSet, EvMenu
class CmdOpenShop(Command):
"""
Open the shop!
Usage:
shop/buy
"""
key = "shop"
aliases = ["buy"]
def func(self):
# this will sit on the Merchant, which is self.obj.
# the self.caller is the player wanting to buy stuff.
self.obj.open_shop(self.caller)
class MerchantCmdSet(CmdSet):
def at_cmdset_creation(self):
self.add(CmdOpenShop())
class NPCMerchant(Object):
def at_object_creation(self):
self.cmdset.add_default(MerchantCmdSet)
def open_shop(self, shopper):
menunodes = {} # TODO!
shopname = self.db.shopname or "The shop"
EvMenu(shopper, menunodes, startnode="shopfront",
shopname=shopname, shopkeeper=self, wares=self.contents)
我們也可以將指令放在單獨的模組中,但為了緊湊性,我們將其全部放在商家 typeclass 中。
請注意,我們將商家設為Object!由於我們沒有給他們任何其他指令,所以讓他們成為 Character 沒有意義。
我們製作一個非常簡單的 shop/buy 指令,並確保將其新增至商家自己的 cmdset 指令。
我們在 shopper 上初始化 EvMenu,但我們還沒有建立任何 menunodes,所以此時實際上不會做太多事情。重要的是,我們將 shopname、shopkeeper 和 wares 傳遞到選單中,這意味著它們將作為 EvMenu 例項上的屬性提供 - 我們將能夠從選單內部存取它們。
本店已開始營業!¶
確保reload。
讓我們透過在遊戲中建立商人和一些商品來嘗試。請記住,我們也必須創造一些黃金來推動經濟發展。
> set self/gold = 8
> create/drop Stan S. Stanman;stan:typeclasses.merchants.NPCMerchant
> set stan/shopname = Stan's previously owned vessles
> create/drop A proud vessel;ship
> set ship/desc = The thing has holes in it.
> set ship/gold_value = 5
> create/drop A classic speedster;rowboat
> set rowboat/gold_value = 2
> set rowboat/desc = It's not going anywhere fast.
請注意,無法存取 Python 程式碼的建構者現在只需使用遊戲內指令即可設定個人化商家。 商店設定完畢後,我們只需要在同一個房間就可以開始消費了!
> buy
*** Welcome to Stan's previously owned vessels! ***
Things for sale (choose 1-3 to inspect, quit to exit):
_________________________________________________________
1. A proud vessel (5 gold)
2. A classic speedster (2 gold)
> 1
You inspect A proud vessel:
The thing has holes in it.
__________________________________________________________
1. Buy A proud vessel (5 gold)
2. Look for something else.
> 1
You pay 5 gold and purchase A proud vessel!
*** Welcome to Stan's previously owned vessels! ***
Things for sale (choose 1-3 to inspect, quit to exit):
_________________________________________________________
1. A classic speedster (2 gold)