Skip to content

Commit

Permalink
initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
spacek531 committed May 17, 2024
1 parent 5b57071 commit b94943f
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 18 deletions.
45 changes: 45 additions & 0 deletions distribution/openrct2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2606,6 +2606,51 @@ declare global {
"waiting_to_depart" |
"waiting_to_start";

type CrashParticleType = 0 | 1 | 2 | 3 | 4;

/**
* Represents a vehicle explosion particle. They are emitted during a vehicle
* crash and will bounce until their timer expires and they are automatically
* removed.
*/
interface CrashedVehicleParticle extends Entity {
/**
* The colour of the particle. Only body and trim are used.
*/
colours: VehicleColour;

/**
* The lifetime of the particle in ticks. Default value 65535. Entity is
* automatically removed at 0.
*/
timeToLive: number;

/**
* The particle velocity.
*/
velocity: CoordsXYZ;

/**
* The x-component of the particle acceleration.
*/
acceleration: CoordsXYZ;

/**
* The type of crash particle.
*/
spriteBase: CrashParticleType;

/**
* The current frame of the crash particle.
*/
frame: number;

/**
* Sets the initial values of the crash particle using the game's default values.
*/
setupValues(colours: VehicleColour): void;
}

/**
* Represents a guest or staff member.
* @deprecated since version 34, use guest or staff instead.
Expand Down
36 changes: 21 additions & 15 deletions src/openrct2/entity/Particle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ template<> bool EntityBase::Is<CrashSplashParticle>() const
{
return Type == EntityType::CrashSplash;
}

void VehicleCrashParticle::SetupValues(VehicleColour& colours)
{
colour[0] = colours.Body;
colour[1] = colours.Trim;
SpriteData.Width = 8;
SpriteData.HeightMin = 8;
SpriteData.HeightMax = 8;

frame = (ScenarioRand() & 0xFF) * 12;
time_to_live = (ScenarioRand() & 0x7F) + 140;
crashed_sprite_base = ScenarioRandMax(static_cast<uint32_t>(std::size(_VehicleCrashParticleSprites)));
acceleration_x = (static_cast<int16_t>(ScenarioRand() & 0xFFFF)) * 4;
acceleration_y = (static_cast<int16_t>(ScenarioRand() & 0xFFFF)) * 4;
acceleration_z = (ScenarioRand() & 0xFFFF) * 4 + 0x10000;
velocity_x = 0;
velocity_y = 0;
velocity_z = 0;
}

/**
*
* rct2: 0x006735A1
Expand All @@ -56,22 +76,8 @@ void VehicleCrashParticle::Create(VehicleColour& colours, const CoordsXYZ& vehic
VehicleCrashParticle* sprite = CreateEntity<VehicleCrashParticle>();
if (sprite != nullptr)
{
sprite->colour[0] = colours.Body;
sprite->colour[1] = colours.Trim;
sprite->SpriteData.Width = 8;
sprite->SpriteData.HeightMin = 8;
sprite->SpriteData.HeightMax = 8;
sprite->MoveTo(vehiclePos);

sprite->frame = (ScenarioRand() & 0xFF) * 12;
sprite->time_to_live = (ScenarioRand() & 0x7F) + 140;
sprite->crashed_sprite_base = ScenarioRandMax(static_cast<uint32_t>(std::size(_VehicleCrashParticleSprites)));
sprite->acceleration_x = (static_cast<int16_t>(ScenarioRand() & 0xFFFF)) * 4;
sprite->acceleration_y = (static_cast<int16_t>(ScenarioRand() & 0xFFFF)) * 4;
sprite->acceleration_z = (ScenarioRand() & 0xFFFF) * 4 + 0x10000;
sprite->velocity_x = 0;
sprite->velocity_y = 0;
sprite->velocity_z = 0;
sprite->SetupValues(colours);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/openrct2/entity/Particle.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct VehicleCrashParticle : EntityBase
int32_t acceleration_y;
int32_t acceleration_z;
static void Create(VehicleColour& colours, const CoordsXYZ& vehiclePos);
void SetupValues(VehicleColour& colours);
void Update();
void Serialise(DataSerialiser& stream);
void Paint(PaintSession& session, int32_t imageDirection) const;
Expand Down Expand Up @@ -76,3 +77,4 @@ struct SteamParticle : EntityBase
void Serialise(DataSerialiser& stream);
void Paint(PaintSession& session, int32_t imageDirection) const;
};

4 changes: 3 additions & 1 deletion src/openrct2/libopenrct2.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@
<ClInclude Include="scenes\title\TitleSequencePlayer.h" />
<ClInclude Include="scripting\bindings\entity\ScGuest.hpp" />
<ClInclude Include="scripting\bindings\entity\ScLitter.hpp" />
<ClInclude Include="scripting\bindings\entity\ScParticle.hpp" />
<ClInclude Include="scripting\bindings\entity\ScPeep.hpp" />
<ClInclude Include="scripting\bindings\entity\ScStaff.hpp" />
<ClInclude Include="scripting\bindings\entity\ScVehicle.hpp" />
Expand Down Expand Up @@ -1030,6 +1031,7 @@
<ClCompile Include="scenes\title\TitleSequenceManager.cpp" />
<ClCompile Include="scripting\bindings\entity\ScGuest.cpp" />
<ClCompile Include="scripting\bindings\entity\ScLitter.cpp" />
<ClCompile Include="scripting\bindings\entity\ScParticle.cpp" />
<ClCompile Include="scripting\bindings\entity\ScStaff.cpp" />
<ClCompile Include="scripting\bindings\entity\ScVehicle.cpp" />
<ClCompile Include="scripting\bindings\network\ScNetwork.cpp" />
Expand Down Expand Up @@ -1084,4 +1086,4 @@
</ClCompile>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
</Project>
</Project>
2 changes: 2 additions & 0 deletions src/openrct2/scripting/ScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
# include "bindings/entity/ScPeep.hpp"
# include "bindings/entity/ScStaff.hpp"
# include "bindings/entity/ScVehicle.hpp"
# include "bindings/entity/ScParticle.hpp"
# include "bindings/game/ScCheats.hpp"
# include "bindings/game/ScConsole.hpp"
# include "bindings/game/ScContext.hpp"
Expand Down Expand Up @@ -433,6 +434,7 @@ void ScriptEngine::Initialise()
ScEntity::Register(ctx);
ScLitter::Register(ctx);
ScVehicle::Register(ctx);
ScCrashedVehicleParticle::Register(ctx);
ScPeep::Register(ctx);
ScGuest::Register(ctx);
ScThought::Register(ctx);
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/scripting/ScriptEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace OpenRCT2

namespace OpenRCT2::Scripting
{
static constexpr int32_t OPENRCT2_PLUGIN_API_VERSION = 87;
static constexpr int32_t OPENRCT2_PLUGIN_API_VERSION = 88;

// Versions marking breaking changes.
static constexpr int32_t API_VERSION_33_PEEP_DEPRECATION = 33;
Expand Down
181 changes: 181 additions & 0 deletions src/openrct2/scripting/bindings/entity/ScParticle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*****************************************************************************
* Copyright (c) 2014-2024 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.
*****************************************************************************/

#include "ScParticle.hpp"

#include "../ride/ScRide.hpp"

#ifdef ENABLE_SCRIPTING

namespace OpenRCT2::Scripting
{

ScCrashedVehicleParticle::ScCrashedVehicleParticle(EntityId id)
: ScEntity(id)
{
}
void ScCrashedVehicleParticle::Register(duk_context* ctx)
{
dukglue_set_base_class<ScEntity, ScCrashedVehicleParticle>(ctx);
dukglue_register_property(
ctx, &ScCrashedVehicleParticle::acceleration_get, &ScCrashedVehicleParticle::acceleration_set, "acceleration");
dukglue_register_property(
ctx, &ScCrashedVehicleParticle::velocity_get, &ScCrashedVehicleParticle::velocity_set, "velocity");
dukglue_register_property(
ctx, &ScCrashedVehicleParticle::colours_get, &ScCrashedVehicleParticle::colours_set, "colours");
dukglue_register_property(
ctx, &ScCrashedVehicleParticle::timeToLive_get, &ScCrashedVehicleParticle::timeToLive_set, "timeToLive");
dukglue_register_property(
ctx, &ScCrashedVehicleParticle::crashedSpriteBase_get, &ScCrashedVehicleParticle::crashedSpriteBase_set, "frame");
dukglue_register_property(ctx, &ScCrashedVehicleParticle::frame_get, &ScCrashedVehicleParticle::frame_set, "frame");
dukglue_register_method(ctx, &ScCrashedVehicleParticle::SetupValues, "setupValues");
}

VehicleCrashParticle* ScCrashedVehicleParticle::GetCrashedVehicleParticle() const
{
return ::GetEntity<VehicleCrashParticle>(_id);
}

void ScCrashedVehicleParticle::frame_set(uint16_t value)
{
auto entity = GetCrashedVehicleParticle();
if (entity != nullptr)
{
entity->frame = value;
}
}

uint16_t ScCrashedVehicleParticle::frame_get() const
{
auto entity = GetCrashedVehicleParticle();
if (entity != nullptr)
{
return entity->frame;
}
return 0;
}

void ScCrashedVehicleParticle::crashedSpriteBase_set(uint16_t value)
{
auto entity = GetCrashedVehicleParticle();
if (entity != nullptr)
{
entity->crashed_sprite_base = value;
}
}

uint16_t ScCrashedVehicleParticle::crashedSpriteBase_get() const
{
auto entity = GetCrashedVehicleParticle();
if (entity != nullptr)
{
return entity->crashed_sprite_base;
}
return 0;
}

void ScCrashedVehicleParticle::timeToLive_set(uint16_t value)
{
auto entity = GetCrashedVehicleParticle();
if (entity != nullptr)
{
entity->time_to_live = value;
}
}

uint16_t ScCrashedVehicleParticle::timeToLive_get() const
{
auto entity = GetCrashedVehicleParticle();
if (entity != nullptr)
{
return entity->time_to_live;
}
return 0;
}

void ScCrashedVehicleParticle::velocity_set(const DukValue& value)
{
auto entity = GetCrashedVehicleParticle();
if (entity != nullptr)
{
auto velocity = FromDuk<CoordsXYZ>(value);
entity->velocity_x = velocity.x;
entity->velocity_y = velocity.y;
entity->velocity_z = velocity.z;
}
}

DukValue ScCrashedVehicleParticle::velocity_get() const
{
auto ctx = GetContext()->GetScriptEngine().GetContext();
auto entity = GetCrashedVehicleParticle();
if (entity != nullptr)
{
return ToDuk(ctx, CoordsXYZ(entity->velocity_x, entity->velocity_y, entity->velocity_z));
}
return {};
}

void ScCrashedVehicleParticle::acceleration_set(const DukValue& value)
{
auto entity = GetCrashedVehicleParticle();
if (entity != nullptr)
{
auto acceleration = FromDuk<CoordsXYZ>(value);
entity->acceleration_x = acceleration.x;
entity->acceleration_y = acceleration.y;
entity->acceleration_z = acceleration.z;
}
}

DukValue ScCrashedVehicleParticle::acceleration_get() const
{
auto ctx = GetContext()->GetScriptEngine().GetContext();
auto entity = GetCrashedVehicleParticle();
if (entity != nullptr)
{
return ToDuk(ctx, CoordsXYZ(entity->acceleration_x, entity->acceleration_y, entity->acceleration_z));
}
return {};
}

void ScCrashedVehicleParticle::SetupValues(const DukValue& value)
{
auto entity = GetCrashedVehicleParticle();
if (entity != nullptr)
{
auto colours = FromDuk<VehicleColour>(value);
entity->SetupValues(colours);
}
}

DukValue ScCrashedVehicleParticle::colours_get() const
{
auto ctx = GetContext()->GetScriptEngine().GetContext();
auto entity = GetCrashedVehicleParticle();
if (entity != nullptr)
{
return ToDuk<VehicleColour>(ctx, VehicleColour(entity->colour[0], entity->colour[1], 0));
}
return ToDuk(ctx, nullptr);
}

void ScCrashedVehicleParticle::colours_set(const DukValue& value)
{
auto entity = GetCrashedVehicleParticle();
if (entity != nullptr)
{
auto colours = FromDuk<VehicleColour>(value);
entity->colour[0] = colours.Body;
entity->colour[1] = colours.Trim;
}
}
}; // namespace OpenRCT2::Scripting

#endif
Loading

0 comments on commit b94943f

Please sign in to comment.