游戏的OO设计:在房间之间移动玩家

时间:2014-10-20 15:21:52

标签: java oop

我对OO设计很陌生,我在制作游戏时遇到了一些问题:

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

目前,会议室控制着玩家。但是通过这种设计,我不确定是否可以在房间之间移动播放器,这是我想要做的。

然后我想在Player类中有一个Room currentRoom,但我觉得我也有这个问题。此外,玩家没有房间,所以这似乎不是OO。

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

1 个答案:

答案 0 :(得分:5)

+1:"带有当前项目的收集"软件设计模式非常普遍。


你的游戏中有几个房间。

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

然后,Game"管理"或"拥有" Rooms

当一个对象"管理"或"拥有"其他对象,负责分配和解除分配("创建"和#34;破坏"内存中的对象)。


可是:

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.中,许多物体可以与其他物体相关, 但是,只有一个对象可以是"的管理者,(和#34;同时与#34;相关),另一个对象。

Room(s)和Game都有一些关系, 或者与Player(s)关联,但是,只有一个对象, 可以是"经理"他们。

因为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

让我们用Inventory替换Items字词:

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).

干杯。