1

I've been working on a game in my spare time. I'm pretty much done defining the primitives and until today everything was pretty well segmented and encapsulated but now it's come time to implement path finding (and other ways for the objects to interact with each other). Here path finding requires at minimum a knowledge of the map, a knowledge of where units are in the map and which types of terrain are passable by the unit whose path is being found.

There are three ways I've thought to implement this

  • Implement it in some high level class that knows about all of these things already (i.e. a game state object)
  • Implement it in some specific method inside one of the "involved" classes (like unit or map) and pass in all relevant state
  • Implement it in some function which is completely separate (because no state needs to be retained) and again pass in all of the relevant data

I want to get your opinion on which of these makes sense (or if there are others I haven't thought of). I like The first one the most because it only couples the objects at a high level and doesn't require exposing objects to state they don't (otherwise) need to know about. I don't like number 2 because it doesn't do those things and number 3 I'm neutral on at this point; it doesn't seem necessarily better or worse than number 1.

If number 2 is in fact the winner I have concerns about how much complexity will be propagated up to this high level game class.

1 Answer 1

3

I would go for #3 in an ideal world, simply due to Single Responsibility Principle. One class does pathfinding, not pathfinding and game state. This keeps it easy to change the path finding strategy as well as testing it in isolation (and in an ideal world, reuse it for different games).

That said, the interface you expose to work with game state (and map info, unit info, etc.) tends to be very query based. Ask for something, get something. Pathfinding tends to need to have everything and then manipulate it to some degree. In the real world, you may need a back door interface to get the pathfinder what it needs or to go with #1.

Remember, a done game is always better than a non-functioning well-designed game.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.