Created
May 21, 2018 03:36
-
-
Save veryjos/ec395169fb1c1bd39db8737eaec3683c 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
#include "Component.hpp" | |
#include <iostream> | |
using namespace tdrp; | |
class ShitComponent : public Component { | |
public: | |
COMPONENT_ENABLE(ShitComponent) | |
float x; | |
}; | |
class PositionComponent : public Component { | |
public: | |
COMPONENT_ENABLE(PositionComponent) | |
float x; | |
float y; | |
private: | |
}; | |
class SpriteComponent : public Component { | |
public: | |
COMPONENT_ENABLE(SpriteComponent) | |
std::string spriteName; | |
std::weak_ptr<PositionComponent> m_positionComponent; | |
void Initialize(ComponentEntity& owner) override { | |
// Ensure co-requisite position component exists | |
m_positionComponent = owner.RequireComponent<PositionComponent>(); | |
}; | |
private: | |
}; | |
void Move(const ComponentEntity& ent) { | |
// Only render things that have a PositionComponent | |
ent.IfMatchComponents( | |
[] (PositionComponent& position) { | |
position.x = 100; | |
position.y = 200; | |
} | |
); | |
} | |
void Render(const ComponentEntity& ent) { | |
// Only render things that have a SpriteComponent and PositionComponent | |
ent.IfMatchComponents( | |
[] (SpriteComponent& sprite, PositionComponent& position) { | |
std::cout << "I'm rendering " << sprite.spriteName << " at " << position.x << " : " << position.y << std::endl; | |
} | |
); | |
} | |
int main(int argc, char* argv[]) { | |
ComponentEntity ent; | |
auto spriteComponent = ent.AddComponent<SpriteComponent>(); | |
{ | |
spriteComponent.lock()->spriteName = "hey little bitch"; | |
} | |
Move(ent); | |
Render(ent); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment