Skip to content

Commit 2870556

Browse files
authored
Allow for control of Tux's fancy idle animations via scripting (#3112)
fixes #3112 * Allow for control of Tux's fancy idle animations via scripting * Mark one liners as `inline` * PART ONE * part 2 * Format namespace * Simplify & Fix * Address feedback
1 parent 94e2b58 commit 2870556

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

src/object/player.cpp

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353

5454
const float TUX_INVINCIBLE_TIME_WARNING = 2.0f;
5555

56-
namespace {
57-
56+
namespace
57+
{
5858
/* Times: */
5959
const float TUX_SAFE_TIME = 1.8f;
6060
const float TUX_INVINCIBLE_TIME = 14.0f;
@@ -232,6 +232,8 @@ Player::Player(PlayerStatus& player_status, const std::string& name_, int player
232232
m_water_jump(false),
233233
m_airarrow(Surface::from_file("images/engine/hud/airarrow.png")),
234234
m_bubbles_sprite(SpriteManager::current()->create("images/particles/air_bubble.sprite")),
235+
m_should_fancy_idle(true),
236+
m_fancy_idle_active(true),
235237
m_floor_normal(0.0f, 0.0f),
236238
m_ghost_mode(false),
237239
m_unduck_hurt_timer(),
@@ -2189,42 +2191,60 @@ Player::draw(DrawingContext& context)
21892191
}
21902192
else
21912193
{
2192-
if (fabsf(m_physic.get_velocity_x()) < 1.0f) {
2193-
if (std::all_of(IDLE_STAGES.begin(), IDLE_STAGES.end(),
2194-
[this](const std::string& stage) { return m_sprite->get_action().find("-" + stage + "-") == std::string::npos; }))
2194+
if (fabsf(m_physic.get_velocity_x()) < 1.0f)
2195+
{
2196+
const bool is_not_idle = std::all_of(IDLE_STAGES.begin(), IDLE_STAGES.end(),
2197+
[this](const std::string& stage) { return m_sprite->get_action().find("-" + stage + "-") == std::string::npos; });
2198+
2199+
if (is_not_idle || (m_should_fancy_idle && !m_fancy_idle_active))
21952200
{
21962201
m_idle_stage = 0;
21972202
m_idle_timer.start(static_cast<float>(TIME_UNTIL_IDLE) / 1000.0f);
2203+
m_sprite->set_action(sa_prefix + ("-" + IDLE_STAGES[m_idle_stage]) + sa_postfix, Sprite::LOOPS_CONTINUED);
21982204

2199-
m_sprite->set_action(sa_prefix+("-" + IDLE_STAGES[m_idle_stage])+sa_postfix, Sprite::LOOPS_CONTINUED);
2200-
}
2201-
else if (m_idle_timer.check() || m_sprite->animation_done()) {
2202-
m_idle_stage++;
2203-
if (m_idle_stage >= static_cast<unsigned int>(IDLE_STAGES.size()))
2205+
if (!m_should_fancy_idle)
22042206
{
2205-
m_idle_stage = static_cast<int>(IDLE_STAGES.size()) - 1;
2206-
m_sprite->set_action(sa_prefix+("-" + IDLE_STAGES[m_idle_stage])+sa_postfix);
22072207
m_sprite->set_animation_loops(-1);
2208+
m_fancy_idle_active = false;
22082209
}
22092210
else
2211+
m_fancy_idle_active = true;
2212+
}
2213+
else if (m_should_fancy_idle)
2214+
{
2215+
if (m_idle_timer.check() || m_sprite->animation_done())
22102216
{
2211-
m_sprite->set_action(sa_prefix+("-" + IDLE_STAGES[m_idle_stage])+sa_postfix, 1);
2217+
m_idle_stage++;
2218+
2219+
if (m_idle_stage >= static_cast<unsigned int>(IDLE_STAGES.size()))
2220+
{
2221+
m_idle_stage = static_cast<int>(IDLE_STAGES.size()) - 1;
2222+
m_sprite->set_action(sa_prefix + ("-" + IDLE_STAGES[m_idle_stage]) + sa_postfix);
2223+
m_sprite->set_animation_loops(-1);
2224+
}
2225+
else
2226+
m_sprite->set_action(sa_prefix + ("-" + IDLE_STAGES[m_idle_stage]) + sa_postfix, 1);
22122227
}
22132228
}
2214-
else {
2215-
m_sprite->set_action(sa_prefix+("-" + IDLE_STAGES[m_idle_stage])+sa_postfix, Sprite::LOOPS_CONTINUED);
2229+
else
2230+
{
2231+
if (m_idle_stage != 0 || m_sprite->get_action() != sa_prefix + ("-" + IDLE_STAGES[0]) + sa_postfix)
2232+
{
2233+
m_idle_stage = 0;
2234+
m_sprite->set_action(sa_prefix + ("-" + IDLE_STAGES[0]) + sa_postfix);
2235+
m_sprite->set_animation_loops(-1);
2236+
}
2237+
m_fancy_idle_active = false;
22162238
}
22172239
}
22182240
else
22192241
{
2220-
if (std::abs(m_physic.get_velocity_x()) >= MAX_RUN_XM-3)
2221-
{
2222-
m_sprite->set_action(sa_prefix+"-run"+sa_postfix);
2223-
}
2242+
if (std::abs(m_physic.get_velocity_x()) >= MAX_RUN_XM - 3)
2243+
m_sprite->set_action(sa_prefix + "-run" + sa_postfix);
22242244
else
2225-
{
2226-
m_sprite->set_action(sa_prefix+"-walk"+sa_postfix);
2227-
}
2245+
m_sprite->set_action(sa_prefix + "-walk" + sa_postfix);
2246+
2247+
m_fancy_idle_active = false;
22282248
}
22292249
}
22302250

@@ -3082,6 +3102,8 @@ Player::register_class(ssq::VM& vm)
30823102
cls.addFunc("make_invincible", &Player::make_invincible);
30833103
cls.addFunc("deactivate", &Player::deactivate);
30843104
cls.addFunc("activate", &Player::activate);
3105+
cls.addFunc("enable_fancy_idling", &Player::enable_fancy_idling);
3106+
cls.addFunc("disable_fancy_idling", &Player::disable_fancy_idling);
30853107
cls.addFunc("walk", &Player::walk);
30863108
cls.addFunc("set_dir", &Player::set_dir);
30873109
cls.addFunc("set_visible", &Player::set_visible);

src/object/player.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,17 @@ class Player final : public MovingObject
390390
*/
391391
void deactivate();
392392

393+
/**
394+
* @scripting
395+
* @description Enables Tux's fancy idle animations.
396+
*/
397+
inline void enable_fancy_idling() { m_should_fancy_idle = true; }
398+
/**
399+
* @scripting
400+
* @description Disables Tux's fancy idle animations.
401+
*/
402+
inline void disable_fancy_idling() { m_should_fancy_idle = false; }
403+
393404
/**
394405
* @scripting
395406
* @description Gets whether the current input on the keyboard/controller/touchpad has been pressed.
@@ -612,6 +623,9 @@ class Player final : public MovingObject
612623
Timer m_bubble_timer; /**< timer for spawning bubble particles */
613624
std::list<std::pair<SpritePtr, Vector>> m_active_bubbles; /**< active bubble particles */
614625

626+
bool m_should_fancy_idle;
627+
bool m_fancy_idle_active;
628+
615629
Vector m_floor_normal;
616630

617631
bool m_ghost_mode; /**< indicates if Tux should float around and through solid objects */

0 commit comments

Comments
 (0)