| 
									
										
										
										
											2025-02-14 21:35:40 +03:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | #include <cstdint>
 | 
					
						
							|  |  |  | #include <bitset>
 | 
					
						
							| 
									
										
										
										
											2025-02-14 22:18:00 +03:00
										 |  |  | #include <format>
 | 
					
						
							| 
									
										
										
										
											2025-02-14 22:27:52 +03:00
										 |  |  | #include <stdexcept>
 | 
					
						
							| 
									
										
										
										
											2025-02-14 21:35:40 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifndef MAX_ZECSY_ENTITIES
 | 
					
						
							|  |  |  |     #define MAX_ZECSY_ENTITIES 65536
 | 
					
						
							|  |  |  | #endif // !MAX_ZECSY_ENTITIES
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace zecsy  | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     using entity_id = uint64_t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-14 22:18:00 +03:00
										 |  |  |     class entity final | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     public: | 
					
						
							|  |  |  |         entity(class world* w, entity_id id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         entity() = default; | 
					
						
							|  |  |  |         entity(const entity &) = default; | 
					
						
							|  |  |  |         entity(entity &&) = default; | 
					
						
							|  |  |  |         entity &operator=(const entity &) = default; | 
					
						
							|  |  |  |         entity &operator=(entity &&) = default; | 
					
						
							|  |  |  |         operator entity_id() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         bool is_alive() const; | 
					
						
							|  |  |  |     private: | 
					
						
							|  |  |  |         entity_id id = 0; | 
					
						
							|  |  |  |         class world* w = nullptr; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-14 21:35:40 +03:00
										 |  |  |     class world final | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     public: | 
					
						
							|  |  |  |         world() = default; | 
					
						
							|  |  |  |         world(const world &) = default; | 
					
						
							|  |  |  |         world(world &&) = default; | 
					
						
							|  |  |  |         world &operator=(const world &) = default; | 
					
						
							|  |  |  |         world &operator=(world &&) = default; | 
					
						
							|  |  |  |     | 
					
						
							| 
									
										
										
										
											2025-02-14 22:18:00 +03:00
										 |  |  |         entity make_entity(); | 
					
						
							| 
									
										
										
										
											2025-02-14 21:41:36 +03:00
										 |  |  |         void destroy_entity(entity_id e); | 
					
						
							| 
									
										
										
										
											2025-02-14 21:35:40 +03:00
										 |  |  |         bool is_alive(entity_id e) const;  | 
					
						
							|  |  |  |     private: | 
					
						
							| 
									
										
										
										
											2025-02-14 22:27:52 +03:00
										 |  |  |         std::bitset<MAX_ZECSY_ENTITIES + 1> entities_bitset; | 
					
						
							| 
									
										
										
										
											2025-02-14 21:35:40 +03:00
										 |  |  |         entity_id entity_counter = 0; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-14 22:18:00 +03:00
										 |  |  |     inline entity::entity(class world* w, entity_id id): w(w), id(id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     inline entity::operator entity_id() const  | 
					
						
							| 
									
										
										
										
											2025-02-14 21:35:40 +03:00
										 |  |  |     { | 
					
						
							|  |  |  |         return id; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-14 22:18:00 +03:00
										 |  |  |     inline bool entity::is_alive() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return w && w->is_alive(id); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     inline entity world::make_entity() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-02-14 22:27:52 +03:00
										 |  |  |         auto id = ++entity_counter; | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         if(id > MAX_ZECSY_ENTITIES) | 
					
						
							| 
									
										
										
										
											2025-02-14 22:18:00 +03:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2025-02-14 22:27:52 +03:00
										 |  |  |             throw std::runtime_error(std::format("Entity id {} exceeds "  | 
					
						
							|  |  |  |                         "MAX_ZECSY_ENTITIES ({})", id, MAX_ZECSY_ENTITIES)); | 
					
						
							| 
									
										
										
										
											2025-02-14 22:18:00 +03:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         entities_bitset.set(id); | 
					
						
							|  |  |  |         return entity(this, id); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-14 21:41:36 +03:00
										 |  |  |     inline void world::destroy_entity(entity_id e) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         entities_bitset.reset(e); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-14 21:35:40 +03:00
										 |  |  |     inline bool world::is_alive(entity_id e) const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return entities_bitset.test(e); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; |