Files
OpenRCT2-Web/src/openrct2/entity/Particle.cpp
T

377 lines
8.9 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "Particle.h"
#include "../SpriteIds.h"
2025-02-17 07:24:03 -05:00
#include "../audio/Audio.h"
#include "../core/DataSerialiser.h"
2021-11-27 15:37:32 +02:00
#include "../paint/Paint.h"
2022-01-29 05:29:08 -08:00
#include "../profiling/Profiling.h"
2022-08-13 20:30:30 +01:00
#include "../ride/VehicleColour.h"
2018-06-22 23:17:03 +02:00
#include "../scenario/Scenario.h"
2024-10-15 22:02:31 +02:00
#include "../world/tile_element/SurfaceElement.h"
2021-11-24 17:19:52 +02:00
#include "EntityRegistry.h"
2015-08-22 16:39:24 +01:00
2018-11-21 23:16:04 +01:00
#include <iterator>
2025-01-19 16:59:45 +01:00
static constexpr uint32_t kVehicleCrashParticleSprites[kCrashedVehicleParticleNumberTypes] = {
SPR_VEHICLE_CRASH_PARTICLE_1, SPR_VEHICLE_CRASH_PARTICLE_2, SPR_VEHICLE_CRASH_PARTICLE_3,
SPR_VEHICLE_CRASH_PARTICLE_4, SPR_VEHICLE_CRASH_PARTICLE_5,
2021-11-27 16:08:29 +02:00
};
template<>
bool EntityBase::Is<SteamParticle>() const
{
return Type == EntityType::SteamParticle;
}
template<>
bool EntityBase::Is<ExplosionFlare>() const
{
return Type == EntityType::ExplosionFlare;
}
template<>
bool EntityBase::Is<ExplosionCloud>() const
{
return Type == EntityType::ExplosionCloud;
}
template<>
bool EntityBase::Is<VehicleCrashParticle>() const
{
return Type == EntityType::CrashedVehicleParticle;
}
template<>
bool EntityBase::Is<CrashSplashParticle>() const
{
return Type == EntityType::CrashSplash;
}
2024-05-29 03:32:41 -07:00
void VehicleCrashParticle::SetSpriteData()
{
SpriteData.Width = 8;
SpriteData.HeightMin = 8;
SpriteData.HeightMax = 8;
}
void VehicleCrashParticle::Launch()
{
frame = (ScenarioRand() & 0xFF) * kCrashedVehicleParticleNumberSprites;
time_to_live = (ScenarioRand() & 0x7F) + 140;
crashed_sprite_base = ScenarioRandMax(kCrashedVehicleParticleNumberTypes);
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;
}
2015-08-22 16:39:24 +01:00
/**
*
* rct2: 0x006735A1
*/
2022-08-13 20:30:30 +01:00
void VehicleCrashParticle::Create(VehicleColour& colours, const CoordsXYZ& vehiclePos)
2015-08-22 16:39:24 +01:00
{
2021-03-16 11:18:06 +00:00
VehicleCrashParticle* sprite = CreateEntity<VehicleCrashParticle>();
2017-11-20 11:37:55 +01:00
if (sprite != nullptr)
{
2024-05-29 03:32:41 -07:00
sprite->MoveTo(vehiclePos);
2022-08-13 20:30:30 +01:00
sprite->colour[0] = colours.Body;
sprite->colour[1] = colours.Trim;
2024-05-29 03:32:41 -07:00
sprite->SetSpriteData();
sprite->Launch();
2017-06-06 23:24:18 +02:00
}
2015-08-22 16:39:24 +01:00
}
/**
*
2015-12-11 09:38:37 -06:00
* rct2: 0x00673298
2015-08-22 16:39:24 +01:00
*/
2020-07-10 14:16:03 +01:00
void VehicleCrashParticle::Update()
2015-08-22 16:39:24 +01:00
{
Invalidate();
2020-07-10 14:16:03 +01:00
time_to_live--;
if (time_to_live == 0)
2017-11-20 11:37:55 +01:00
{
2021-11-24 15:37:47 +02:00
EntityRemove(this);
2017-06-06 23:24:18 +02:00
return;
}
2015-08-22 16:39:24 +01:00
2017-06-06 23:24:18 +02:00
// Apply gravity
2020-07-10 14:16:03 +01:00
acceleration_z -= 5041;
2015-08-22 16:39:24 +01:00
2017-06-06 23:24:18 +02:00
// Apply air resistance
2020-07-10 14:16:03 +01:00
acceleration_x -= (acceleration_x / 256);
acceleration_y -= (acceleration_y / 256);
acceleration_z -= (acceleration_z / 256);
2015-08-22 16:39:24 +01:00
2017-06-06 23:24:18 +02:00
// Update velocity and position
2020-07-10 14:16:03 +01:00
int32_t vx = velocity_x + acceleration_x;
int32_t vy = velocity_y + acceleration_y;
int32_t vz = velocity_z + acceleration_z;
2015-08-22 16:39:24 +01:00
2020-07-10 14:16:03 +01:00
CoordsXYZ newLoc = { x + (vx >> 16), y + (vy >> 16), z + (vz >> 16) };
2015-08-22 16:39:24 +01:00
2020-07-10 14:16:03 +01:00
velocity_x = vx & 0xFFFF;
velocity_y = vy & 0xFFFF;
velocity_z = vz & 0xFFFF;
2015-08-22 16:39:24 +01:00
2017-06-06 23:24:18 +02:00
// Check collision with land / water
2022-10-12 06:35:20 +01:00
int16_t landZ = TileElementHeight(newLoc);
int16_t waterZ = TileElementWaterHeight(newLoc);
2015-08-22 16:39:24 +01:00
2020-07-10 14:16:03 +01:00
if (waterZ != 0 && z >= waterZ && newLoc.z <= waterZ)
2017-11-20 11:37:55 +01:00
{
2017-06-06 23:24:18 +02:00
// Splash
2020-10-07 00:34:42 +03:00
OpenRCT2::Audio::Play3D(OpenRCT2::Audio::SoundId::Water2, { x, y, waterZ });
2021-05-12 08:04:29 +01:00
CrashSplashParticle::Create({ x, y, waterZ });
2021-11-24 15:37:47 +02:00
EntityRemove(this);
2017-06-06 23:24:18 +02:00
return;
}
2015-08-22 16:39:24 +01:00
2020-07-10 14:16:03 +01:00
if (z >= landZ && newLoc.z <= landZ)
2017-11-20 11:37:55 +01:00
{
2017-06-06 23:24:18 +02:00
// Bounce
2020-07-10 14:16:03 +01:00
acceleration_z *= -1;
newLoc.z = landZ;
2017-06-06 23:24:18 +02:00
}
2020-07-10 14:16:03 +01:00
MoveTo(newLoc);
2015-08-22 16:39:24 +01:00
2024-05-29 03:32:41 -07:00
frame += kCrashedVehicleParticleFrameIncrement;
if (frame >= (kCrashedVehicleParticleNumberSprites * kCrashedVehicleParticleFrameToSprite))
2017-11-20 11:37:55 +01:00
{
2020-07-10 14:16:03 +01:00
frame = 0;
2017-06-06 23:24:18 +02:00
}
2015-08-22 16:39:24 +01:00
}
void VehicleCrashParticle::Serialise(DataSerialiser& stream)
{
EntityBase::Serialise(stream);
stream << frame;
stream << time_to_live;
stream << colour;
stream << crashed_sprite_base;
stream << velocity_x;
stream << velocity_y;
stream << velocity_z;
stream << acceleration_x;
stream << acceleration_y;
stream << acceleration_z;
}
void VehicleCrashParticle::Paint(PaintSession& session, int32_t imageDirection) const
2021-11-27 15:20:17 +02:00
{
2022-01-29 05:29:08 -08:00
PROFILED_FUNCTION();
RenderTarget& rt = session.DPI;
if (rt.zoom_level > ZoomLevel{ 0 })
{
return;
}
2025-01-19 16:59:45 +01:00
uint32_t imageId = kVehicleCrashParticleSprites[crashed_sprite_base] + frame / 256;
auto image = ImageId(imageId, colour[0], colour[1]);
PaintAddImageAsParent(session, image, { 0, 0, z }, { 1, 1, 0 });
2021-11-27 15:20:17 +02:00
}
2015-08-22 16:39:24 +01:00
/**
*
* rct2: 0x00673699
*/
2021-05-12 08:04:29 +01:00
void CrashSplashParticle::Create(const CoordsXYZ& splashPos)
2015-08-22 16:39:24 +01:00
{
2021-03-16 11:18:06 +00:00
auto* sprite = CreateEntity<CrashSplashParticle>();
2017-11-20 11:37:55 +01:00
if (sprite != nullptr)
{
2023-04-07 19:47:26 +03:00
sprite->SpriteData.Width = 33;
sprite->SpriteData.HeightMin = 51;
sprite->SpriteData.HeightMax = 16;
2020-06-21 13:41:11 -03:00
sprite->MoveTo(splashPos + CoordsXYZ{ 0, 0, 3 });
2018-06-22 23:17:03 +02:00
sprite->frame = 0;
2017-06-06 23:24:18 +02:00
}
2015-08-22 16:39:24 +01:00
}
/**
*
2015-12-11 09:38:37 -06:00
* rct2: 0x0067339D
2015-08-22 16:39:24 +01:00
*/
2020-07-10 14:16:03 +01:00
void CrashSplashParticle::Update()
2015-08-22 16:39:24 +01:00
{
Invalidate();
2020-07-10 14:16:03 +01:00
frame += 85;
if (frame >= 7168)
2017-11-20 11:37:55 +01:00
{
2021-11-24 15:37:47 +02:00
EntityRemove(this);
2017-06-06 23:24:18 +02:00
}
2015-08-22 16:39:24 +01:00
}
2021-05-29 08:07:32 +01:00
void CrashSplashParticle::Serialise(DataSerialiser& stream)
{
EntityBase::Serialise(stream);
stream << frame;
}
void CrashSplashParticle::Paint(PaintSession& session, int32_t imageDirection) const
2021-11-27 15:20:17 +02:00
{
2022-01-29 05:29:08 -08:00
PROFILED_FUNCTION();
uint32_t imageId = SPR_CRASH_SPLASH_PARTICLE + (frame / 256);
PaintAddImageAsParent(session, ImageId(imageId), { 0, 0, z }, { 1, 1, 0 });
2021-11-27 15:20:17 +02:00
}
2021-05-29 08:07:32 +01:00
/**
*
* rct2: 0x006734B2
*/
void SteamParticle::Create(const CoordsXYZ& coords)
{
auto surfaceElement = MapGetSurfaceElementAt(coords);
2021-05-29 08:07:32 +01:00
if (surfaceElement != nullptr && coords.z > surfaceElement->GetBaseZ())
{
SteamParticle* steam = CreateEntity<SteamParticle>();
if (steam == nullptr)
return;
2023-04-07 19:47:26 +03:00
steam->SpriteData.Width = 20;
steam->SpriteData.HeightMin = 18;
steam->SpriteData.HeightMax = 16;
2021-05-29 08:07:32 +01:00
steam->frame = 256;
steam->time_to_move = 0;
steam->MoveTo(coords);
}
}
/**
*
* rct2: 0x00673200
*/
void SteamParticle::Update()
{
// Move up 1 z every 3 ticks (Starts after 4 ticks)
Invalidate();
time_to_move++;
if (time_to_move >= 4)
{
time_to_move = 1;
MoveTo({ x, y, z + 1 });
}
frame += 64;
if (frame >= (56 * 64))
{
2021-11-24 15:37:47 +02:00
EntityRemove(this);
2021-05-29 08:07:32 +01:00
}
}
void SteamParticle::Serialise(DataSerialiser& stream)
{
EntityBase::Serialise(stream);
stream << frame;
stream << time_to_move;
}
void SteamParticle::Paint(PaintSession& session, int32_t imageDirection) const
2021-11-27 15:20:17 +02:00
{
2022-01-29 05:29:08 -08:00
PROFILED_FUNCTION();
uint32_t imageId = SPR_STEAM_PARTICLE + (frame / 256);
PaintAddImageAsParent(session, ImageId(imageId), { 0, 0, z }, { 1, 1, 0 });
2021-11-27 15:20:17 +02:00
}
2021-05-29 08:07:32 +01:00
/**
*
* rct2: 0x0067363D
*/
void ExplosionCloud::Create(const CoordsXYZ& cloudPos)
{
auto* entity = CreateEntity<ExplosionCloud>();
if (entity != nullptr)
{
2023-04-07 19:47:26 +03:00
entity->SpriteData.Width = 44;
entity->SpriteData.HeightMin = 32;
entity->SpriteData.HeightMax = 34;
2021-05-29 08:07:32 +01:00
entity->MoveTo(cloudPos + CoordsXYZ{ 0, 0, 4 });
entity->frame = 0;
}
}
/**
*
* rct2: 0x00673385
*/
void ExplosionCloud::Update()
{
Invalidate();
frame += 128;
if (frame >= (36 * 128))
{
2021-11-24 15:37:47 +02:00
EntityRemove(this);
2021-05-29 08:07:32 +01:00
}
}
void ExplosionCloud::Serialise(DataSerialiser& stream)
{
EntityBase::Serialise(stream);
stream << frame;
}
void ExplosionCloud::Paint(PaintSession& session, int32_t imageDirection) const
2021-11-27 15:20:17 +02:00
{
2022-01-29 05:29:08 -08:00
PROFILED_FUNCTION();
uint32_t imageId = SPR_EXPLOSION_CLOUD + (frame / 256);
PaintAddImageAsParent(session, ImageId(imageId), { 0, 0, z }, { 1, 1, 0 });
2021-11-27 15:20:17 +02:00
}
2021-05-29 08:07:32 +01:00
/**
*
* rct2: 0x0067366B
*/
void ExplosionFlare::Create(const CoordsXYZ& flarePos)
{
auto* entity = CreateEntity<ExplosionFlare>();
if (entity != nullptr)
{
2023-04-07 19:47:26 +03:00
entity->SpriteData.Width = 25;
entity->SpriteData.HeightMin = 85;
entity->SpriteData.HeightMax = 8;
2021-05-29 08:07:32 +01:00
entity->MoveTo(flarePos + CoordsXYZ{ 0, 0, 4 });
entity->frame = 0;
}
}
/**
*
* rct2: 0x006733B4
*/
void ExplosionFlare::Update()
{
Invalidate();
frame += 64;
if (frame >= (124 * 64))
{
2021-11-24 15:37:47 +02:00
EntityRemove(this);
2021-05-29 08:07:32 +01:00
}
}
void ExplosionFlare::Serialise(DataSerialiser& stream)
{
EntityBase::Serialise(stream);
stream << frame;
}
2021-11-27 15:20:17 +02:00
void ExplosionFlare::Paint(PaintSession& session, int32_t imageDirection) const
2021-11-27 15:20:17 +02:00
{
2022-01-29 05:29:08 -08:00
PROFILED_FUNCTION();
uint32_t imageId = SPR_EXPLOSION_FLARE + (frame / 256);
PaintAddImageAsParent(session, ImageId(imageId), { 0, 0, z }, { 1, 1, 0 });
2021-11-27 15:20:17 +02:00
}