Last active
June 10, 2016 20:39
-
-
Save NetzwergX/3a29e1b106c6bb9c7308e89dd715ee20 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* written by Sebastian Teumert in 2016. | |
* Released into Public Domain. No Rights Reserved. | |
*/ | |
#include "stdafx.h" | |
#include <string> | |
#include<map> | |
#include<vector> | |
#include<iostream> | |
class Component { | |
public: | |
// we need this, in Java we'd simply use getClass() | |
virtual std::string type() const = 0; | |
}; | |
class Attack : public Component { | |
public: | |
std::string type() const override { return std::string("mygame::components::Attack"); } | |
int attackValue = 0; | |
}; | |
class Defense : public Component { | |
public: | |
std::string type() const override { return std::string("mygame::components::Defense"); } | |
int defenseValue = 0; | |
}; | |
class WalkSpeed : Component { | |
public: | |
std::string type() const override { return std::string("mygame::components::WalkSpeed"); } | |
int speedBonus; | |
}; | |
class Tool { | |
private: | |
std::map<std::string, Component*> components; | |
public: | |
/** Adds a component to the tool */ | |
void addComponent(Component* component) { | |
components[component->type()] = component; | |
}; | |
/** Removes a component from the tool */ | |
void removeComponent(Component* component) { components.erase(component->type()); }; | |
/** Return the component with the given type */ | |
Component* getComponentByType(std::string type) { | |
std::map<std::string, Component*>::iterator it = components.find(type); | |
if (it != components.end()) { return it->second; } | |
return nullptr; | |
}; | |
/** Check wether a tol has a given component */ | |
bool hasComponent(std::string type) { | |
std::map<std::string, Component*>::iterator it = components.find(type); | |
return it != components.end(); | |
} | |
}; | |
class Player { | |
private: | |
int attack = 0; | |
int defense = 0; | |
int walkSpeed; | |
public: | |
std::vector<Tool*> tools; | |
std::vector<Tool*> getToolsByComponentType(std::string type) { | |
std::vector<Tool*> retVal; | |
for (Tool* tool : tools) { | |
if (tool->hasComponent(type)) { | |
retVal.push_back(tool); | |
} | |
} | |
return retVal; | |
} | |
void doAttack() { | |
int attackValue = this->attack; | |
int defenseValue = this->defense; | |
for (Tool* tool : this->getToolsByComponentType(std::string("mygame::components::Attack"))) { | |
Attack* component = (Attack*) tool->getComponentByType(std::string("mygame::components::Attack")); | |
attackValue += component->attackValue; | |
} | |
for (Tool* tool : this->getToolsByComponentType(std::string("mygame::components::Defense"))) { | |
Defense* component = (Defense*)tool->getComponentByType(std::string("mygame::components::Defense")); | |
defenseValue += component->defenseValue; | |
} | |
std::cout << "Attack with strength " << attackValue << "! Defend with strenght " << defenseValue << "!"; | |
} | |
void walkTo() { | |
int walkSpeed = this->walkSpeed; | |
for (Tool* tool : this->getToolsByComponentType(std::string("mygame::components:WalkSpeed"))) { | |
WalkSpeed* component = (WalkSpeed*)tool->getComponentByType(std::string("mygame::components::Defense")); | |
walkSpeed += component->speedBonus; | |
std::cout << "Walk with " << walkSpeed << std::endl; | |
} | |
} | |
}; | |
int main() | |
{ | |
Player p; | |
Tool* tool = new Tool(); | |
Attack* attack = new Attack(); | |
attack->attackValue = 15; | |
Defense* defense = new Defense(); | |
defense->defenseValue = 10; | |
tool->addComponent(attack); | |
tool->addComponent(defense); | |
p.tools.push_back(tool); | |
p.doAttack(); // prints "Attack with strength 15! Defend with strenght 10!" | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment