So, you are entrusted with customizing something like a game under design patterns that were originally created only for enterprise software. Games are not corporate software by definition, and the reason many people avoid thinking about design patterns when developing games. This does not mean that it is not feasible, though.
My recommendations:
- Think about the model first: ask about your problem, how do you imagine it.
- Remember that this is still a game, so it must follow the patterns of game development: there is only one - the game cycle.
So, if you combine the two above (I would like the second not to be there), then I would design it this way (I mentioned the design template if my suggestions remind me of one):
- Mark current time point.
- The environment begins the cycle of the game.
- For each step of the cycle, calculate the elapsed time since the last moment. This will give you a time interval in some units (e.g. missed milliseconds).
- For a given time interval, you need to ask each object to update its state (conceptually, ask them - where would you be now if N milliseconds have passed?). This is a bit like a visitor style.
- After all the objects have updated their states, the environment will display the results on the screen (in real games this means that you need to draw the current state of the game - each object is redrawn on the screen, for your simple application you can check whether Fox reported that he caught a rabbit).
- Obviously, during the step of the cycle, you need to continue to mark the current time, so that the difference in time interval can be calculated at each step.
Step number 4 includes some difficulties, especially if the accuracy is critical. for example, what if the time span is about a second, and during that second (somewhere in the middle) the fox would catch a rabbit, but in the end there is still distance? this can happen if the speeds of the fox and the rabbit are functions of time (sometimes they slow down, sometimes they accelerate) [By the way, this sounds like a strategy template - a variation for calculating the current speed - for example, a linear and time function). Obviously, if the fox and the rabbit simply report their positions at the end of the time interval, a trapping moment will be missed, which is undesirable.
Here is how I would decide: for a given period of time, if it is more than one millisecond (suppose that a millisecond is the shortest acceptable atomic time for reasonably good accuracy), then divide it into time intervals of one millisecond each, and for each millisecond ask each object to update its state. After all, if an object can update its state based on a time interval, we can name it as many times as we want, with shorter periods of time. Obviously, the inevitable side effect is that you need to update the states in some order, but given that the millisecond is too short, it should be very good to do so.
The pseudo code would look like this:
var foxAndRabbitGame = new FoxAndRabbitGame(); foxAndRabbitGame.RunGame(screen);
class FoxAndRabbitGame { private fox = new Fox(Speed.Linear()); //strategy private rabbit = new Rabbit(Speed.Linear()); //strategy void RunGame(screen) { var currentTime = NOW; while (true) { var timePassed = NOW - currentTime; currentTime = NOW; foreach (millisecond in timePassed) { fox.UpdateState ( millisecond , rabbit ); rabbit.UpdateState ( millisecond, fox ); if (fox.TryBite(rabbit)) { //game over. return; } } //usually, drawing is much slower than calculating state, //so we do it once, after all calculations. screen.Draw(this); //visitor screen.Draw(Fox); //visitor screen.Draw(rabbit); //visitor } } }