Files
OpenRCT2-Web/src/openrct2/scripting/ScEntity.hpp
T

177 lines
4.9 KiB
C++
Raw Normal View History

2018-06-17 15:48:42 +02:00
/*****************************************************************************
2020-04-22 16:06:29 +01:00
* Copyright (c) 2014-2020 OpenRCT2 developers
2018-06-17 15:48:42 +02: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.
*****************************************************************************/
2018-03-20 19:40:38 +00:00
#pragma once
#ifdef ENABLE_SCRIPTING
2020-02-23 15:33:01 +00:00
# include "../common.h"
# include "../world/Sprite.h"
# include "Duktape.hpp"
# include "ScriptEngine.h"
2018-07-31 17:59:48 +02:00
2018-03-20 19:40:38 +00:00
namespace OpenRCT2::Scripting
{
2020-04-21 19:32:08 +01:00
class ScEntity
2018-03-20 19:40:38 +00:00
{
2020-03-01 13:22:17 +00:00
protected:
2020-03-01 12:44:36 +00:00
uint16_t _id = SPRITE_INDEX_NULL;
2018-03-20 19:40:38 +00:00
public:
2020-04-21 19:32:08 +01:00
ScEntity(uint16_t id)
2020-03-01 12:44:36 +00:00
: _id(id)
2018-03-20 19:40:38 +00:00
{
}
2020-03-01 12:44:36 +00:00
private:
2020-04-24 21:18:58 +01:00
std::string type_get() const
2018-03-20 19:40:38 +00:00
{
2020-04-21 19:32:08 +01:00
auto entity = GetEntity();
if (entity != nullptr)
2018-03-20 19:40:38 +00:00
{
2020-04-21 19:32:08 +01:00
switch (entity->sprite_identifier)
2020-03-01 12:44:36 +00:00
{
case SPRITE_IDENTIFIER_VEHICLE:
return "car";
case SPRITE_IDENTIFIER_PEEP:
return "peep";
case SPRITE_IDENTIFIER_MISC:
2020-04-21 19:32:08 +01:00
switch (entity->type)
2020-03-01 12:44:36 +00:00
{
case SPRITE_MISC_BALLOON:
return "balloon";
case SPRITE_MISC_DUCK:
return "duck";
}
break;
case SPRITE_IDENTIFIER_LITTER:
return "litter";
}
2018-03-20 19:40:38 +00:00
}
return "unknown";
}
2018-08-11 16:54:13 +02:00
// x getter and setter
2020-04-24 21:18:58 +01:00
int32_t x_get() const
2018-07-31 17:59:48 +02:00
{
2020-04-21 19:32:08 +01:00
auto entity = GetEntity();
return entity != nullptr ? entity->x : 0;
2018-07-31 17:59:48 +02:00
}
2018-08-11 16:54:13 +02:00
void x_set(int32_t value)
{
2020-02-23 02:00:22 +00:00
ThrowIfGameStateNotMutable();
2020-04-21 19:32:08 +01:00
auto entity = GetEntity();
if (entity != nullptr)
2020-03-01 12:44:36 +00:00
{
2020-04-21 19:32:08 +01:00
sprite_move(value, entity->y, entity->z, entity);
2020-03-01 12:44:36 +00:00
}
2018-08-11 16:54:13 +02:00
}
// y getter and setter
2020-04-24 21:18:58 +01:00
int32_t y_get() const
2018-07-31 17:59:48 +02:00
{
2020-04-21 19:32:08 +01:00
auto entity = GetEntity();
return entity != nullptr ? entity->y : 0;
2018-07-31 17:59:48 +02:00
}
2018-08-11 16:54:13 +02:00
void y_set(int32_t value)
{
2020-02-23 02:00:22 +00:00
ThrowIfGameStateNotMutable();
2020-04-21 19:32:08 +01:00
auto entity = GetEntity();
if (entity != nullptr)
2020-03-01 12:44:36 +00:00
{
2020-04-21 19:32:08 +01:00
sprite_move(entity->x, value, entity->z, entity);
2020-03-01 12:44:36 +00:00
}
2018-08-11 16:54:13 +02:00
}
// z getter and setter
2020-04-24 21:18:58 +01:00
int16_t z_get() const
2018-07-31 17:59:48 +02:00
{
2020-04-21 19:32:08 +01:00
auto entity = GetEntity();
return entity != nullptr ? entity->z : 0;
2018-07-31 17:59:48 +02:00
}
2018-08-11 16:54:13 +02:00
void z_set(int16_t value)
{
2020-02-23 02:00:22 +00:00
ThrowIfGameStateNotMutable();
2020-04-21 19:32:08 +01:00
auto entity = GetEntity();
if (entity != nullptr)
2020-03-01 12:44:36 +00:00
{
2020-04-21 19:32:08 +01:00
sprite_move(entity->x, entity->y, value, entity);
2020-03-01 12:44:36 +00:00
}
2018-08-11 16:54:13 +02:00
}
2018-03-20 19:40:38 +00:00
2020-04-24 21:18:58 +01:00
SpriteBase* GetEntity() const
2020-03-01 13:22:17 +00:00
{
return &get_sprite(_id)->generic;
}
public:
static void Register(duk_context* ctx)
{
2020-04-21 19:32:08 +01:00
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");
2020-03-01 13:22:17 +00:00
}
};
2020-04-21 19:32:08 +01:00
class ScPeep : public ScEntity
2020-03-01 13:22:17 +00:00
{
public:
ScPeep(uint16_t id)
2020-04-21 19:32:08 +01:00
: ScEntity(id)
2020-03-01 13:22:17 +00:00
{
}
private:
2020-04-24 21:18:58 +01:00
uint8_t tshirtColour_get() const
2018-07-31 17:59:48 +02:00
{
2020-03-01 12:44:36 +00:00
auto peep = GetPeep();
return peep != nullptr ? peep->tshirt_colour : 0;
2018-07-31 17:59:48 +02:00
}
void tshirtColour_set(uint8_t value)
{
2020-02-23 02:00:22 +00:00
ThrowIfGameStateNotMutable();
2020-03-01 12:44:36 +00:00
auto peep = GetPeep();
if (peep != nullptr)
{
peep->tshirt_colour = value;
}
2018-07-31 17:59:48 +02:00
}
2020-04-24 21:18:58 +01:00
uint8_t trousersColour_get() const
2018-07-31 17:59:48 +02:00
{
2020-03-01 12:44:36 +00:00
auto peep = GetPeep();
return peep != nullptr ? peep->trousers_colour : 0;
2018-07-31 17:59:48 +02:00
}
void trousersColour_set(uint8_t value)
{
2020-02-23 02:00:22 +00:00
ThrowIfGameStateNotMutable();
2020-03-01 12:44:36 +00:00
auto peep = GetPeep();
if (peep != nullptr)
{
peep->trousers_colour = value;
}
2018-07-31 17:59:48 +02:00
}
2018-03-20 19:40:38 +00:00
2020-04-24 21:18:58 +01:00
Peep* GetPeep() const
2020-03-01 12:44:36 +00:00
{
return get_sprite(_id)->AsPeep();
}
public:
2018-07-31 17:59:48 +02:00
static void Register(duk_context* ctx)
2018-03-20 19:40:38 +00:00
{
2020-04-21 19:32:08 +01:00
dukglue_set_base_class<ScEntity, ScPeep>(ctx);
2020-03-01 13:22:17 +00:00
dukglue_register_property(ctx, &ScPeep::tshirtColour_get, &ScPeep::tshirtColour_set, "tshirtColour");
dukglue_register_property(ctx, &ScPeep::trousersColour_get, &ScPeep::trousersColour_set, "trousersColour");
2018-03-20 19:40:38 +00:00
}
};
2020-03-01 13:22:17 +00:00
2018-07-31 17:59:48 +02:00
} // namespace OpenRCT2::Scripting
2020-02-23 15:33:01 +00:00
#endif