mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-22 15:23:01 +01:00
Get thing inhertiance working
This commit is contained in:
@@ -14,4 +14,10 @@
|
||||
# include <dukglue/dukglue.h>
|
||||
# include <duktape.h>
|
||||
|
||||
template<typename T> DukValue GetObjectAsDukValue(duk_context* ctx, const std::shared_ptr<T>& value)
|
||||
{
|
||||
dukglue::types::DukType<std::shared_ptr<T>>::template push<T>(ctx, value);
|
||||
return DukValue::take_from_stack(ctx);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -98,7 +98,7 @@ namespace OpenRCT2::Scripting
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<ScThing>> getAllThings(const std::string& type)
|
||||
std::vector<DukValue> getAllThings(const std::string& type)
|
||||
{
|
||||
SPRITE_LIST targetList{};
|
||||
uint8_t targetType{};
|
||||
@@ -109,7 +109,7 @@ namespace OpenRCT2::Scripting
|
||||
}
|
||||
if (type == "car")
|
||||
{
|
||||
targetList = SPRITE_LIST_VEHICLE;
|
||||
targetList = SPRITE_LIST_VEHICLE_HEAD;
|
||||
}
|
||||
else if (type == "litter")
|
||||
{
|
||||
@@ -129,17 +129,45 @@ namespace OpenRCT2::Scripting
|
||||
duk_error(_context, DUK_ERR_ERROR, "Invalid thing type.");
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<ScThing>> result;
|
||||
std::vector<DukValue> result;
|
||||
auto spriteId = gSpriteListHead[targetList];
|
||||
while (spriteId != SPRITE_INDEX_NULL)
|
||||
{
|
||||
auto sprite = get_sprite(spriteId);
|
||||
if (sprite != nullptr)
|
||||
if (sprite == nullptr)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Only the misc list checks the type property
|
||||
if (targetList != SPRITE_LIST_MISC || sprite->generic.type == targetType)
|
||||
{
|
||||
result.push_back(std::make_shared<ScThing>(spriteId));
|
||||
if (targetList == SPRITE_LIST_PEEP)
|
||||
{
|
||||
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScPeep>(spriteId)));
|
||||
}
|
||||
else if (targetList == SPRITE_LIST_VEHICLE_HEAD)
|
||||
{
|
||||
auto carId = spriteId;
|
||||
while (carId != SPRITE_INDEX_NULL)
|
||||
{
|
||||
auto car = get_sprite(carId);
|
||||
if (car == nullptr)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScThing>(carId)));
|
||||
carId = car->vehicle.next_vehicle_on_train;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result.push_back(GetObjectAsDukValue(_context, std::make_shared<ScThing>(spriteId)));
|
||||
}
|
||||
}
|
||||
spriteId = sprite->generic.next;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace OpenRCT2::Scripting
|
||||
{
|
||||
class ScThing
|
||||
{
|
||||
private:
|
||||
protected:
|
||||
uint16_t _id = SPRITE_INDEX_NULL;
|
||||
|
||||
public:
|
||||
@@ -105,6 +105,30 @@ namespace OpenRCT2::Scripting
|
||||
}
|
||||
}
|
||||
|
||||
SpriteBase* GetThing()
|
||||
{
|
||||
return &get_sprite(_id)->generic;
|
||||
}
|
||||
|
||||
public:
|
||||
static void Register(duk_context* ctx)
|
||||
{
|
||||
dukglue_register_property(ctx, &ScThing::type_get, nullptr, "type");
|
||||
dukglue_register_property(ctx, &ScThing::x_get, &ScThing::x_set, "x");
|
||||
dukglue_register_property(ctx, &ScThing::y_get, &ScThing::y_set, "y");
|
||||
dukglue_register_property(ctx, &ScThing::z_get, &ScThing::z_set, "z");
|
||||
}
|
||||
};
|
||||
|
||||
class ScPeep : public ScThing
|
||||
{
|
||||
public:
|
||||
ScPeep(uint16_t id)
|
||||
: ScThing(id)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
uint8_t tshirtColour_get()
|
||||
{
|
||||
auto peep = GetPeep();
|
||||
@@ -134,11 +158,6 @@ namespace OpenRCT2::Scripting
|
||||
}
|
||||
}
|
||||
|
||||
SpriteBase* GetThing()
|
||||
{
|
||||
return &get_sprite(_id)->generic;
|
||||
}
|
||||
|
||||
Peep* GetPeep()
|
||||
{
|
||||
return get_sprite(_id)->AsPeep();
|
||||
@@ -147,14 +166,12 @@ namespace OpenRCT2::Scripting
|
||||
public:
|
||||
static void Register(duk_context* ctx)
|
||||
{
|
||||
dukglue_register_property(ctx, &ScThing::type_get, nullptr, "type");
|
||||
dukglue_register_property(ctx, &ScThing::x_get, &ScThing::x_set, "x");
|
||||
dukglue_register_property(ctx, &ScThing::y_get, &ScThing::y_set, "y");
|
||||
dukglue_register_property(ctx, &ScThing::z_get, &ScThing::z_set, "z");
|
||||
dukglue_register_property(ctx, &ScThing::tshirtColour_get, &ScThing::tshirtColour_set, "tshirtColour");
|
||||
dukglue_register_property(ctx, &ScThing::trousersColour_get, &ScThing::trousersColour_set, "trousersColour");
|
||||
dukglue_set_base_class<ScThing, ScPeep>(ctx);
|
||||
dukglue_register_property(ctx, &ScPeep::tshirtColour_get, &ScPeep::tshirtColour_set, "tshirtColour");
|
||||
dukglue_register_property(ctx, &ScPeep::trousersColour_get, &ScPeep::trousersColour_set, "trousersColour");
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace OpenRCT2::Scripting
|
||||
|
||||
#endif
|
||||
|
||||
@@ -377,6 +377,7 @@ void ScriptEngine::Initialise()
|
||||
ScTile::Register(ctx);
|
||||
ScTileElement::Register(ctx);
|
||||
ScThing::Register(ctx);
|
||||
ScPeep::Register(ctx);
|
||||
|
||||
dukglue_register_global(ctx, std::make_shared<ScConsole>(_console), "console");
|
||||
dukglue_register_global(ctx, std::make_shared<ScContext>(_execInfo, _hookEngine), "context");
|
||||
|
||||
Reference in New Issue
Block a user