Skip to content

Commit 7395cbc

Browse files
committed
Code quality improvements
1 parent a884148 commit 7395cbc

File tree

6 files changed

+46
-61
lines changed

6 files changed

+46
-61
lines changed

src/badguy/dispenser.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Dispenser::Dispenser(const ReaderMapping& reader) :
8383
SoundManager::current()->preload("sounds/squish.wav");
8484
reader.get("cycle", m_cycle, 5.0f);
8585
if (reader.get("gravity", m_gravity)) m_physic.enable_gravity(true);
86-
if ( !reader.get("badguy", m_badguys)) m_badguys.clear();
86+
if (!reader.get("badguy", m_badguys)) m_badguys.clear();
8787
reader.get("random", m_random, false);
8888
std::string type_s = "dropper"; //default
8989
reader.get("type", type_s, "");
@@ -160,7 +160,8 @@ Dispenser::activate()
160160
return;
161161

162162
if (m_autotarget && !m_swivel)
163-
{ // auto cannon sprite might be wrong
163+
{
164+
// auto cannon sprite might be wrong
164165
auto* player = get_nearest_player();
165166
if (player)
166167
{
@@ -206,7 +207,8 @@ Dispenser::collision(GameObject& other, const CollisionHit& hit)
206207
{
207208
auto player = dynamic_cast<Player*> (&other);
208209
if (player)
209-
{ // hit from above?
210+
{
211+
// hit from above?
210212
if (player->get_bbox().get_bottom() < (m_col.m_bbox.get_top() + 16))
211213
{
212214
collision_squished(*player);
@@ -237,7 +239,7 @@ Dispenser::active_update(float dt_sec)
237239
// auto always shoots in Tux's direction
238240
if (m_autotarget)
239241
{
240-
if ( m_sprite->animation_done())
242+
if (m_sprite->animation_done())
241243
{
242244
m_sprite->set_action(m_dir == Direction::LEFT ? "working-left" : "working-right");
243245
m_swivel = false;
@@ -247,13 +249,16 @@ Dispenser::active_update(float dt_sec)
247249
if (player && !m_swivel)
248250
{
249251
Direction targetdir = (player->get_pos().x > get_pos().x) ? Direction::RIGHT : Direction::LEFT;
250-
if ( m_dir != targetdir )
251-
{ // no target: swivel cannon
252+
if (m_dir != targetdir)
253+
{
254+
// no target: swivel cannon
252255
m_swivel = true;
253256
m_dir = targetdir;
254257
m_sprite->set_action(m_dir == Direction::LEFT ? "swivel-left" : "swivel-right", 1);
255-
} else
256-
{ // tux in sight: shoot
258+
}
259+
else
260+
{
261+
// tux in sight: shoot
257262
launch_badguy();
258263
}
259264
}
@@ -278,10 +283,10 @@ Dispenser::launch_badguy()
278283
if (!is_offscreen() && !Editor::is_active())
279284
{
280285
Direction launchdir = m_dir;
281-
if ( !m_autotarget && m_start_dir == Direction::AUTO )
286+
if (!m_autotarget && m_start_dir == Direction::AUTO)
282287
{
283288
Player* player = get_nearest_player();
284-
if ( player )
289+
if (player)
285290
launchdir = (player->get_pos().x > get_pos().x) ? Direction::RIGHT : Direction::LEFT;
286291
}
287292

src/badguy/ghosttree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ GhostTree::active_update(float /*dt_sec*/)
158158
/* TODO indicate root with an animation */
159159
auto player = get_nearest_player();
160160
if (player)
161-
Sector::get().add<Root>(Vector(player->get_bbox().get_left(), (m_flip == NO_FLIP ? (m_col.m_bbox.get_bottom()+ROOT_TOP_OFFSET) : (m_col.m_bbox.get_top()-ROOT_TOP_OFFSET-ROOT_HEIGHT))), m_flip);
161+
Sector::get().add<Root>(Vector(player->get_bbox().get_left(), (m_flip == NO_FLIP ? (m_col.m_bbox.get_bottom() + ROOT_TOP_OFFSET) : (m_col.m_bbox.get_top() - ROOT_TOP_OFFSET - ROOT_HEIGHT))), m_flip);
162162
}
163163
} else if (mystate == STATE_SWALLOWING) {
164164
if (suck_lantern) {

src/object/coin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,11 @@ HeavyCoin::after_editor_set()
344344
MovingSprite::after_editor_set();
345345
}
346346

347-
// Call on_flip from grandparent class HeavyCoin to avoid flipping of
348-
// gravity-affected object HeavyCoin
349347
void
350348
HeavyCoin::on_flip(float height)
351349
{
350+
// Call on_flip from grandparent class MovingSprite to
351+
// avoid flipping of gravity-affected object HeavyCoin
352352
MovingSprite::on_flip(height);
353353
}
354354

src/object/firefly.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class Firefly final : public MovingSprite
4545
SpritePtr m_sprite_light;
4646
bool activated;
4747
Vector initial_position; /**< position as in level file. This is where Tux will have to respawn, as the level is reset every time */
48+
49+
private:
50+
Firefly(const Firefly&) = delete;
51+
Firefly& operator=(const Firefly&) = delete;
4852
};
4953

5054
#endif

src/object/ispy.cpp

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SuperTux - Ispy
22
// Copyright (C) 2007 Christoph Sommer <[email protected]>
3+
// 2022 Jiri Palecek <[email protected]>
34
//
45
// This program is free software: you can redistribute it and/or modify
56
// it under the terms of the GNU General Public License as published by
@@ -41,14 +42,7 @@ Ispy::Ispy(const ReaderMapping& reader) :
4142
if (m_dir == Direction::AUTO)
4243
log_warning << "Setting an Ispy's direction to AUTO is no good idea." << std::endl;
4344

44-
switch (m_dir)
45-
{
46-
case Direction::DOWN: m_sprite->set_action("idle-down"); break;
47-
case Direction::UP: m_sprite->set_action("idle-up"); break;
48-
case Direction::LEFT: m_sprite->set_action("idle-left"); break;
49-
case Direction::RIGHT: m_sprite->set_action("idle-right"); break;
50-
default: break;
51-
}
45+
set_sprite_action("idle");
5246
}
5347

5448
ObjectSettings
@@ -68,14 +62,7 @@ void
6862
Ispy::after_editor_set()
6963
{
7064
MovingSprite::after_editor_set();
71-
switch (m_dir)
72-
{
73-
case Direction::DOWN: m_sprite->set_action("idle-down"); break;
74-
case Direction::UP: m_sprite->set_action("idle-up"); break;
75-
case Direction::LEFT: m_sprite->set_action("idle-left"); break;
76-
case Direction::RIGHT: m_sprite->set_action("idle-right"); break;
77-
default: break;
78-
}
65+
set_sprite_action("idle");
7966
}
8067

8168
HitResponse
@@ -104,29 +91,15 @@ Ispy::update(float dt_sec)
10491

10592
if (Sector::get().can_see_player(eye))
10693
{
107-
switch (m_dir)
108-
{
109-
case Direction::DOWN: m_sprite->set_action("alert-down", 1); break;
110-
case Direction::UP: m_sprite->set_action("alert-up", 1); break;
111-
case Direction::LEFT: m_sprite->set_action("alert-left", 1); break;
112-
case Direction::RIGHT: m_sprite->set_action("alert-right", 1); break;
113-
default: break;
114-
}
94+
set_sprite_action("alert", 1);
11595
m_state = ISPYSTATE_ALERT;
11696
}
11797
}
11898
if (m_state == ISPYSTATE_ALERT)
11999
{
120100
if (m_sprite->animation_done())
121101
{
122-
switch (m_dir)
123-
{
124-
case Direction::DOWN: m_sprite->set_action("hiding-down", 1); break;
125-
case Direction::UP: m_sprite->set_action("hiding-up", 1); break;
126-
case Direction::LEFT: m_sprite->set_action("hiding-left", 1); break;
127-
case Direction::RIGHT: m_sprite->set_action("hiding-right", 1); break;
128-
default: break;
129-
}
102+
set_sprite_action("hiding", 1);
130103
m_state = ISPYSTATE_HIDING;
131104

132105
Sector::get().run_script(m_script, "Ispy");
@@ -136,34 +109,33 @@ Ispy::update(float dt_sec)
136109
{
137110
if (m_sprite->animation_done())
138111
{
139-
switch (m_dir)
140-
{
141-
case Direction::DOWN: m_sprite->set_action("showing-down", 1); break;
142-
case Direction::UP: m_sprite->set_action("showing-up", 1); break;
143-
case Direction::LEFT: m_sprite->set_action("showing-left", 1); break;
144-
case Direction::RIGHT: m_sprite->set_action("showing-right", 1); break;
145-
default: break;
146-
}
112+
set_sprite_action("showing", 1);
147113
m_state = ISPYSTATE_SHOWING;
148114
}
149115
}
150116
if (m_state == ISPYSTATE_SHOWING)
151117
{
152118
if (m_sprite->animation_done())
153119
{
154-
switch (m_dir)
155-
{
156-
case Direction::DOWN: m_sprite->set_action("idle-down"); break;
157-
case Direction::UP: m_sprite->set_action("idle-up"); break;
158-
case Direction::LEFT: m_sprite->set_action("idle-left"); break;
159-
case Direction::RIGHT: m_sprite->set_action("idle-right"); break;
160-
default: break;
161-
}
120+
set_sprite_action("idle");
162121
m_state = ISPYSTATE_IDLE;
163122
}
164123
}
165124
}
166125

126+
void
127+
Ispy::set_sprite_action(std::string action, int loops)
128+
{
129+
switch (m_dir)
130+
{
131+
case Direction::DOWN: m_sprite->set_action(action + "-down", loops); break;
132+
case Direction::UP: m_sprite->set_action(action + "-up", loops); break;
133+
case Direction::LEFT: m_sprite->set_action(action + "-left", loops); break;
134+
case Direction::RIGHT: m_sprite->set_action(action + "-right", loops); break;
135+
default: break;
136+
}
137+
}
138+
167139
void
168140
Ispy::on_flip(float height)
169141
{

src/object/ispy.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SuperTux - Ispy
22
// Copyright (C) 2007 Christoph Sommer <[email protected]>
3+
// 2022 Jiri Palecek <[email protected]>
34
//
45
// This program is free software: you can redistribute it and/or modify
56
// it under the terms of the GNU General Public License as published by
@@ -37,6 +38,9 @@ class Ispy final : public MovingSprite
3738

3839
virtual void on_flip(float height) override;
3940

41+
private:
42+
void set_sprite_action(std::string action, int loops = -1);
43+
4044
private:
4145
enum IspyState {
4246
ISPYSTATE_IDLE,

0 commit comments

Comments
 (0)