onsdag, augusti 23, 2006

During a design pattern lecture I recognized a similarity between the "Strategy" pattern and a certain aspect of the videogame "Command & Conquer-Generals"© by Electronic Arts.

Command & Conquer-Generals is a real time strategy game where three factions struggle for peace and/or world domination. This is done by building up a base and producing more units. Each faction also has a unique tech-centre which provides additional upgrades for your units and can also provide other bonuses.

What I'm going to try here is to breakdown one of theese techcentres to some kind of c code using the strategy pattern.

One of the three factions is the USA and their techcenter is called "Strategy center". When built it can provide one of three different bonuses to all the units the player controlls.
We can say that the building has (or provides) a strategy. The three strategies are "hold the line" (more armor) , "Seek & Destroy" (longer range) and "Bombardment" (more firepower). You can only use 1 strategy at a given time but you are free to change to another whenever you wish. When you change to another strategy your units will have to be signaled by radio, laserbeam or a homing pigeon using the overridden signalStrategy method making them unavailable for new orders for a second.



int main(int argc, char **argv)
{
//build the StrategyCenter building
StrategyCenter sc = new StrategyCenter();

//Start by selecting the Seek&Destroy strategy
sc->useStrategy(new SeekNDestStrat());

//Signal the troops
sc->signalTroops();

return 0;
}

This could output:
> "Construction complete"
> " *toggle to Seek & Destroy Strategy* "
> "Seek & Destroy !"

So there we have it, now we just need an 3D engine and we have a C&C clone :)