Files
OpenRCT2-Web/src/openrct2/scripting/bindings/entity/ScLitter.cpp
T

93 lines
3.1 KiB
C++
Raw Normal View History

2021-08-09 20:32:20 +03:00
/*****************************************************************************
2026-01-05 14:42:59 +01:00
* Copyright (c) 2014-2026 OpenRCT2 developers
2021-08-09 20:32:20 +03:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#ifdef ENABLE_SCRIPTING
#include "ScLitter.hpp"
2021-08-09 20:32:20 +03:00
2025-09-29 02:05:00 +10:00
#include "../../../core/EnumMap.hpp"
#include "../../../entity/Litter.h"
2021-11-26 15:03:14 +00:00
2021-08-09 20:32:20 +03:00
namespace OpenRCT2::Scripting
{
2025-09-29 02:05:00 +10:00
static const EnumMap<Litter::Type> LitterTypeMap(
2025-07-31 11:01:48 +02:00
{
2025-10-05 14:54:56 +02:00
{ "vomit", Litter::Type::vomit },
{ "vomit_alt", Litter::Type::vomitAlt },
{ "empty_can", Litter::Type::emptyCan },
{ "rubbish", Litter::Type::rubbish },
{ "burger_box", Litter::Type::burgerBox },
{ "empty_cup", Litter::Type::emptyCup },
{ "empty_box", Litter::Type::emptyBox },
{ "empty_bottle", Litter::Type::emptyBottle },
{ "empty_bowl_red", Litter::Type::emptyBowlRed },
{ "empty_drink_carton", Litter::Type::emptyDrinkCarton },
{ "empty_juice_cup", Litter::Type::emptyJuiceCup },
{ "empty_bowl_blue", Litter::Type::emptyBowlBlue },
2025-07-31 11:01:48 +02:00
});
2021-08-09 20:32:20 +03:00
ScLitter gScLitter;
2025-09-29 02:05:00 +10:00
JSValue ScLitter::New(JSContext* ctx, EntityId entityId)
2021-08-09 20:32:20 +03:00
{
return gScEntity.NewDerivedInstance(ctx, entityId, gScLitter.GetProto());
2021-08-09 20:32:20 +03:00
}
void ScLitter::Register(JSContext* ctx)
2021-08-09 20:32:20 +03:00
{
2025-09-29 02:05:00 +10:00
static constexpr JSCFunctionListEntry funcs[] = {
JS_CGETSET_DEF("litterType", &ScLitter::litterType_get, &ScLitter::litterType_set),
JS_CGETSET_DEF("creationTick", &ScLitter::creationTick_get, nullptr)
};
gScLitter.RegisterDerived(ctx, gScEntity, funcs);
2021-08-09 20:32:20 +03:00
}
2025-09-29 02:05:00 +10:00
Litter* ScLitter::GetLitter(JSValue thisVal)
2021-08-09 20:32:20 +03:00
{
2025-09-29 02:05:00 +10:00
auto id = GetEntityId(thisVal);
return getGameState().entities.GetEntity<Litter>(id);
2021-08-09 20:32:20 +03:00
}
2025-09-29 02:05:00 +10:00
JSValue ScLitter::litterType_get(JSContext* ctx, JSValue thisVal)
2021-08-09 20:32:20 +03:00
{
2025-09-29 02:05:00 +10:00
auto* litter = GetLitter(thisVal);
if (litter != nullptr)
{
auto it = LitterTypeMap.find(litter->SubType);
if (it != LitterTypeMap.end())
{
2025-09-29 02:05:00 +10:00
return JSFromStdString(ctx, it->first);
}
}
2025-09-29 02:05:00 +10:00
return JS_UNDEFINED;
2021-08-09 20:32:20 +03:00
}
2025-09-29 02:05:00 +10:00
JSValue ScLitter::litterType_set(JSContext* ctx, JSValue thisVal, JSValue jsValue)
2021-08-09 20:32:20 +03:00
{
2025-09-29 02:05:00 +10:00
JS_UNPACK_STR(litterType, ctx, jsValue);
JS_THROW_IF_GAME_STATE_NOT_MUTABLE();
2021-08-09 20:32:20 +03:00
auto it = LitterTypeMap.find(litterType);
2025-09-29 02:05:00 +10:00
if (it != LitterTypeMap.end())
{
auto* litter = GetLitter(thisVal);
litter->SubType = it->second;
litter->Invalidate();
}
return JS_UNDEFINED;
2021-08-09 20:32:20 +03:00
}
2025-09-29 02:05:00 +10:00
JSValue ScLitter::creationTick_get(JSContext* ctx, JSValue thisVal)
2021-08-09 20:32:20 +03:00
{
2025-09-29 02:05:00 +10:00
auto* litter = GetLitter(thisVal);
return JS_NewUint32(ctx, litter == nullptr ? 0 : litter->creationTick);
2021-08-09 20:32:20 +03:00
}
} // namespace OpenRCT2::Scripting
#endif