Link to github: https://github.com/coffee-enthusiast/microIkariam
This project is a micro version of Ikariam, an old online strategy game.
As a player you build towns, you gather as much resources you can. There are also other players that you live with in islands and you can attack them and steal their resources. So you have to train army both to attack and defend other players. The choice is yours.
First, the game runs in real time so there has to be a clock timer always running and calculating the passed time. Time is used for gathering resources, traveling to locations and in war. The time passed is calculated in each loop by a Simulate() member method of Player class.
void Player::Simulate()
{
double seconds = difftime(time(NULL), lastModified) - difftime(lastUpdate, lastModified);
for (auto t = myTowns.begin(); t != myTowns.end(); t++)
{
(*t)->Simulate(seconds, &myResearchPoints, &myGold);
}
lastModified = time(NULL);
lastUpdate = time(NULL);
}
Then each town of the player simulates. The gold paid by free citizens, research points produced by scientists and resources gathered by workers.
void Town::Simulate(double seconds, double *rP, double *g)
{
*g += ((double)citizensFree * 3 * seconds) / 3600;
myWorkers->Simulate(seconds, myResources, g);
myScientists->Simulate(seconds, rP, g);
cout << "Gold: " << *g << ", ResearchP: " << *rP << endl;
cout << "Citizens: " << citizens << "(" << citizensFree << ")" << endl;
}
Resources of the game that can get gathered are: Wood, Crystal, Marble, Sulfur, etc. This is the implementation of resource class I made which can be reused and easily extended regardless of any change to the actual resources.
enum ResourceType {
Wood = 0,
Marble,
Sulfur,
Crystal,
Wine
};
class Resource
{
ResourceType type;
float amount;
public:
Resource();
Resource(ResourceType t, float a);
bool operator<=(Resource other);
bool operator+(float value);
bool operator-(float value);
float getAmount();
ResourceType getType();
};
class Resources
{
public:
// 0:wood, 1:marble, 2:sulfur, 3:crystal, 4:wine
Resource allResources[5] = { Resource() };
Resources();
Resources(Resource r1);
Resources(Resource r1, Resource r2);
Resources(Resource r1, Resource r2, Resource r3, Resource r4, Resource r5);
void operator+(Resource other);
bool operator<=(Resources other);
void operator+(Resources other);
void operator-(Resources other);
void toString();
};