Hi, I have a question re object-oriented design - I have two classes, a Map (in effect an x,y grid) and Items placed on the map. The Map holds all the items and their locations and controls the program flow. The Items have some intelligence and determine if the wish to move around on the map.
For instance the map code may look like:
class Map
{
...
Items[,] map;
public void DoItemUpdate()
{
...
for (x = 0 to MaxX)
{
for (y = 0 to MaxY)
{
map[x,y].UpdateItem();
}
}
}
}
And the Item code may look like:
class Items
{
...
Vector2 location;
public void UpdateItem()
{
// determine if we want to move the unit
....
// if yes, move the unit
this.Move(fromXY, toXY);
}
public void Move(Vector2 from, Vector2 to)
{
// what goes in here - raise event, use reference to Map class?
}
}
Basically what I'm trying to work out is how the move code should be structured so that it asks the map if the move is legal, and then registers the move in the map.
I have thought of a couple of approaches
- Pass a reference to the parent Map class to the Item and call a function from there
- Use a delegate / event structure to "callback" the map
Any comments on these approaches - what is the best/cleanest/most appropriate for OO code? Thanks!