I've been reading a lot about design patterns, but in using these patterns there's one question that I keep coming back to. How should my entities access information about each other?
Let's say I'm using a simple pattern where each object updates and draws itself:
class Monster extends GameEntity {
public void update(double delta) {}
public void draw(Graphics g) {}
}
I'm writing my update loop, but I realize that for Monster requires knowledge about where the player is, where cities on the main map are, or some other random piece of data.
How does Monster get at this? Do I initialize it with a reference to every possible thing it might need?
class Monster extends GameEntity {
public Monster(Player player, City[] cityList) {
this.player = player;
this.cityList = cityList;
}
}
That looks pretty messy. Or do I have a main Manager of sorts, which has access to everything, and pass that to everything?
class Monster extends GameEntity {
public Monster(GiantGlobalManager manager) {
this.manager = manager;
}
public void update(double delta) {
cities = this.manager.getCities();
}
}
Or perhaps have the manager as singleton? We could also break these managers into their areas of concern.
class Monster extends GameEntity {
public void update(double delta) {
cities = CityManager.getInstance().getCities();
player = PlayerManager.getInstance().getPlayer();
guiManager.getInstance().createMenu();
}
}
But this still doesn't feel quite right. Now I'm having to maintain all these managers, and it seems wrong for my objects to be making all these static calls. Second, many of these objects should not have access to everything provided by these classes. Letting everything access everything feels like a cop-out to me.
Whatever patterns I'm using - whether I'm breaking entities into components, or just moving my drawing and update loops to separate objects - I still need some way for them to communicate. What is a good way to go about this? What am I missing here?
Should this all be done through events? But getting a list of cities is not really an event, it's just data the monster needs.