### Quick Start Example Source: https://github.com/martinstarkov/ecs/blob/main/README.md Demonstrates the basic usage of the ECS library, including creating an entity, adding components (Position, Velocity), and iterating over entities with specific components to update their state. ```cpp #include "ecs/ecs.h" struct Position { float x, y; }; struct Velocity { float dx, dy; }; int main() { ecs::Manager manager; auto entity = manager.CreateEntity(); entity.Add(0.f, 0.f); entity.Add(1.f, 1.f); manager.Refresh(); for (auto [e, pos, vel] : manager.EntitiesWith()) { pos.x += vel.dx; pos.y += vel.dy; } } ``` -------------------------------- ### Retrieving Components Source: https://github.com/martinstarkov/ecs/blob/main/README.md Illustrates how to get references to components attached to an entity. It supports retrieving single or multiple components simultaneously. It is advisable to use `Has()` before `Get()` to avoid debug assertions when a component is missing. ```cpp auto& human = entity.Get(); auto [robot, alien] = entity.Get(); ``` -------------------------------- ### Component Definition Example Source: https://github.com/martinstarkov/ecs/blob/main/README.md Illustrates the definition of a component struct, `HumanComponent`, which must have a valid constructor, destructor, and move constructor. This component stores an integer age and a double height. ```cpp struct HumanComponent { HumanComponent(int age, double height) : age{ age }, height{ height } {} int age; double height; }; ``` -------------------------------- ### Introspection Source: https://github.com/martinstarkov/ecs/blob/main/README.md Provides functions to inspect the state of the manager, such as checking if it's empty or getting its current size and capacity. ```cpp bool is_empty = manager.IsEmpty(); std::size_t size = manager.Size(); std::size_t capacity = manager.Capacity(); ``` -------------------------------- ### Removing Components Source: https://github.com/martinstarkov/ecs/blob/main/README.md Provides examples for removing single or multiple components from an entity. The operation is a no-op if the specified component does not exist on the entity. ```cpp entity.Remove(); entity.Remove(); ``` -------------------------------- ### Cloning Managers Source: https://github.com/martinstarkov/ecs/blob/main/README.md Shows how to create a deep copy of an entire manager, including all its entities and components. ```cpp auto new_manager = manager.Clone(); assert(new_manager == manager); // Deep comparison ``` -------------------------------- ### Entity Creation Source: https://github.com/martinstarkov/ecs/blob/main/README.md Demonstrates the creation of a new entity using the `CreateEntity()` method of the `ecs::Manager`. ```cpp auto entity = manager.CreateEntity(); ``` -------------------------------- ### Copying Entities Source: https://github.com/martinstarkov/ecs/blob/main/README.md Demonstrates various ways to copy entities, including creating a new entity from an existing one, copying to an existing entity, and copying specific components. ```cpp auto new_entity = entity.Copy(); // Or: auto new_entity = manager.CopyEntity(entity); // Or copy to existing: manager.CopyEntity(source_entity, destination_entity); // Or copy specific components: auto new_entity = manager.CopyEntity(entity); ``` -------------------------------- ### ECS Manager Initialization Source: https://github.com/martinstarkov/ecs/blob/main/README.md Shows how to initialize the core `ecs::Manager` class, which is responsible for managing all entities and their components within the ECS. ```cpp #include "ecs/ecs.h" ecs::Manager manager; ``` -------------------------------- ### CMake Integration for ECS Library Source: https://github.com/martinstarkov/ecs/blob/main/README.md Provides instructions on how to integrate the ECS library into a CMake project using `FetchContent` to download and include the library from its GitHub repository. ```cmake include(FetchContent) FetchContent_Declare( ecs GIT_REPOSITORY https://github.com/martinstarkov/ecs.git GIT_TAG main ) FetchContent_MakeAvailable(ecs) set(ECS_INCLUDE_DIR "${ecs_SOURCE_DIR}/include") target_include_directories( PRIVATE ${ECS_INCLUDE_DIR}) ``` -------------------------------- ### Adding Components Source: https://github.com/martinstarkov/ecs/blob/main/README.md Demonstrates how to add components to an entity. Adding a component replaces an existing one of the same type. Be aware that adding a component can invalidate existing references to other components of the same type due to potential memory reallocations. ```cpp auto& human = entity.Add(22, 180.5); human.height += 0.5; ``` -------------------------------- ### Iterating All Entities Source: https://github.com/martinstarkov/ecs/blob/main/README.md Demonstrates how to iterate through all entities managed by the system, allowing for operations on every entity. ```cpp for (auto entity : manager.Entities()) { entity.Add(); } ``` -------------------------------- ### Checking for Components Source: https://github.com/martinstarkov/ecs/blob/main/README.md Shows how to check if an entity possesses a specific component or a combination of components. ```cpp bool is_human = entity.Has(); bool is_cyborg = entity.Has(); ``` -------------------------------- ### Iterating Entities Without Specific Components Source: https://github.com/martinstarkov/ecs/blob/main/README.md Illustrates how to iterate over entities that do not have certain components, useful for cleanup or specific logic. ```cpp for (auto entity : manager.EntitiesWithout()) { entity.Destroy(); } manager.Refresh(); ``` -------------------------------- ### Capacity Management Source: https://github.com/martinstarkov/ecs/blob/main/README.md Details functions for managing the manager's capacity, including pre-allocating memory, clearing all entities while retaining capacity, and resetting the manager completely. ```cpp manager.Reserve(100); // Preallocate for 100 entities manager.Clear(); // Remove all entities, keep capacity manager.Reset(); // Remove all entities and free memory ``` -------------------------------- ### Entity Comparisons and Identity Source: https://github.com/martinstarkov/ecs/blob/main/README.md Details how to compare entity handles using `==` and `!=`, and how to compare entity components using `entity.IsIdenticalTo(other)`. Entity handles are also hashable for use in containers like `std::unordered_map`. ```cpp // Use == and != to compare entity handles. // Use entity.IsIdenticalTo(other) to compare entity components (not just handles). // Entity handles are hashable and usable in std::unordered_map. ``` -------------------------------- ### Accessing Entity's Parent Manager Source: https://github.com/martinstarkov/ecs/blob/main/README.md Demonstrates how to retrieve a reference to the parent manager that owns an entity using the `GetManager()` function. ```cpp auto& manager_ref = entity.GetManager(); ``` -------------------------------- ### ECS Project CMake Configuration Source: https://github.com/martinstarkov/ecs/blob/main/tests/CMakeLists.txt Configures the CMake build system for the ECS project. It specifies the minimum required CMake version, sets the project name, enforces C++ standard 17, and defines the 'ecs_tests' executable target. It also includes necessary header directories for the project. ```cmake cmake_minimum_required(VERSION 3.20) project(ecs_tests) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED True) add_executable(ecs_tests "${CMAKE_SOURCE_DIR}/main.cpp" "${CMAKE_SOURCE_DIR}/test_ecs.h" "${CMAKE_SOURCE_DIR}/../include/ecs/ecs.h") target_include_directories(ecs_tests PRIVATE "${CMAKE_SOURCE_DIR}/../include") ``` -------------------------------- ### Iterating Entities with Specific Components Source: https://github.com/martinstarkov/ecs/blob/main/README.md Shows how to iterate only over entities that possess a specific set of components, enabling targeted operations. ```cpp for (auto [entity, zombie, food] : manager.EntitiesWith()) { if (food.amount < 10) { // feed the zombie } } ``` -------------------------------- ### Entity Lifecycle and Manager Refresh Source: https://github.com/martinstarkov/ecs/blob/main/README.md Explains the entity lifecycle, emphasizing that entities are not considered 'alive' for iteration until `manager.Refresh()` is called. This prevents iterator invalidation during loops and ensures proper state updates for entity creation and destruction. ```cpp auto entity = manager.CreateEntity(); entity.Add(5.0f); // Add a component to the entity // Entity is not yet considered "alive" for iteration for (auto e : manager.Entities()) { // This loop will not include the newly created entity yet } // Now call Refresh to update the manager's internal state manager.Refresh(); // The entity is now considered "alive" and will be included // Destroy the entity entity.Destroy(); // The entity is still considered alive until we call Refresh for (auto e : manager.Entities()) { // The entity will still be part of the iteration, because it hasn't been refreshed yet } // Call Refresh again to update the state, removing destroyed entities manager.Refresh(); // Now the entity is excluded from future entity loops ``` -------------------------------- ### Invalid Entity Representation Source: https://github.com/martinstarkov/ecs/blob/main/README.md Shows how to represent an invalid entity by using a default-constructed `ecs::Entity{}`. ```cpp ecs::Entity invalid_entity{}; // Using default-constructed entity ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.