Skip to content

Instantly share code, notes, and snippets.

@veryjos
Created May 21, 2018 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veryjos/ec395169fb1c1bd39db8737eaec3683c to your computer and use it in GitHub Desktop.
Save veryjos/ec395169fb1c1bd39db8737eaec3683c to your computer and use it in GitHub Desktop.
#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