diff --git a/src/SB/Core/x/xHudMeter.cpp b/src/SB/Core/x/xHudMeter.cpp index aeadd57ad..31af911b3 100644 --- a/src/SB/Core/x/xHudMeter.cpp +++ b/src/SB/Core/x/xHudMeter.cpp @@ -1,3 +1,171 @@ #include "xHudMeter.h" #include + +#include "xString.h" +#include "xMathInlines.h" + +namespace xhud +{ + namespace + { + static void add_global_tweaks() + { + } + } // namespace + + meter_widget::meter_widget(const meter_asset& asset) + : widget((xhud::asset&)asset), res((xhud::meter_asset&)asset), value(asset.start_value), + min_value(asset.min_value), max_value(asset.max_value), end_value(asset.start_value), + value_vel(0.0f), ping_delay(10.0f) + { + add_global_tweaks(); + } +} // namespace xhud + +void xhud::meter_widget::set_value(F32 v) +{ + F32 dvalue; + F32 sign; + + dvalue = v - value; + if (dvalue < -0.01f) + { + if (res.sound.start_decrement != 0) + { + xSndPlay(res.sound.start_decrement, 1.0f, 0.0f, 0x80, 0x0, NULL, SND_CAT_GAME, 0.0f); + } + + if (res.decrement_time < 0.01f) + { + set_value_immediate(v); + return; + } + + dvalue = -1.0f; + } + else if (dvalue > 0.01f) + { + if (res.sound.start_increment != 0) + { + xSndPlay(res.sound.start_increment, 1.0f, 0.0f, 0x80, 0x0, NULL, SND_CAT_GAME, 0.0f); + } + + if (res.increment_time < 0.01f) + { + set_value_immediate(v); + return; + } + + dvalue = 1.0f; + } + else + { + set_value_immediate(v); + return; + } + + end_value = dvalue; + + if (res.decrement_time == 0.0f) + { + printf("decrement time = 0 -- ass saved!\n"); + } + + dvalue = res.decrement_time > 1e-5f ? 1e-5f : dvalue / res.decrement_time; + value_vel = dvalue; + value_accel = 50.0f * res.decrement_time; + + if (xsqrt(2.0f * (v - value) / res.decrement_time) > 2.0f) + { + value_accel = 25.0f * res.decrement_time; + } +} + +void xhud::meter_widget::set_value_immediate(F32 v) +{ + value = v; + end_value = v; + value_vel = 0.0f; +} + +void xhud::meter_widget::destruct() +{ + xhud::widget::destruct(); +} + +U32 xhud::meter_widget::type() const +{ + static U32 myid = xStrHash(res.type_name()); + + return myid; +} + +bool xhud::meter_widget::is(U32 id) const +{ + bool isTheWidget = false; + + if (id == xhud::meter_widget::type() || xhud::widget::is(id)) + { + isTheWidget = true; + } + + return isTheWidget; +} + +void xhud::meter_widget::updater(F32 dt) +{ + F32 old_value; + F32 pitch; // This was Heavy Iron, idk why they chose a name that causes name collisions :( + F32 min_ping_time; + + xhud::widget::updater(dt); + + ping_delay += dt; + this->pitch += dt; + + if (value_vel != 0.0f) + { + old_value = value; + + value = value + dt * (0.5f * value_accel * dt); + value_vel += value_accel * dt; + + if (value_vel < 0.0f) + { + if (value <= end_value) + { + value = end_value; + end_value = 0.0f; + } + + pitch = range_limit(-4.0f * this->pitch, -10.0f, 6.5f); + min_ping_time = 0.05f * xpow(0.5f, 0.083333336f * pitch); + + if ((S32)value != (S32)old_value && res.sound.decrement != 0 && + ping_delay > min_ping_time) + { + ping_delay = 0.0f; + pings.play(res.sound.decrement, 1.0f, pitch, 0x80, 0, 0, SND_CAT_GAME); + } + } + else + { + if (value >= end_value) + { + value = end_value; + value_vel = 0.0f; + } + + pitch = range_limit(2.0f * this->pitch, -10.0f, 6.5f); + min_ping_time = 0.05f * xpow(0.5f, 0.083333336f * pitch); + + if ((S32)value != (S32)old_value && res.sound.increment != 0 && + ping_delay > min_ping_time) + { + ping_delay = 0.0f; + pings.play(res.sound.increment, 1.0f, pitch, 0x80, 0, 0, SND_CAT_GAME); + } + } + } +} diff --git a/src/SB/Core/x/xHudMeter.h b/src/SB/Core/x/xHudMeter.h index d9b6fb6ab..38ad44f4d 100644 --- a/src/SB/Core/x/xHudMeter.h +++ b/src/SB/Core/x/xHudMeter.h @@ -20,6 +20,11 @@ namespace xhud U32 start_decrement; U32 decrement; } sound; + + static const char* type_name() + { + return "hud:meter"; + } }; struct meter_widget : widget @@ -34,6 +39,20 @@ namespace xhud F32 ping_delay; F32 pitch; sound_queue<4> pings; + + meter_widget(const meter_asset& asset); + + void set_value(F32 v); + void set_value_immediate(F32 v); + void destruct(); + U32 type() const; + bool is(U32 id) const; + void updater(F32 dt); + + void update(F32 dt) + { + updater(dt); + } }; } // namespace xhud diff --git a/src/SB/Core/x/xSnd.h b/src/SB/Core/x/xSnd.h index f49476621..771ef4b0d 100644 --- a/src/SB/Core/x/xSnd.h +++ b/src/SB/Core/x/xSnd.h @@ -85,6 +85,16 @@ template struct sound_queue U32 _playing[N + 1]; S32 head; S32 tail; + + sound_queue() + { + head = 0; + tail = 0; + } + + void play(U32 id, F32 vol, F32 pitch, U32 priority, U32 flags, U32 parentID, + sound_category snd_category); + void push(U32 id); }; enum sound_effect