2014-10-20 68 views
2

之间移动的球员,我很新的面向对象的设计,我有设计游戏中的几个问题:OO设计:房间

游戏有客房的集合。 每个房间都有一系列玩家和库存。 每个库存都有一个项目集合。

所以目前,房间已经控制了玩家。但是通过这种设计,我不确定是否可以在各个房间之间移动玩家,这正是我想要做的。

然后我想在Player类中有一个Room currentRoom,但我觉得我也有这个问题。另外,玩家没有空间,所以这看起来不太OO。

关于如何设计我的游戏的任何提示?谢谢!

+0

您应该有一个Room类,并根据需要从Room类创建多个对象。房间的数量。然后,您应该创建尽可能多的Player对象。然后在你的房间对象中,你应该有一些列表当前在那个房间里的球员...... – brso05 2014-10-20 15:25:22

+0

这已经被建模。下面是一个非常古老的在线游戏平台如何做的例子:http://www.hayseed.net/MOO/manuals/ProgrammersManual.html#SEC6 – 2014-10-20 15:26:22

+2

当玩家从房间移动到另一个房间时,请移除来自他们所在房间的玩家,并将他们添加到新房间。 – brso05 2014-10-20 15:26:34

回答

5

+1:“具有当前项目的集合”软件设计模式非常常见。


您的游戏中有多个房间。

The `Game` has a collection of `Rooms`. 

然后,Game “管理” 或 “拥有” Rooms

当一个对象“管理”或“拥有”其他对象时,负责分配和释放(对内存中对象的“创建”和“销毁”)。


但是:

Each `room` has a collection of `Players` and an `Inventory`. 

等待。你忘了:

The `Game` has a collection of `Players`. 

和:

Each `room` has a collection of `Players`. 

等待中,Game,也有Players相同的集合。

小心“有”字。

在O.O.P.中,许多对象可以与其他对象相关联,但是,只有一个对象可以是“管理者”(和“同时”关联)另一个对象。

两者,所述Room(一个或多个)和Game,有一定的关系, 或伴随Player(一个或多个),但是,只有一个对象, 可以是它们中的“经理”。

因为,一个Player总是Game的一部分,但是, 可以留下一个,当前,Room ...

...然后在房间可以引用Players同一集合, 比Game,但不“管理”它们。


因此,让我们改变了以前的声明为:

The `Game` manages a collection of `Players`. 
Each `Room` relates to a collection of `Players`. 

现在:

Each `room` has (a collection of |) an `Inventory`. 

然后,每个Room “管理” 或 “拥有” Inventory

让我们Items更换Inventory字:

Each `room` has a collection of `item`s, called an `Inventory` 

所以:

The `Game` manages a collection of `Rooms`. 
The `Game` manages a collection of `Players`. 
Each `Room` relates to a collection of `Players`. 
Each `Player` relates to a single, current `Room`. 

的问题是 “有” 字。意味着联想,有时,“管理”/“拥有”对象,有时意味着“涉及但不管理”一个对象。


而且,最后:

Each `Room` manages to a collection of `Items`, also called `Inventory`. 

但是,如果Player可以采取Items,与他/她, 和改变Room(S),并且每个Player,可能下降的Item, 纳入Room,就像放下一把枪,并拿一把斧头。

然后事情会变得有点混乱。

可以说item“可以位于”中,而不是“具有”room

所以,每个player可以涉及到的Items的集合,并且,每个 可以room涉及到的Items, 对象的集合,该Items被“管理”,由Game

Each `Player` can relate to a collection of `Item` (s). 
Each `Room` can relate to a collection of `Item` (s). 
Each `Item`, maybe related to a `Room`, 
can be located in a `Room`, but, not always. 
Each `Item`, maybe related to a `Player`, but, not always. 
Each `Item` is part of an universe called the `Game`. 
So, the `Game` "manages" all the `Item` (s). 

干杯。