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

306 lines
7.0 KiB
C++
Raw Normal View History

/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 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"
2015-08-22 16:39:24 +01:00
#include "../audio/audio.h"
#include "../core/DataSerialiser.h"
#include "../paint/sprite/Paint.Sprite.h"
2018-06-22 23:17:03 +02:00
#include "../scenario/Scenario.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>
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;
}
2021-08-25 17:42:09 +03:00
template<> bool EntityBase::Is<VehicleCrashParticle>() const
{
return Type == EntityType::CrashedVehicleParticle;
}
2021-08-25 17:42:09 +03:00
template<> bool EntityBase::Is<CrashSplashParticle>() const
{
return Type == EntityType::CrashSplash;
}
2015-08-22 16:39:24 +01:00
/**
*
* rct2: 0x006735A1
*/
2021-05-12 08:04:29 +01:00
void VehicleCrashParticle::Create(rct_vehicle_colour 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)
{
2017-06-06 23:24:18 +02:00
sprite->colour[0] = colours.body_colour;
sprite->colour[1] = colours.trim_colour;
2018-06-22 23:17:03 +02:00
sprite->sprite_width = 8;
2017-06-06 23:24:18 +02:00
sprite->sprite_height_negative = 8;
sprite->sprite_height_positive = 8;
sprite->MoveTo(vehiclePos);
2015-08-22 16:39:24 +01:00
2018-06-22 23:17:03 +02:00
sprite->frame = (scenario_rand() & 0xFF) * 12;
sprite->time_to_live = (scenario_rand() & 0x7F) + 140;
2020-04-17 16:08:22 -03:00
sprite->crashed_sprite_base = scenario_rand_max(static_cast<uint32_t>(std::size(vehicle_particle_base_sprites)));
sprite->acceleration_x = (static_cast<int16_t>(scenario_rand() & 0xFFFF)) * 4;
sprite->acceleration_y = (static_cast<int16_t>(scenario_rand() & 0xFFFF)) * 4;
2017-06-06 23:24:18 +02:00
sprite->acceleration_z = (scenario_rand() & 0xFFFF) * 4 + 0x10000;
2018-06-22 23:17:03 +02:00
sprite->velocity_x = 0;
sprite->velocity_y = 0;
sprite->velocity_z = 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: 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
2020-07-10 14:16:03 +01:00
int16_t landZ = tile_element_height(newLoc);
int16_t waterZ = tile_element_water_height(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
2020-07-10 14:16:03 +01:00
frame += 85;
if (frame >= 3072)
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;
}
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)
{
2018-06-22 23:17:03 +02:00
sprite->sprite_width = 33;
2017-06-06 23:24:18 +02:00
sprite->sprite_height_negative = 51;
sprite->sprite_height_positive = 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;
}
2021-05-29 08:07:32 +01:00
/**
*
* rct2: 0x006734B2
*/
void SteamParticle::Create(const CoordsXYZ& coords)
{
auto surfaceElement = map_get_surface_element_at(coords);
if (surfaceElement != nullptr && coords.z > surfaceElement->GetBaseZ())
{
SteamParticle* steam = CreateEntity<SteamParticle>();
if (steam == nullptr)
return;
steam->sprite_width = 20;
steam->sprite_height_negative = 18;
steam->sprite_height_positive = 16;
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;
}
2021-05-29 08:07:32 +01:00
/**
*
* rct2: 0x0067363D
*/
void ExplosionCloud::Create(const CoordsXYZ& cloudPos)
{
auto* entity = CreateEntity<ExplosionCloud>();
if (entity != nullptr)
{
entity->sprite_width = 44;
entity->sprite_height_negative = 32;
entity->sprite_height_positive = 34;
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;
}
2021-05-29 08:07:32 +01:00
/**
*
* rct2: 0x0067366B
*/
void ExplosionFlare::Create(const CoordsXYZ& flarePos)
{
auto* entity = CreateEntity<ExplosionFlare>();
if (entity != nullptr)
{
entity->sprite_width = 25;
entity->sprite_height_negative = 85;
entity->sprite_height_positive = 8;
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;
}