如何实现游戏的库存系统?

时间:2022-10-18 14:30:10

How would I go about implementing an inventory system for a game in PHP? I've been trying to figure out a way so that items can be dynamically used instead of just sitting in your inventory, e.g. equipping weapons, using potions, teleporting, etc.

我如何在PHP中实现游戏的库存系统?我一直试图找出一种方法,以便动态使用商品,而不是只是坐在你的库存中,例如装备武器,使用魔药,传送等

The only solution I can come up with is to store PHP code in the database, and fetch it when the item is used, but that doesn't really seem appropriate. Is there a better way to implement this?

我能想到的唯一解决方案是将PHP代码存储在数据库中,并在使用该项时获取它,但这似乎并不合适。有没有更好的方法来实现这个?

To clarify: It's an mmo browser game, and the inventory of each player needs to persist through the game. Having a class for each item (There may be hundreds of items) seems difficult to maintain if any changes were to be made.

澄清:这是一个mmo浏览器游戏,每个玩家的库存需要在游戏中持续存在。如果要进行任何更改,每个项目的课程(可能有数百个项目)似乎很难维护。

2 个解决方案

#1


In addition to implementing a 'baseItem' class as suggested, you may also want to considering developing an interface class for outlining the methods that all items should have. Then, each type of item could implement these methods differently.

除了按照建议实现'baseItem'类之外,您可能还需要考虑开发一个接口类来概述所有项目应该具有的方法。然后,每种类型的项目可以不同地实现这些方法。

For example, all items might need to have an 'equip()' method, but implement it differently based on which kind of items it is. You may not need to create a class for each individual item, but you should probably create classes which are capable of creating unique instances of items based on some sort of data structure you provide (which can be stored in the database).

例如,所有项目可能需要具有“装备()”方法,但根据它的类型实现不同的方法。您可能不需要为每个单独的项创建一个类,但您应该创建能够根据您提供的某种数据结构(可以存储在数据库中)创建项的唯一实例的类。

A really general example might be:

一个非常普遍的例子可能是:

class Leather_Armor extends Armor_Base implements Equippable
{
     // these would probably be defined in the base class instead of here
     protected $_itemName;
     protected $_itemDescription;
     protected $_defenseRating;

     public function __construct(params)
     {
         // intialize properties with data from the db
     }


     // the Equippable interface requires us to define this function
     public function equip()
     {
        // call a function from the base class
        $this->recalculateDefenseRating($this->defenseRating)
     }

}

You should probably read up on interfaces and abstract classes to fully get the idea.

您应该阅读接口和抽象类以完全理解。

Also, be aware that this is a really broad, open-ended question than can be approached a thousand different ways. You might want to ask something more specific, or provide concrete examples.

此外,请注意,这是一个非常广泛,开放式的问题,而不是千方百计的问题。您可能想要提出更具体的内容,或提供具体示例。

#2


Using PHP you'll need to set up an inventory class, and classes for each of your items - including a "baseItem" class that items inherit from which includes things common to all items (icon? sound? amount?).

使用PHP,您需要设置一个库存类,以及每个项目的类 - 包括项目继承的“baseItem”类,其中包括所有项目共有的内容(icon?sound?amount?)。

You'll need to store the user's instance of the inventory class in a session variable. If you want it to persist it should be in a database.

您需要将用户的库存类实例存储在会话变量中。如果您希望它持久化,它应该在数据库中。

Any page that accesses the inventory will need to reference the file that contains the item and inventory classes.

访问清单的任何页面都需要引用包含项目和库存类的文件。

This link shows setting an object to a session and accessing it across pages.

此链接显示将对象设置为会话并跨页面访问它。

EDIT: typed too fast and mixed up a couple key terms

编辑:键入太快并混合了几个关键术语

#1


In addition to implementing a 'baseItem' class as suggested, you may also want to considering developing an interface class for outlining the methods that all items should have. Then, each type of item could implement these methods differently.

除了按照建议实现'baseItem'类之外,您可能还需要考虑开发一个接口类来概述所有项目应该具有的方法。然后,每种类型的项目可以不同地实现这些方法。

For example, all items might need to have an 'equip()' method, but implement it differently based on which kind of items it is. You may not need to create a class for each individual item, but you should probably create classes which are capable of creating unique instances of items based on some sort of data structure you provide (which can be stored in the database).

例如,所有项目可能需要具有“装备()”方法,但根据它的类型实现不同的方法。您可能不需要为每个单独的项创建一个类,但您应该创建能够根据您提供的某种数据结构(可以存储在数据库中)创建项的唯一实例的类。

A really general example might be:

一个非常普遍的例子可能是:

class Leather_Armor extends Armor_Base implements Equippable
{
     // these would probably be defined in the base class instead of here
     protected $_itemName;
     protected $_itemDescription;
     protected $_defenseRating;

     public function __construct(params)
     {
         // intialize properties with data from the db
     }


     // the Equippable interface requires us to define this function
     public function equip()
     {
        // call a function from the base class
        $this->recalculateDefenseRating($this->defenseRating)
     }

}

You should probably read up on interfaces and abstract classes to fully get the idea.

您应该阅读接口和抽象类以完全理解。

Also, be aware that this is a really broad, open-ended question than can be approached a thousand different ways. You might want to ask something more specific, or provide concrete examples.

此外,请注意,这是一个非常广泛,开放式的问题,而不是千方百计的问题。您可能想要提出更具体的内容,或提供具体示例。

#2


Using PHP you'll need to set up an inventory class, and classes for each of your items - including a "baseItem" class that items inherit from which includes things common to all items (icon? sound? amount?).

使用PHP,您需要设置一个库存类,以及每个项目的类 - 包括项目继承的“baseItem”类,其中包括所有项目共有的内容(icon?sound?amount?)。

You'll need to store the user's instance of the inventory class in a session variable. If you want it to persist it should be in a database.

您需要将用户的库存类实例存储在会话变量中。如果您希望它持久化,它应该在数据库中。

Any page that accesses the inventory will need to reference the file that contains the item and inventory classes.

访问清单的任何页面都需要引用包含项目和库存类的文件。

This link shows setting an object to a session and accessing it across pages.

此链接显示将对象设置为会话并跨页面访问它。

EDIT: typed too fast and mixed up a couple key terms

编辑:键入太快并混合了几个关键术语