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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
#include "StarStagehand.hpp"
#include "StarDataStreamExtra.hpp"
#include "StarJsonExtra.hpp"
#include "StarConfigLuaBindings.hpp"
#include "StarEntityLuaBindings.hpp"
#include "StarBehaviorLuaBindings.hpp"
#include "StarLuaGameConverters.hpp"
namespace Star {
Stagehand::Stagehand(Json const& config)
: Stagehand() {
setUniqueId(config.optString("uniqueId"));
readConfig(config);
}
Stagehand::Stagehand(ByteArray const& netStore, NetCompatibilityRules rules) : Stagehand() {
readConfig(DataStreamBuffer::deserialize<Json>(netStore));
}
Json Stagehand::diskStore() const {
auto saveData = m_config.setAll({
{"position", jsonFromVec2F({m_xPosition.get(), m_yPosition.get()})},
{"uniqueId", jsonFromMaybe(uniqueId())}
});
if (!m_scripted)
return saveData;
else
return saveData.set("scriptStorage", m_scriptComponent.getScriptStorage());
}
ByteArray Stagehand::netStore(NetCompatibilityRules rules) {
return DataStreamBuffer::serialize(m_config);
}
void Stagehand::init(World* world, EntityId entityId, EntityMode mode) {
Entity::init(world, entityId, mode);
if (isMaster() && m_scripted) {
m_scriptComponent.addCallbacks("stagehand", makeStagehandCallbacks());
m_scriptComponent.addCallbacks("config", LuaBindings::makeConfigCallbacks([this](String const& name, Json const& def) {
return m_config.query(name, def);
}));
m_scriptComponent.addCallbacks("entity", LuaBindings::makeEntityCallbacks(this));
m_scriptComponent.addCallbacks("behavior", LuaBindings::makeBehaviorCallbacks(&m_behaviors));
m_scriptComponent.init(world);
}
}
void Stagehand::uninit() {
Entity::uninit();
if (m_scripted) {
m_scriptComponent.uninit();
m_scriptComponent.removeCallbacks("stagehand");
m_scriptComponent.removeCallbacks("config");
m_scriptComponent.removeCallbacks("entity");
}
}
EntityType Stagehand::entityType() const {
return EntityType::Stagehand;
}
void Stagehand::setPosition(Vec2F const& position) {
m_xPosition.set(position[0]);
m_yPosition.set(position[1]);
}
Vec2F Stagehand::position() const {
return {m_xPosition.get(), m_yPosition.get()};
}
RectF Stagehand::metaBoundBox() const {
return m_boundBox;
}
pair<ByteArray, uint64_t> Stagehand::writeNetState(uint64_t fromVersion, NetCompatibilityRules rules) {
return m_netGroup.writeNetState(fromVersion, rules);
}
void Stagehand::readNetState(ByteArray data, float interpolationTime, NetCompatibilityRules rules) {
m_netGroup.readNetState(data, interpolationTime, rules);
}
void Stagehand::update(float dt, uint64_t) {
if (!inWorld())
return;
if (isMaster() && m_scripted)
m_scriptComponent.update(m_scriptComponent.updateDt(dt));
if (world()->isClient()) {
auto boundBox = metaBoundBox().translated(position());
SpatialLogger::logPoly("world", PolyF(boundBox), { 0, 255, 255, 255 });
SpatialLogger::logLine("world", boundBox.min(), boundBox.max(), {0, 255, 255, 255});
SpatialLogger::logLine("world", { boundBox.xMin(), boundBox.yMax() }, { boundBox.xMax(), boundBox.yMin() }, {0, 255, 255, 255});
}
}
bool Stagehand::shouldDestroy() const {
return m_dead;
}
Maybe<LuaValue> Stagehand::callScript(String const& func, LuaVariadic<LuaValue> const& args) {
return m_scriptComponent.invoke(func, args);
}
Maybe<LuaValue> Stagehand::evalScript(String const& code) {
return m_scriptComponent.eval(code);
}
Stagehand::Stagehand() {
setPersistent(true);
m_netGroup.addNetElement(&m_xPosition);
m_netGroup.addNetElement(&m_yPosition);
m_netGroup.addNetElement(&m_uniqueIdNetState);
m_netGroup.setNeedsLoadCallback([this](bool) {
setUniqueId(m_uniqueIdNetState.get());
});
m_netGroup.setNeedsStoreCallback([this]() {
m_uniqueIdNetState.set(uniqueId());
});
}
void Stagehand::readConfig(Json config) {
m_config = config;
m_scripted = m_config.contains("scripts");
if (m_config.contains("position")) {
auto pos = jsonToVec2F(m_config.get("position"));
m_xPosition.set(pos[0]);
m_yPosition.set(pos[1]);
}
Maybe<RectF> broadcastArea = jsonToMaybe<RectF>(config.opt("broadcastArea").value(Json()), jsonToRectF);
if (broadcastArea && (broadcastArea->size()[0] < 0.0 || broadcastArea->size()[1] < 0.0))
broadcastArea.reset();
m_boundBox = broadcastArea.value(RectF(-5.0, -5.0, 5.0, 5.0));
if (m_scripted) {
m_scriptComponent.setScripts(jsonToStringList(m_config.getArray("scripts", JsonArray())));
m_scriptComponent.setUpdateDelta(m_config.getInt("scriptDelta", 5));
if (m_config.contains("scriptStorage"))
m_scriptComponent.setScriptStorage(m_config.getObject("scriptStorage"));
}
setKeepAlive(config.getBool("keepAlive", false));
}
LuaCallbacks Stagehand::makeStagehandCallbacks() {
LuaCallbacks callbacks;
callbacks.registerCallback("id", [this]() {
return entityId();
});
callbacks.registerCallback("position", [this]() {
return position();
});
callbacks.registerCallback("setPosition", [this](Vec2F const& position) {
setPosition(position);
});
callbacks.registerCallback("die", [this]() {
m_dead = true;
});
callbacks.registerCallback("typeName", [this]() {
return typeName();
});
callbacks.registerCallback("setUniqueId", [this](Maybe<String> const& uniqueId) {
setUniqueId(uniqueId);
});
return callbacks;
}
String Stagehand::typeName() const {
return m_config.getString("type");
}
Maybe<Json> Stagehand::receiveMessage(ConnectionId sendingConnection, String const& message, JsonArray const& args) {
return m_scriptComponent.handleMessage(message, sendingConnection == world()->connection(), args);
}
}
|