Last active
January 3, 2016 21:38
-
-
Save rioki/8522753 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
class Entity | |
{ | |
private: | |
void add_component(Component* c) | |
{ | |
components.push_back(c); | |
c->entity = this; | |
} | |
void remove_component(Component* c) | |
{ | |
auto i = std::find(components.begin(), components.end(), c); | |
if (i != componets.end()) | |
{ | |
c->entity = NULL; | |
components.erase(i); | |
} | |
} | |
const std::vector<Component*>& get_components() | |
{ | |
return components; | |
} | |
std::vector<const Component*> get_components() const | |
{ | |
return std::vector<const Component*>(components.begin(), components.end()); | |
} | |
private: | |
std::vector<Component*> components; | |
}; | |
class Component | |
{ | |
public: | |
protected: | |
Entity* entity; | |
friend class Entity; | |
}; | |
class TransformComponent : public Component | |
{ | |
// here is get and set | |
public: | |
Matrix44 tansform; | |
}; | |
class ModelComponent : public Component | |
{ | |
public: | |
// here is get and set | |
private: | |
Material material; | |
Mesh mesh; | |
}; | |
class Node | |
{ | |
public: | |
virtual void execute() = 0; | |
}; | |
class RenderNode : public Node | |
{ | |
public: | |
RenderNode(GraphicSystem* g, TransformComponent* t, ModelComponent* m) | |
: graphic(g), transform(t), model(m) {} | |
void execute() | |
{ | |
const Matrix44& t = transform->get_transform(); | |
const Material& ma = model->get_material(); | |
const Mesh& me = model->get_mesh(); | |
graphic->enqueue(t, ma, me) | |
} | |
private: | |
GraphicSystem* graphic | |
TransformComponent* transform; | |
ModelComponent* model; | |
}; | |
class System | |
{ | |
public: | |
System(Engine* e) | |
: engine(e) {} | |
virtual void step() = 0; | |
virtual void on_entity_add(Entity* entity) = 0; | |
protected: | |
Engine* engine; | |
}; | |
class GraphicSystem | |
{ | |
public: | |
GraphicSystem(Engine* e) | |
: engine(e) | |
{ | |
// create graphic context and stuff | |
} | |
virtual void step() | |
{ | |
// process render queues | |
// swap frames | |
} | |
void enqueue(const Matrix44& t, const Material& ma, const Mesh& me) | |
{ | |
// add object to render queue | |
} | |
virtual void on_entity_add(Entity* entity) | |
{ | |
TransformComponent* transform = NULL; | |
ModelComponent* model = NULL; | |
const std::vector<Component*>& components = entity->get_components(); | |
for (size_t i = 0; i < components.size(); i++) | |
{ | |
if (transform == NULL) | |
{ | |
transform = dynamic_cast<TransformComponent*>(components[i]); | |
} | |
if (model == NULL) | |
{ | |
model = dynamic_cast<ModelComponent*>(components[i]); | |
} | |
} | |
if (transform != NULL && model != NULL) | |
{ | |
engine->add_node(new RenderNode(this, transform, model)); | |
} | |
} | |
}; | |
class Engine | |
{ | |
public: | |
void step() | |
{ | |
for (size_t i = 0; i < nodes.size(); i++) | |
{ | |
nodes[i]->execute(); | |
} | |
for (size_t i = 0; i < system.size(); i++) | |
{ | |
system[i]->step(); | |
} | |
} | |
private: | |
std::vector<System*> system; | |
std::vector<Entity*> entities; | |
std::vector<Nodes*> nodes; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment