我对OO设计很陌生,我在设计游戏时遇到了一些问题:
游戏中有一系列房间.每个房间都有一系列玩家和库存.每个库存都有一个项目集合.
所以目前,会议室控制着玩家.但是通过这种设计,我不确定是否可以在房间之间移动播放器,这就是我想做的事情.
然后我想Room currentRoom在Player课程中有一个,但我觉得我也有这个问题.此外,玩家没有房间,所以这似乎不是很好.
关于如何设计我的游戏的任何提示?谢谢!
+1:"使用当前项目收集"软件设计模式非常常见.
你的游戏中有几个房间.
The `Game` has a collection of `Rooms`.
Run Code Online (Sandbox Code Playgroud)
然后,Game"管理"或"拥有" Rooms.
当一个对象"管理"或"拥有"其他对象时,负责分配和释放(内存中对象的"创建"和"破坏").
但:
Each `room` has a collection of `Players` and an `Inventory`.
Run Code Online (Sandbox Code Playgroud)
等待.你忘记了:
The `Game` has a collection of `Players`.
Run Code Online (Sandbox Code Playgroud)
和:
Each `room` has a collection of `Players`.
Run Code Online (Sandbox Code Playgroud)
等等,Game也有同样的集合Players.
小心"有"字.
在OOP中,许多对象可以与其他对象相关,但是,只有一个对象可以是"管理者",(和"同时","同时"),另一个对象.
Room(s)和(s)都与(s)Game有某种关系或关联Player,但是,只有一个对象,可以是它们的"管理者".
因为,a Player总是一部分Game,但是,可以留下一个,当前,Room......
...然后在房间可以引用同一集合Players,比Game,但不会"管理"他们.
因此,让我们将之前的声明更改为:
The `Game` manages a collection of `Players`.
Each `Room` relates to a collection of `Players`.
Run Code Online (Sandbox Code Playgroud)
现在:
Each `room` has (a collection of | ) an `Inventory`.
Run Code Online (Sandbox Code Playgroud)
然后,每个Room"管理"或"拥有" Inventory.
让我们来更换Inventory用字Items:
Each `room` has a collection of `item`s, called an `Inventory`
Run Code Online (Sandbox Code Playgroud)
所以:
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`.
Run Code Online (Sandbox Code Playgroud)
问题是"有"字.意味着关联,有时"管理"/"拥有"对象,有时意味着"关联但不管理"对象.
最后:
Each `Room` manages to a collection of `Items`, also called `Inventory`.
Run Code Online (Sandbox Code Playgroud)
但是,如果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).
Run Code Online (Sandbox Code Playgroud)
干杯.