I'm writing the core logic of Pokemon Gen I in Java (8), and I am struggling to figure out the best way to implement the turn-based aspect of the game. Right now I have a Battle
class which has two Player
s each with a PokemonTeam
.
The idea is that one Player
takes a Turn
(a Battle
has many Turn
s), and some action happens, e.g. attack with a Pokemon
, use an Item
, switch Pokemon
, etc. Then, once that single action is taken, it needs to be applied during that turn, and then the other player gets to take their turn.
My original design, which has changed very slightly, looks something like this:
I was planning on implementing an Action
and ActionResult
superclass, with subclasses for each of the action types (e.g. AttackAction
and AttackActionResult
). But that seems like it would be a bit confusing to use, and responsibility of applying damage, effects, handling Pokemon fainting, etc. all gets stuffed into those subclasses, which seems like a bad idea.
Then I stumbled across the command pattern which seems to fit the bill - "a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time". Assuming good use of the pattern, I could have good delegation of applying actions, handling results, updating the battle state, etc. without turning classes into God classes.
Is the command pattern a good fit for a turn-based game like Pokemon, or is there something more appropriate?
Action
superclass and acommand
superclass (except the name, of course)?Action
type is pretty much aCommand
, as you mentioned - so really, my initial design was a sort-of command pattern. At first glance, the command pattern seemed really different to what I had, but I suppose that the naming threw me off. Would you recommend such a pattern for this kind of task? It seems to fit well, at least in my opinion, but I'm no expert!