Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/scripting
diff options
context:
space:
mode:
Diffstat (limited to 'source/game/scripting')
-rw-r--r--source/game/scripting/StarLuaAnimationComponent.hpp28
-rw-r--r--source/game/scripting/StarLuaGameConverters.cpp54
-rw-r--r--source/game/scripting/StarLuaGameConverters.hpp7
3 files changed, 63 insertions, 26 deletions
diff --git a/source/game/scripting/StarLuaAnimationComponent.hpp b/source/game/scripting/StarLuaAnimationComponent.hpp
index 76c3f28..6d94466 100644
--- a/source/game/scripting/StarLuaAnimationComponent.hpp
+++ b/source/game/scripting/StarLuaAnimationComponent.hpp
@@ -61,36 +61,12 @@ LuaAnimationComponent<Base>::LuaAnimationComponent() {
animationCallbacks.registerCallback("clearDrawables", [this]() {
m_drawables.clear();
});
- animationCallbacks.registerCallback("addDrawable", [this](LuaTable const& drawableTable, Maybe<String> renderLayerName) {
+ animationCallbacks.registerCallback("addDrawable", [this](Drawable drawable, Maybe<String> renderLayerName) {
Maybe<EntityRenderLayer> renderLayer;
if (renderLayerName)
renderLayer = parseRenderLayer(*renderLayerName);
- Color color = drawableTable.get<Maybe<Color>>("color").value(Color::White);
-
- Drawable drawable;
- if (auto line = drawableTable.get<Maybe<Line2F>>("line"))
- drawable = Drawable::makeLine(line.take(), drawableTable.get<float>("width"), color);
- else if (auto poly = drawableTable.get<Maybe<PolyF>>("poly"))
- drawable = Drawable::makePoly(poly.take(), color);
- else if (auto image = drawableTable.get<Maybe<String>>("image"))
- drawable = Drawable::makeImage(image.take(), 1.0f / TilePixels, drawableTable.get<Maybe<bool>>("centered").value(true), Vec2F(), color);
- else
- throw LuaAnimationComponentException("Drawable table must have 'line', 'poly', or 'image'");
-
- if (auto transformation = drawableTable.get<Maybe<Mat3F>>("transformation"))
- drawable.transform(*transformation);
- if (auto rotation = drawableTable.get<Maybe<float>>("rotation"))
- drawable.rotate(*rotation);
- if (drawableTable.get<bool>("mirrored"))
- drawable.scale(Vec2F(-1, 1));
- if (auto scale = drawableTable.get<Maybe<float>>("scale"))
- drawable.scale(*scale);
- if (auto position = drawableTable.get<Maybe<Vec2F>>("position"))
- drawable.translate(*position);
-
- drawable.fullbright = drawableTable.get<bool>("fullbright");
-
+ drawable.scale(1.0f / TilePixels);
m_drawables.append({move(drawable), renderLayer});
});
animationCallbacks.registerCallback("clearLightSources", [this]() {
diff --git a/source/game/scripting/StarLuaGameConverters.cpp b/source/game/scripting/StarLuaGameConverters.cpp
index e2fd516..070048e 100644
--- a/source/game/scripting/StarLuaGameConverters.cpp
+++ b/source/game/scripting/StarLuaGameConverters.cpp
@@ -417,6 +417,60 @@ Maybe<LiquidLevel> LuaConverter<LiquidLevel>::to(LuaEngine& engine, LuaValue con
return {};
}
+LuaValue LuaConverter<Drawable>::from(LuaEngine& engine, Drawable const& v) {
+ auto table = engine.createTable();
+ if (auto line = v.part.ptr<Drawable::LinePart>()) {
+ table.set("line", line->line);
+ table.set("width", line->width);
+ } else if (auto poly = v.part.ptr<Drawable::PolyPart>()) {
+ table.set("poly", poly->poly);
+ } else if (auto image = v.part.ptr<Drawable::ImagePart>()) {
+ table.set("image", AssetPath::join(image->image));
+ table.set("transformation", image->transformation);
+ }
+
+ table.set("position", v.position);
+ table.set("color", v.color);
+ table.set("fullbright", v.fullbright);
+
+ return table;
+}
+
+Maybe<Drawable> LuaConverter<Drawable>::to(LuaEngine& engine, LuaValue const& v) {
+ if (auto table = v.ptr<LuaTable>()) {
+ Maybe<Drawable> result;
+ result.emplace();
+ Drawable& drawable = result.get();
+
+ Color color = table->get<Maybe<Color>>("color").value(Color::White);
+
+ if (auto line = table->get<Maybe<Line2F>>("line"))
+ drawable = Drawable::makeLine(line.take(), table->get<float>("width"), color);
+ else if (auto poly = table->get<Maybe<PolyF>>("poly"))
+ drawable = Drawable::makePoly(poly.take(), color);
+ else if (auto image = table->get<Maybe<String>>("image"))
+ drawable = Drawable::makeImage(image.take(), 1.0f, table->get<Maybe<bool>>("centered").value(true), Vec2F(), color);
+ else
+ return {}; // throw LuaAnimationComponentException("Drawable table must have 'line', 'poly', or 'image'");
+
+ if (auto transformation = table->get<Maybe<Mat3F>>("transformation"))
+ drawable.transform(*transformation);
+ if (auto rotation = table->get<Maybe<float>>("rotation"))
+ drawable.rotate(*rotation);
+ if (table->get<bool>("mirrored"))
+ drawable.scale(Vec2F(-1, 1));
+ if (auto scale = table->get<Maybe<float>>("scale"))
+ drawable.scale(*scale);
+ if (auto position = table->get<Maybe<Vec2F>>("position"))
+ drawable.translate(*position);
+
+ drawable.fullbright = table->get<bool>("fullbright");
+
+ return result;
+ }
+ return {};
+}
+
LuaValue LuaConverter<Collection>::from(LuaEngine& engine, Collection const& c) {
auto table = engine.createTable();
table.set("name", c.name);
diff --git a/source/game/scripting/StarLuaGameConverters.hpp b/source/game/scripting/StarLuaGameConverters.hpp
index c0f5416..ff84792 100644
--- a/source/game/scripting/StarLuaGameConverters.hpp
+++ b/source/game/scripting/StarLuaGameConverters.hpp
@@ -9,6 +9,7 @@
#include "StarCollectionDatabase.hpp"
#include "StarBehaviorState.hpp"
#include "StarSystemWorld.hpp"
+#include "StarDrawable.hpp"
namespace Star {
@@ -98,6 +99,12 @@ struct LuaConverter<LiquidLevel> {
static Maybe<LiquidLevel> to(LuaEngine& engine, LuaValue const& v);
};
+template <>
+struct LuaConverter<Drawable> {
+ static LuaValue from(LuaEngine& engine, Drawable const& v);
+ static Maybe<Drawable> to(LuaEngine& engine, LuaValue const& v);
+};
+
template <typename T>
LuaMethods<RpcPromise<T>> LuaUserDataMethods<RpcPromise<T>>::make() {
LuaMethods<RpcPromise<T>> methods;