From dc20fca17dc49d2c8360157c865244bfb3c76683 Mon Sep 17 00:00:00 2001 From: James R Date: Thu, 7 Dec 2023 14:35:47 -0800 Subject: [PATCH] Broly: convert to C++ --- src/objects/CMakeLists.txt | 2 +- src/objects/broly.c | 77 --------------------------- src/objects/broly.cpp | 106 +++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 78 deletions(-) delete mode 100644 src/objects/broly.c create mode 100644 src/objects/broly.cpp diff --git a/src/objects/CMakeLists.txt b/src/objects/CMakeLists.txt index b7dda6caf..73a671802 100644 --- a/src/objects/CMakeLists.txt +++ b/src/objects/CMakeLists.txt @@ -8,7 +8,7 @@ target_sources(SRB2SDL2 PRIVATE orbinaut.c jawz.c duel-bomb.c - broly.c + broly.cpp ufo.c monitor.c item-spot.c diff --git a/src/objects/broly.c b/src/objects/broly.c deleted file mode 100644 index 2419fac10..000000000 --- a/src/objects/broly.c +++ /dev/null @@ -1,77 +0,0 @@ -#include "../doomdef.h" -#include "../info.h" -#include "../k_kart.h" -#include "../k_objects.h" -#include "../m_easing.h" -#include "../p_local.h" -#include "../s_sound.h" - -/* An object may not be visible on the same tic: - 1) that it spawned - 2) that it cycles to the next state */ -#define BUFFER_TICS (2) - -#define broly_duration(o) ((o)->extravalue1) -#define broly_maxscale(o) ((o)->extravalue2) - -static inline fixed_t -get_unit_linear (const mobj_t *x) -{ - const tic_t t = (x->tics - BUFFER_TICS); - - return t * FRACUNIT / broly_duration(x); -} - -mobj_t * -Obj_SpawnBrolyKi -( mobj_t * source, - tic_t duration) -{ - mobj_t *x; - - if (duration <= 0) - { - return NULL; - } - - x = P_SpawnMobjFromMobj( - source, 0, 0, 0, MT_BROLY); - - P_SetTarget(&x->target, source); - - // Shrink into center of source object. - x->z = (source->z + source->height / 2); - - x->colorized = true; - x->color = source->color; - x->hitlag = 0; // do not copy source hitlag - - broly_maxscale(x) = 64 * mapobjectscale; - broly_duration(x) = duration; - - x->tics = (duration + BUFFER_TICS); - - K_ReduceVFXForEveryone(x); - - S_StartSound(x, sfx_cdfm74); - - return x; -} - -boolean -Obj_BrolyKiThink (mobj_t *x) -{ - if (broly_duration(x) <= 0) - { - P_RemoveMobj(x); - return false; - } - - const fixed_t - t = get_unit_linear(x), - n = Easing_OutSine(t, 0, broly_maxscale(x)); - - P_InstaScale(x, n); - - return true; -} diff --git a/src/objects/broly.cpp b/src/objects/broly.cpp new file mode 100644 index 000000000..ebca879af --- /dev/null +++ b/src/objects/broly.cpp @@ -0,0 +1,106 @@ +// DR. ROBOTNIK'S RING RACERS +//----------------------------------------------------------------------------- +// Copyright (C) 2023 by James Robert Roman +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- + +#include "../math/fixed.hpp" +#include "../mobj.hpp" + +#include "../doomdef.h" +#include "../info.h" +#include "../k_kart.h" +#include "../k_objects.h" +#include "../m_easing.h" +#include "../p_local.h" +#include "../s_sound.h" + +using srb2::Mobj; +using srb2::math::Fixed; + +namespace +{ + +struct Broly : Mobj +{ + /* An object may not be visible on the same tic: + 1) that it spawned + 2) that it cycles to the next state */ + static constexpr int kBufferTics = 2; + + void extravalue1() = delete; + tic_t duration() const { return mobj_t::extravalue1; } + void duration(tic_t n) { mobj_t::extravalue1 = n; } + + void extravalue2() = delete; + Fixed max_scale() const { return mobj_t::extravalue2; } + void max_scale(Fixed n) { mobj_t::extravalue2 = n; } + + bool valid() const { return duration(); } + + tic_t remaining() const { return tics - kBufferTics; } + + Fixed linear() const { return (remaining() * FRACUNIT) / duration(); } + + static Broly* spawn(Mobj* source, tic_t duration) + { + if (duration == 0) + { + return nullptr; + } + + Broly* x = source->spawn_from({}, MT_BROLY); + + x->target(source); + + // Shrink into center of source object. + x->z = (source->z + source->height / 2); + + x->colorized = true; + x->color = source->color; + x->mobj_t::hitlag = 0; // do not copy source hitlag + + x->max_scale(64 * mapobjectscale); + x->duration(duration); + + x->tics = (duration + kBufferTics); + + K_ReduceVFXForEveryone(x); + + x->voice(sfx_cdfm74); + + return x; + } + + bool think() + { + if (!valid()) + { + remove(); + return false; + } + + scale(Easing_OutSine(linear(), 0, max_scale())); + + return true; + } +}; + +}; // namespace + +mobj_t * +Obj_SpawnBrolyKi +( mobj_t * source, + tic_t duration) +{ + return Broly::spawn(static_cast(source), duration); +} + +boolean +Obj_BrolyKiThink (mobj_t *x) +{ + return static_cast(x)->think(); +}