Can’t sleep at night cus of this action mess that looked promosing at the start… let me explain.
I have an action object for each ‘action’ in my game like selecting a unit, moving from a to b, Attacking,etc
Each action has a doAction and undoAction method.
The idea for this was to allow to go back 1 step in a undo list, this worked using the swing Undomanager.
An example of that
Before the action is executed I set the (first) clicked tile in session, actions are stored in a map by name:
session.setClick(1,tile)
Action selectAction = actions.get("Select")
selectAction.doAction()
if selectAction.canUndo()
undoManager.add(selectAction)
selectAction.java
/**
* Select a unit and make it the active unit in the game. This is the first action
* for a unit.
*/
public class SelectAction extends AbstractAction {
private MapRenderer mapRenderer;
private InGameSession inGameSession;
private Game game;
public SelectAction(Game game, MapRenderer mapRenderer, InGameSession inGameSession) {
super("Select");
this.game = game;
this.inGameSession = inGameSession;
this.mapRenderer = mapRenderer;
}
public void doAction() {
selectUnit((Unit) inGameSession.getClick(1).getUnit());
inGameSession.setMode(InGameSession.MODE.UNIT_SELECT);
}
private void selectUnit(Unit selectedUnit) {
game.setActiveUnit(selectedUnit);
mapRenderer.setActiveUnit(selectedUnit);
mapRenderer.removeZones();
mapRenderer.showMoveZone();
mapRenderer.showArrows(true);
}
public void undoAction() {
deselectActiveUnit();
inGameSession.setMode(InGameSession.MODE.DEFAULT);
}
private void deselectActiveUnit() {
game.setActiveUnit(null);
mapRenderer.setActiveUnit(null);
mapRenderer.removeZones();
mapRenderer.showArrows(false);
}
}
Now I want to replay the actions that happened in the game.
But I always reuse the same Action, I don’t create new action objects… Action objects have a lot of constructor parameters and I want to execute actions @ places that don’t have access to these objects.
Also the actions get their input from a session object limiting it’s use. a MoveAction can only move from session tile 1 to session tile 2.
Following the Command pattern an Action can’t have setters, it should get all information from the constructor parameters that’s why I used the session object.
When I would save a replay it probably look like
Select 0 0
Move 0 0 11
Select 2 2
…
Every game has a replay system, how do they do it? & how can I use Action objects to replay all the actions.