NPCs 對你的出現做出反應

> north 
------------------------------------
Meadow
You are standing in a green meadow. 
A bandit is here. 
------------------------------------
Bandit gives you a menacing look!

本教學展示了 NPC 物件的實現,該物件響應輸入的字元 位置。

我們需要的是以下內容:

  • 當有人進入時可以做出反應的NPC typeclass。

  • 自訂房間typeclass,可以告訴NPC有人進入。

  • 我們也會稍微調整預設的Character typeclass。

# in mygame/typeclasses/npcs.py  (for example)

from typeclasses.characters import Character

class NPC(Character):
    """
    A NPC typeclass which extends the character class.
    """
    def at_char_entered(self, character, **kwargs):
        """
        A simple is_aggressive check.
        Can be expanded upon later.
        """
        if self.db.is_aggressive:
            self.execute_cmd(f"say Graaah! Die, {character}!")
        else:
            self.execute_cmd(f"say Greetings, {character}!")

這裡我們對NPC˙做了一個簡單的方法,稱為at_char_entered。我們期望當(玩家)角色進入房間時呼叫它。我們實際上並沒有預先設定is_aggressive Attribute;我們將其留給管理員在遊戲中啟動。如果沒有設定,NPC 就是非敵對的。

每當_something_進入Room時,它的at_object_receive鉤子就會被呼叫。所以我們應該重寫它。

# in mygame/typeclasses/rooms.py

from evennia import utils

# ... 

class Room(ObjectParent, DefaultRoom):

    # ... 
    
    def at_object_receive(self, arriving_obj, source_location, **kwargs):
        if arriving_obj.account: 
            # this has an active acccount - a player character
            for item in self.contents:
                # get all npcs in the room and inform them
                if utils.inherits_from(item, "typeclasses.npcs.NPC"):
                    item.at_char_entered(arriving_obj, **kwargs)

目前操縱的角色將附加一個 .account。我們用它來知道到達的東西是一個角色。然後,我們使用 Evennia 的 utils.inherits_from 輔助實用程式來取得房間中的每個 NPC 可以使用他們新建立的 at_char_entered 方法。

確保reload

讓我們建立一個 NPC 並使其具有攻擊性。在本範例中,我們假設您的名字是“Anna”,並且您目前位置的北邊有一個房間。

> create/drop Orc:typeclasses.npcs.NPC
> north 
> south 
Orc says, Greetings, Anna!

現在讓獸人變得更具攻擊性。

> set orc/is_aggressive = True 
> north 
> south 
Orc says, Graah! Die, Anna!

這是一個容易激怒的獸人!