#pragma once #include "component/component.h" #include "../transformable.h" #include "../util.h" #include #include #include #include #include #include #include namespace simpleengine { /** * @brief A Model is a object that will be shown on the screen by a renderer. * */ class Entity : public simpleengine::Event, public simpleengine::Transformable { private: static uint32_t incrementing_handle; uint32_t handle; public: std::vector> components; Entity() : components({}) { handle = incrementing_handle++; } /* Entity() : components({}) { std::random_device rd; // obtain a random number from hardware std::mt19937 gen(rd()); // seed the generator std::uniform_int_distribution distr(1, std::numeric_limits::max()); uint16_t num = distr(gen); uint32_t pid = simpleengine::util::get_current_pid(); handle |= num; handle |= (pid << 16); std::string binary = std::bitset<16>(num).to_string(); std::cout << "Entity handle: " << binary << std::endl; } */ Entity(std::vector> components) : Entity() { this->components = components; } uint32_t get_handle() { return handle; } virtual void update(const float& delta_time) override { std::cout << "Update entity" << std::endl; for (auto& component : components) { component->update(delta_time); } } template bool has_component() const { for (const auto& comp : components) { if (std::dynamic_pointer_cast(comp)) { return true; } } return false; } template std::shared_ptr get_component() const { for (const auto& comp : components) { if (std::shared_ptr dyn_comp = std::dynamic_pointer_cast(comp); dyn_comp) { return dyn_comp; } } return nullptr; } template void add_component(std::shared_ptr component) { static_assert(std::is_base_of_v, "Component class must derive from simpleengine::Component"); // Only allow one type of the same component assert(!has_component()); // TODO: Don't assert, give an error components.push_back(component); } template void add_component(T component) { static_assert(std::is_base_of_v, "Component class must derive from simpleengine::Component"); // Only allow one type of the same component assert(!has_component()); // TODO: Don't assert, give an error components.push_back(std::make_shared(component)); } }; }