Advertisement
Lynix

Nazara - Entity Component System

Mar 30th, 2015
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. struct PositionComponent : public Ndk::Component<PositionComponent>
  2. {
  3.     NzVector3f position;
  4.  
  5.     static Ndk::ComponentIndex ComponentIndex;
  6. };
  7.  
  8. Ndk::ComponentIndex PositionComponent::ComponentIndex = Ndk::BaseComponent::Register<PositionComponent>("NdkPos");
  9.  
  10. struct VelocityComponent : public Ndk::Component<VelocityComponent>
  11. {
  12.     NzVector3f velocity;
  13.  
  14.     static Ndk::ComponentIndex ComponentIndex;
  15. };
  16.  
  17. Ndk::ComponentIndex VelocityComponent::ComponentIndex = Ndk::BaseComponent::Register<VelocityComponent>("NdkVel");
  18.  
  19. struct TestComponent : public Ndk::Component<TestComponent>
  20. {
  21.     NzString name = "Hello world";
  22.  
  23.     static Ndk::ComponentIndex ComponentIndex;
  24. };
  25.  
  26. Ndk::ComponentIndex TestComponent::ComponentIndex = Ndk::BaseComponent::Register<TestComponent>("NdkTest");
  27.  
  28. class VelocitySystem : public Ndk::System<VelocitySystem>
  29. {
  30.     public:
  31.         VelocitySystem()
  32.         {
  33.             Requires<PositionComponent, VelocityComponent>();
  34.             Excludes<TestComponent>();
  35.         }
  36.  
  37.         void Update(float elapsedTime)
  38.         {
  39.             for (const Ndk::EntityHandle& entity : GetEntities())
  40.             {
  41.                 PositionComponent& posComponent = entity->GetComponent<PositionComponent>();
  42.                 VelocityComponent& velComponent = entity->GetComponent<VelocityComponent>();
  43.  
  44.                 posComponent.position += velComponent.velocity * elapsedTime;
  45.             }
  46.         }
  47.  
  48.         static Ndk::SystemIndex SystemIndex;
  49. };
  50.  
  51. Ndk::SystemIndex VelocitySystem::SystemIndex = Ndk::BaseSystem::GetNextIndex();
  52.  
  53. int main()
  54. {
  55.     Ndk::World world;
  56.  
  57.     // Ajout du système auprès du monde
  58.     VelocitySystem& system = world.AddSystem<VelocitySystem>();
  59.  
  60.     // Création de trois entités et récupération des handles
  61.     std::vector<Ndk::EntityHandle> entities = world.CreateEntities(3);
  62.  
  63.     // Ajout d'un premier composant via template (#0)
  64.     TestComponent& t1 = entities[0]->AddComponent<TestComponent>();
  65.     t1.name = "Hello le monde !";
  66.  
  67.     // Récupération du composant via template
  68.     TestComponent& t2 = entities[0]->GetComponent<TestComponent>();
  69.     std::cout << t2.name << std::endl; // "Hello le monde !"
  70.  
  71.     // Ajout d'une copie du composant à une autre entité (#1)
  72.     TestComponent& t3 = *static_cast<TestComponent*>(entities[1]->AddComponent(std::unique_ptr<Ndk::BaseComponent>(t2.Clone())));
  73.     std::cout << t3.name << std::endl; // "Hello le monde !"
  74.  
  75.     entities[1]->AddComponent<PositionComponent>();
  76.     entities[1]->AddComponent<VelocityComponent>();
  77.  
  78.     // Ajout d'une position/vitesse (#2)
  79.     entities[2]->AddComponent<PositionComponent>().position = NzVector3f(0.f); // Initialisation
  80.     entities[2]->AddComponent<VelocityComponent>().velocity = NzVector3f::Up();
  81.  
  82.     // On tue l'entité #0
  83.     entities[0]->Kill();
  84.  
  85.     // Mise à jour du monde
  86.     world.Update(1.f); // mise à jour d'une seconde
  87.  
  88.     // À ce stade, notre système est associé avec une seule entité (#2) car elle est la seule à correspondre aux filtres (#1 contient un TestComponent qui est exclu du système)
  89.  
  90.     std::cout << entities[2]->GetComponent<PositionComponent>().position << std::endl; // affichera une valeur très proche de NzVector3f::Up()
  91.  
  92.     // Juste pour le fun
  93.     std::cout << entities[2] << std::endl; // EntityHandle(Entity(2))
  94.     std::cout << entities[0] << std::endl; // EntityHandle(Null entity)
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement