|
| 1 | +/****************************************************************************** |
| 2 | + Copyright © 1997-2001 Id Software, Inc. |
| 3 | + Copyright © 2020-2025 Mark E Sowden <hogsy@oldtimes-software.com> |
| 4 | +
|
| 5 | + This program is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU General Public License |
| 7 | + as published by the Free Software Foundation; either version 2 |
| 8 | + of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This program is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | +
|
| 14 | + See the GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program; if not, write to the Free Software |
| 18 | + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | +******************************************************************************/ |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include <map> |
| 23 | +#include <memory> |
| 24 | + |
| 25 | +class Entity; |
| 26 | +class EntityManager |
| 27 | +{ |
| 28 | +protected: |
| 29 | + typedef Entity *( *constructor )( edict_t *edict ); |
| 30 | + static std::map< std::string, constructor > classRegistry; |
| 31 | + |
| 32 | +public: |
| 33 | + class ClassRegistration |
| 34 | + { |
| 35 | + public: |
| 36 | + ClassRegistration( const std::string &name, constructor constructor ) : name( name ) |
| 37 | + { |
| 38 | + classRegistry[ name ] = constructor; |
| 39 | + } |
| 40 | + ClassRegistration() = delete; |
| 41 | + |
| 42 | + private: |
| 43 | + std::string name; |
| 44 | + }; |
| 45 | + |
| 46 | + struct SpawnVariable |
| 47 | + { |
| 48 | + std::string key; |
| 49 | + std::string value; |
| 50 | + }; |
| 51 | + typedef std::map< std::string, SpawnVariable > SpawnVariables; |
| 52 | + |
| 53 | + static bool ParseSpawnVariables( const char **buf, SpawnVariables &variables ); |
| 54 | + |
| 55 | + static EntityManager *GetInstance() |
| 56 | + { |
| 57 | + static EntityManager instance; |
| 58 | + return &instance; |
| 59 | + } |
| 60 | + |
| 61 | + static Entity *CreateEntity( edict_t *edict, const std::string &classname ); |
| 62 | + |
| 63 | +private: |
| 64 | + EntityManager() = default; |
| 65 | + ~EntityManager() = default; |
| 66 | +}; |
| 67 | + |
| 68 | +#define REGISTER_ENTITY_CLASS( NAME, CLASS ) \ |
| 69 | + static Entity *NAME##_make( edict_t *edict ) { return new CLASS( edict ); } \ |
| 70 | + static EntityManager::ClassRegistration __attribute__( ( init_priority( 2000 ) ) ) \ |
| 71 | + _register_entity_##NAME##_name( #NAME, NAME##_make ); |
0 commit comments