Created
February 1, 2013 13:14
-
-
Save luizdepra/4691231 to your computer and use it in GitHub Desktop.
A pattern idea using mixins to develop game engines in C++.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
// Entity Base Class | |
class Entity | |
{ | |
protected: | |
int _x; | |
int _y; | |
public: | |
void setX(int value) | |
{ | |
_x = value; | |
} | |
void setY(int value) | |
{ | |
_y = value; | |
} | |
int getX() | |
{ | |
return _x; | |
} | |
int getY() | |
{ | |
return _y; | |
} | |
}; | |
// Health Mixin | |
template <class Base> | |
class Health : public Base | |
{ | |
protected: | |
int _health; | |
int _maxHealth; | |
public: | |
void setHealth(int initialHealth, int maxHealth) | |
{ | |
_health = initialHealth; | |
_maxHealth = maxHealth; | |
} | |
int getHealth() | |
{ | |
return _health; | |
} | |
void increaseHealth(int value) | |
{ | |
if (_health + value > _maxHealth) | |
_health = _maxHealth; | |
else | |
_health += value; | |
} | |
void decreaseHealth(int value) | |
{ | |
if (_health - value < 0) | |
_health = 0; | |
else | |
_health -= value; | |
} | |
}; | |
// Mana Mixin | |
template <class Base> | |
class Mana : public Base | |
{ | |
protected: | |
int _mana; | |
int _maxMana; | |
public: | |
void setMana(int initialMana, int maxMana) | |
{ | |
_mana = initialMana; | |
_maxMana = maxMana; | |
} | |
int getMana() | |
{ | |
return _mana; | |
} | |
void increaseMana(int value) | |
{ | |
if (_mana + value > _maxMana) | |
_mana = _maxMana; | |
else | |
_mana += value; | |
} | |
void decreaseMana(int value) | |
{ | |
if (_mana - value < 0) | |
_mana = 0; | |
else | |
_mana -= value; | |
} | |
}; | |
// 4-Way Movement Mixin | |
template <class Base> | |
class FourWayMovement : public Base | |
{ | |
public: | |
void moveUp(int steps = 1) | |
{ | |
_y -= steps; | |
} | |
void moveDown(int steps = 1) | |
{ | |
_y += steps; | |
} | |
void moveLeft(int steps = 1) | |
{ | |
_x -= steps; | |
} | |
void moveRight(int steps = 1) | |
{ | |
_x += steps; | |
} | |
}; | |
// The Player | |
class Player : public FourWayMovement<Health<Mana<Entity> > > | |
{ | |
// Player logic | |
public: | |
string whoami() | |
{ | |
return "I'm a Player!"; | |
} | |
}; | |
// A Monster | |
class Monster : public FourWayMovement<Health<Entity> > | |
{ | |
// Monster logic | |
public: | |
string whoami() | |
{ | |
return "I'm a Monster!"; | |
} | |
}; | |
// A Rock | |
class Rock : public FourWayMovement<Entity> | |
{ | |
// Rock logic | |
public: | |
string whoami() | |
{ | |
return "I'm a Rock!"; | |
} | |
}; | |
int main(int argc, char **argv) | |
{ | |
Rock rock; | |
Monster monster; | |
Player player; | |
cout << rock.whoami() << endl; | |
rock.setX(1); | |
rock.setY(10); | |
rock.moveDown(5); | |
cout << "I'm at X=" << rock.getX() << " Y=" << rock.getY() << endl << endl; | |
cout << monster.whoami() << endl; | |
monster.setX(15); | |
monster.setY(21); | |
monster.moveRight(3); | |
cout << "I'm at X=" << monster.getX() << " Y=" << monster.getY() << endl; | |
monster.setHealth(10, 10); | |
monster.decreaseHealth(3); | |
cout << "My health is " << monster.getHealth() << endl << endl; | |
cout << player.whoami() << endl; | |
player.setX(5); | |
player.setY(5); | |
player.moveUp(1); | |
player.moveRight(1); | |
cout << "I'm at X=" << player.getX() << " Y=" << player.getY() << endl; | |
player.setHealth(15, 25); | |
player.increaseHealth(5); | |
cout << "My health is " << player.getHealth() << endl; | |
player.setMana(10, 20); | |
player.increaseMana(2); | |
cout << "My mana is " << player.getMana() << endl << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment