game-programming-patterns icon indicating copy to clipboard operation
game-programming-patterns copied to clipboard

Comment on Commands page

Open sataaa opened this issue 8 years ago • 0 comments

In your Commands page (Commands , on Github) you go on about how to implement the undo() of a "unit->moveTo(x, y)", which would involve saving a state for the command. However, I think this can be avoided in this case.

If you, instead, stored the dx/dy of the movement in relation to where the unit is, it could be simple to implement execute() and undo().

For example, on the constructor, you could set dx as "x - unit->x" and dy as "y - unit->y".

By storing the difference of the positions, you can always cast it back to find where you were and where you will be just by adding or subtracting the dx and dy:

execute() {
  unit->moveTo(unit->x + dx, unit->y + dy);
}
undo() {
  unit->moveTo(unit->x - dx, unit->y - dy);
}

Of course this is just a minor comment on your Command example, please feel free to ignore this.

Edit: I noticed that there was some earlier comment about the state being set during the execution, in order to store what should be reversible only at that point, which makes perfect sense and covers up my argument on this post. Please feel free to disregard this even harder :)

sataaa avatar Apr 17 '17 18:04 sataaa