我正在尝试为在线游戏(教育项目,而不是商业项目)找到一个好的设计模式。
假设我的数据库中有一张玩家表和一张地图表。所以实体框架自动生成了这些模型类:
public partial class Map
{
public Map()
{
this.Players = new HashSet<Player>();
}
public int MapID { get; set; }
public string Name { get; set; }
public virtual ICollection<Player> Players { get; set; }
}
public partial class Character
{
public int CharacterID { get; set; }
public string Name { get; set; }
public int MapID { get; set; }
public virtual Map Map { get; set; }
}
现在我需要一个“控制器”播放器和地图类,它们将保存状态信息(如地图上的位置):
public class MapController
{
private Map _map;
}
public class PlayerContoller
{
private Player _player;
private int _locationX;
private int _locationY;
}
现在我想向 PlayerController 添加一个 MapController 属性(这将返回玩家所在地图的 MapController)。
我可以通过使用获取 Map 对象,this._player.Map
但现在要找到 MapContoller,我必须实现我自己的 MapControllers 列表并搜索该列表。就像是:
public class PlayerContoller
{
private Player _player;
private int _locationX;
private int _locationY;
public MapController MapController
{
get { globalListOfMapControllers.Search(this._player.Map); }
}
}
这有点丑陋。我想有一个更好的方法来找出它,这已经为我实现了。(就像 Player.Map 一样)此外,它是一个冗余搜索,因为Player.Map
已经使用Player.MapID
.
任何人都可以为这样的系统想出更好的设计吗?
谢谢你,布格尔。