khora_core/ecs/entity.rs
1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Defines core types related to entities in the ECS architecture.
16
17use bincode::{Decode, Encode};
18use serde::{Deserialize, Serialize};
19
20/// A unique identifier for an entity in the world.
21///
22/// It combines an index with a generation count to solve the "ABA problem".
23/// When an entity is despawned, its index can be recycled for a new entity,
24/// but the generation is incremented. This ensures that old `EntityId` handles
25/// pointing to a recycled index become invalid and cannot accidentally affect
26/// the new entity.
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Encode, Decode)]
28pub struct EntityId {
29 /// The index of the entity's metadata in the central `Vec<EntityMetadata>`.
30 pub index: u32,
31 /// A generation counter that is incremented each time the index is recycled.
32 pub generation: u32,
33}