/
EntityModule.cpp
153 lines (124 loc) · 3.72 KB
/
EntityModule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "EntityModule.h"
#include "EntitySettings.h"
#include "itextstream.h"
#include "iundo.h"
#include "imap.h"
#include "iscenegraph.h"
#include "ieventmanager.h"
#include "inamespace.h"
#include "entitylib.h"
#include "gamelib.h"
#include "string/replace.h"
#include <iostream>
#include "i18n.h"
#include "Doom3Entity.h"
#include "light/LightNode.h"
#include "doom3group/Doom3GroupNode.h"
#include "speaker/SpeakerNode.h"
#include "generic/GenericEntityNode.h"
#include "eclassmodel/EclassModelNode.h"
#include "target/TargetManager.h"
#include "module/StaticModule.h"
namespace entity
{
IEntityNodePtr createNodeForEntity(const IEntityClassPtr& eclass)
{
// Null entityclass check
if (!eclass)
{
throw std::runtime_error(
_("createNodeForEntity(): "
"cannot create entity for NULL entityclass.")
);
}
// Otherwise create the correct entity subclass based on the entity class
// parameters.
EntityNodePtr node;
if (eclass->isLight())
{
node = LightNode::Create(eclass);
}
else if (!eclass->isFixedSize())
{
// Variable size entity
node = Doom3GroupNode::Create(eclass);
}
else if (!eclass->getAttribute("model").getValue().empty())
{
// Fixed size, has model path
node = EclassModelNode::Create(eclass);
}
else if (eclass->getName() == "speaker")
{
node = SpeakerNode::create(eclass);
}
else
{
// Fixed size, no model path
node = GenericEntityNode::Create(eclass);
}
return node;
}
IEntityNodePtr Doom3EntityModule::createEntity(const IEntityClassPtr& eclass)
{
IEntityNodePtr node = createNodeForEntity(eclass);
if (GlobalMapModule().getRoot())
{
// All entities are created in the active layer by default
node->moveToLayer(GlobalMapModule().getRoot()->getLayerManager().getActiveLayer());
}
node->getEntity().setKeyValue("classname", eclass->getName());
// If this is not a worldspawn or unrecognised entity, generate a unique
// name for it
const std::string& eclassName = eclass->getName();
if (!eclassName.empty() &&
eclassName != "worldspawn" &&
eclassName != "UNKNOWN_CLASS")
{
/* Clean up the name of the entity that is about the created
* so that nothing bad can happen (for example, the colon character
* seems to be causing problems in Doom 3 Scripting)
*/
std::string entityName = string::replace_all_copy(eclassName, ":", "_") + "_1";
node->getEntity().setKeyValue("name", entityName);
}
return node;
}
ITargetManagerPtr Doom3EntityModule::createTargetManager()
{
return std::make_shared<TargetManager>();
}
// RegisterableModule implementation
const std::string& Doom3EntityModule::getName() const
{
static std::string _name(MODULE_ENTITY);
return _name;
}
const StringSet& Doom3EntityModule::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_XMLREGISTRY);
_dependencies.insert(MODULE_MAP);
_dependencies.insert(MODULE_GAMEMANAGER);
}
return _dependencies;
}
void Doom3EntityModule::initialiseModule(const ApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called." << std::endl;
LightShader::m_defaultShader = game::current::getValue<std::string>("/defaults/lightShader");
GlobalEventManager().addRegistryToggle("ToggleShowAllLightRadii", RKEY_SHOW_ALL_LIGHT_RADII);
GlobalEventManager().addRegistryToggle("ToggleShowAllSpeakerRadii", RKEY_SHOW_ALL_SPEAKER_RADII);
GlobalEventManager().addRegistryToggle("ToggleDragResizeEntitiesSymmetrically", RKEY_DRAG_RESIZE_SYMMETRICALLY);
}
void Doom3EntityModule::shutdownModule()
{
rMessage() << getName() << "::shutdownModule called." << std::endl;
// Destroy the settings instance
EntitySettings::destroy();
}
// Static module instance
module::StaticModule<Doom3EntityModule> entityModule;
} // namespace entity