2014-10-20 85 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). 

乾杯。