Health
This example shows the basic functionality of the ECS portion of Acheron
#include <print>
#include <chrono>
#include "acheron.hpp"
using namespace acheron;
struct Player {};
struct Health {
float value;
};
struct ShouldQuit {
bool value = false;
};
int main() {
world.RegisterComponent<Player>();
world.RegisterComponent<Health>();
world.RegisterSystem<Player, Health>([](
ecs::World& world, ecs::Entity entity) {
auto& health = world.GetComponent<Health>(entity);
health.value -= 1;
std::println("health: {}", health.value);
if(health.value <= 0) world.GetSingleton<ShouldQuit>().value = true;
});
world.SetSingleton<ShouldQuit>({});
world.SpawnWith<Player, Health>(Health{20});
auto& shouldQuit = world.GetSingleton<ShouldQuit>();
auto lastTime = std::chrono::high_resolution_clock::now();
while(!shouldQuit.value) {
auto currentTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<float> elapsed = currentTime - lastTime;
lastTime = currentTime;
float dt = elapsed.count();
world.Update(dt);
}
}
Central context for the ECS.
Definition world.hpp:20