/***************************************************************************** * Copyright (c) 2014-2020 OpenRCT2 developers * * 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. *****************************************************************************/ #pragma once #ifdef ENABLE_SCRIPTING # include "../common.h" # include "../world/Sprite.h" # include "Duktape.hpp" # include "ScriptEngine.h" namespace OpenRCT2::Scripting { class ScEntity { protected: uint16_t _id = SPRITE_INDEX_NULL; public: ScEntity(uint16_t id) : _id(id) { } private: std::string type_get() const { auto entity = GetEntity(); if (entity != nullptr) { switch (entity->sprite_identifier) { case SPRITE_IDENTIFIER_VEHICLE: return "car"; case SPRITE_IDENTIFIER_PEEP: return "peep"; case SPRITE_IDENTIFIER_MISC: switch (entity->type) { case SPRITE_MISC_BALLOON: return "balloon"; case SPRITE_MISC_DUCK: return "duck"; } break; case SPRITE_IDENTIFIER_LITTER: return "litter"; } } return "unknown"; } // x getter and setter int32_t x_get() const { auto entity = GetEntity(); return entity != nullptr ? entity->x : 0; } void x_set(int32_t value) { ThrowIfGameStateNotMutable(); auto entity = GetEntity(); if (entity != nullptr) { sprite_move(value, entity->y, entity->z, entity); } } // y getter and setter int32_t y_get() const { auto entity = GetEntity(); return entity != nullptr ? entity->y : 0; } void y_set(int32_t value) { ThrowIfGameStateNotMutable(); auto entity = GetEntity(); if (entity != nullptr) { sprite_move(entity->x, value, entity->z, entity); } } // z getter and setter int16_t z_get() const { auto entity = GetEntity(); return entity != nullptr ? entity->z : 0; } void z_set(int16_t value) { ThrowIfGameStateNotMutable(); auto entity = GetEntity(); if (entity != nullptr) { sprite_move(entity->x, entity->y, value, entity); } } SpriteBase* GetEntity() const { return &get_sprite(_id)->generic; } public: static void Register(duk_context* ctx) { dukglue_register_property(ctx, &ScEntity::type_get, nullptr, "type"); dukglue_register_property(ctx, &ScEntity::x_get, &ScEntity::x_set, "x"); dukglue_register_property(ctx, &ScEntity::y_get, &ScEntity::y_set, "y"); dukglue_register_property(ctx, &ScEntity::z_get, &ScEntity::z_set, "z"); } }; class ScPeep : public ScEntity { public: ScPeep(uint16_t id) : ScEntity(id) { } private: uint8_t tshirtColour_get() const { auto peep = GetPeep(); return peep != nullptr ? peep->tshirt_colour : 0; } void tshirtColour_set(uint8_t value) { ThrowIfGameStateNotMutable(); auto peep = GetPeep(); if (peep != nullptr) { peep->tshirt_colour = value; } } uint8_t trousersColour_get() const { auto peep = GetPeep(); return peep != nullptr ? peep->trousers_colour : 0; } void trousersColour_set(uint8_t value) { ThrowIfGameStateNotMutable(); auto peep = GetPeep(); if (peep != nullptr) { peep->trousers_colour = value; } } Peep* GetPeep() const { return get_sprite(_id)->AsPeep(); } public: static void Register(duk_context* ctx) { dukglue_set_base_class(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