Improve Sprite Misc Update (#12191)

* Use better types for SpriteMiscUpdate

* Change all sprite misc update functions to member functions
This commit is contained in:
Duncan 2020-07-10 14:16:03 +01:00 committed by GitHub
parent e5e70688c5
commit 4dc4bbea92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 80 deletions

View File

@ -334,24 +334,24 @@ void create_duck(const CoordsXY& pos)
sprite->duck.frame = 0;
}
void duck_update(Duck* duck)
void Duck::Update()
{
switch (static_cast<DUCK_STATE>(duck->state))
switch (static_cast<DUCK_STATE>(state))
{
case DUCK_STATE::FLY_TO_WATER:
duck->UpdateFlyToWater();
UpdateFlyToWater();
break;
case DUCK_STATE::SWIM:
duck->UpdateSwim();
UpdateSwim();
break;
case DUCK_STATE::DRINK:
duck->UpdateDrink();
UpdateDrink();
break;
case DUCK_STATE::DOUBLE_DRINK:
duck->UpdateDoubleDrink();
UpdateDoubleDrink();
break;
case DUCK_STATE::FLY_AWAY:
duck->UpdateFlyAway();
UpdateFlyAway();
break;
}
}

View File

@ -57,63 +57,61 @@ void crashed_vehicle_particle_create(rct_vehicle_colour colours, const CoordsXYZ
*
* rct2: 0x00673298
*/
void crashed_vehicle_particle_update(VehicleCrashParticle* particle)
void VehicleCrashParticle::Update()
{
particle->Invalidate0();
particle->time_to_live--;
if (particle->time_to_live == 0)
Invalidate0();
time_to_live--;
if (time_to_live == 0)
{
sprite_remove(particle);
sprite_remove(this);
return;
}
// Apply gravity
particle->acceleration_z -= 5041;
acceleration_z -= 5041;
// Apply air resistance
particle->acceleration_x -= (particle->acceleration_x / 256);
particle->acceleration_y -= (particle->acceleration_y / 256);
particle->acceleration_z -= (particle->acceleration_z / 256);
acceleration_x -= (acceleration_x / 256);
acceleration_y -= (acceleration_y / 256);
acceleration_z -= (acceleration_z / 256);
// Update velocity and position
int32_t vx = particle->velocity_x + particle->acceleration_x;
int32_t vy = particle->velocity_y + particle->acceleration_y;
int32_t vz = particle->velocity_z + particle->acceleration_z;
int32_t vx = velocity_x + acceleration_x;
int32_t vy = velocity_y + acceleration_y;
int32_t vz = velocity_z + acceleration_z;
int16_t x = particle->x + (vx >> 16);
int16_t y = particle->y + (vy >> 16);
int16_t z = particle->z + (vz >> 16);
CoordsXYZ newLoc = { x + (vx >> 16), y + (vy >> 16), z + (vz >> 16) };
particle->velocity_x = vx & 0xFFFF;
particle->velocity_y = vy & 0xFFFF;
particle->velocity_z = vz & 0xFFFF;
velocity_x = vx & 0xFFFF;
velocity_y = vy & 0xFFFF;
velocity_z = vz & 0xFFFF;
// Check collision with land / water
int16_t landZ = tile_element_height({ x, y });
int16_t waterZ = tile_element_water_height({ x, y });
int16_t landZ = tile_element_height(newLoc);
int16_t waterZ = tile_element_water_height(newLoc);
if (waterZ != 0 && particle->z >= waterZ && z <= waterZ)
if (waterZ != 0 && z >= waterZ && newLoc.z <= waterZ)
{
// Splash
audio_play_sound_at_location(SoundId::Water2, { particle->x, particle->y, waterZ });
crash_splash_create({ particle->x, particle->y, waterZ });
sprite_remove(particle);
audio_play_sound_at_location(SoundId::Water2, { x, y, waterZ });
crash_splash_create({ x, y, waterZ });
sprite_remove(this);
return;
}
if (particle->z >= landZ && z <= landZ)
if (z >= landZ && newLoc.z <= landZ)
{
// Bounce
particle->acceleration_z *= -1;
z = landZ;
acceleration_z *= -1;
newLoc.z = landZ;
}
particle->MoveTo({ x, y, z });
particle->Invalidate0();
MoveTo(newLoc);
Invalidate0();
particle->frame += 85;
if (particle->frame >= 3072)
frame += 85;
if (frame >= 3072)
{
particle->frame = 0;
frame = 0;
}
}
@ -140,12 +138,12 @@ void crash_splash_create(const CoordsXYZ& splashPos)
*
* rct2: 0x0067339D
*/
void crash_splash_update(CrashSplashParticle* splash)
void CrashSplashParticle::Update()
{
splash->Invalidate2();
splash->frame += 85;
if (splash->frame >= 7168)
Invalidate2();
frame += 85;
if (frame >= 7168)
{
sprite_remove(splash);
sprite_remove(this);
}
}

View File

@ -533,21 +533,21 @@ static void move_sprite_to_list(SpriteBase* sprite, EntityListId newListIndex)
*
* rct2: 0x00673200
*/
static void sprite_steam_particle_update(SteamParticle* steam)
void SteamParticle::Update()
{
steam->Invalidate2();
Invalidate2();
// Move up 1 z every 3 ticks (Starts after 4 ticks)
steam->time_to_move++;
if (steam->time_to_move >= 4)
time_to_move++;
if (time_to_move >= 4)
{
steam->time_to_move = 1;
steam->MoveTo({ steam->x, steam->y, steam->z + 1 });
time_to_move = 1;
MoveTo({ x, y, z + 1 });
}
steam->frame += 64;
if (steam->frame >= (56 * 64))
frame += 64;
if (frame >= (56 * 64))
{
sprite_remove(steam);
sprite_remove(this);
}
}
@ -574,13 +574,13 @@ void sprite_misc_explosion_cloud_create(const CoordsXYZ& cloudPos)
*
* rct2: 0x00673385
*/
static void sprite_misc_explosion_cloud_update(rct_sprite* sprite)
void ExplosionCloud::Update()
{
sprite->generic.Invalidate2();
sprite->generic.frame += 128;
if (sprite->generic.frame >= (36 * 128))
Invalidate2();
frame += 128;
if (frame >= (36 * 128))
{
sprite_remove(&sprite->generic);
sprite_remove(this);
}
}
@ -607,13 +607,13 @@ void sprite_misc_explosion_flare_create(const CoordsXYZ& flarePos)
*
* rct2: 0x006733B4
*/
static void sprite_misc_explosion_flare_update(rct_sprite* sprite)
void ExplosionFlare::Update()
{
sprite->generic.Invalidate2();
sprite->generic.frame += 64;
if (sprite->generic.frame >= (124 * 64))
Invalidate2();
frame += 64;
if (frame >= (124 * 64))
{
sprite_remove(&sprite->generic);
sprite_remove(this);
}
}
@ -621,37 +621,37 @@ static void sprite_misc_explosion_flare_update(rct_sprite* sprite)
*
* rct2: 0x006731CD
*/
static void sprite_misc_update(rct_sprite* sprite)
static void sprite_misc_update(SpriteBase* sprite)
{
switch (sprite->generic.type)
switch (sprite->type)
{
case SPRITE_MISC_STEAM_PARTICLE:
sprite_steam_particle_update(reinterpret_cast<SteamParticle*>(sprite));
sprite->As<SteamParticle>()->Update();
break;
case SPRITE_MISC_MONEY_EFFECT:
sprite->money_effect.Update();
sprite->As<MoneyEffect>()->Update();
break;
case SPRITE_MISC_CRASHED_VEHICLE_PARTICLE:
crashed_vehicle_particle_update(reinterpret_cast<VehicleCrashParticle*>(sprite));
sprite->As<VehicleCrashParticle>()->Update();
break;
case SPRITE_MISC_EXPLOSION_CLOUD:
sprite_misc_explosion_cloud_update(sprite);
sprite->As<ExplosionCloud>()->Update();
break;
case SPRITE_MISC_CRASH_SPLASH:
crash_splash_update(reinterpret_cast<CrashSplashParticle*>(sprite));
sprite->As<CrashSplashParticle>()->Update();
break;
case SPRITE_MISC_EXPLOSION_FLARE:
sprite_misc_explosion_flare_update(sprite);
sprite->As<ExplosionFlare>()->Update();
break;
case SPRITE_MISC_JUMPING_FOUNTAIN_WATER:
case SPRITE_MISC_JUMPING_FOUNTAIN_SNOW:
sprite->jumping_fountain.Update();
sprite->As<JumpingFountain>()->Update();
break;
case SPRITE_MISC_BALLOON:
balloon_update(&sprite->balloon);
sprite->As<Balloon>()->Update();
break;
case SPRITE_MISC_DUCK:
duck_update(&sprite->duck);
sprite->As<Duck>()->Update();
break;
}
}
@ -664,8 +664,7 @@ void sprite_misc_update_all()
{
for (auto entity : EntityList(EntityListId::Misc))
{
// TODO: Use more specific Sprite class
sprite_misc_update(reinterpret_cast<rct_sprite*>(entity));
sprite_misc_update(entity);
}
}

View File

@ -61,15 +61,18 @@ struct Duck : SpriteGeneric
int16_t target_y;
uint8_t state;
void Update();
uint32_t GetFrameImage(int32_t direction) const;
void Invalidate();
bool IsFlying();
void Remove();
private:
void UpdateFlyToWater();
void UpdateSwim();
void UpdateDrink();
void UpdateDoubleDrink();
void UpdateFlyAway();
uint32_t GetFrameImage(int32_t direction) const;
void Invalidate();
bool IsFlying();
void Remove();
};
struct MoneyEffect : SpriteBase
@ -98,23 +101,30 @@ struct VehicleCrashParticle : SpriteGeneric
int32_t acceleration_x;
int32_t acceleration_y;
int32_t acceleration_z;
void Update();
};
struct ExplosionFlare : SpriteGeneric
{
void Update();
};
struct ExplosionCloud : SpriteGeneric
{
void Update();
};
struct CrashSplashParticle : SpriteGeneric
{
void Update();
};
struct SteamParticle : SpriteGeneric
{
uint16_t time_to_move;
void Update();
};
#pragma pack(push, 1)