Skip to content

Commit 9bda8cb

Browse files
committed
Tux will not drop an object when releasing Action button on touchscreen
Holding the Action button on a phone makes it impossible to press the Jump button Also prevents Tux from blasting a stunned Mr. Ice Block with a fireball when trying to pick him up Using keyboard or gamepad will behave the same way as before, releasing Action button will drop the object
1 parent 1ee395e commit 9bda8cb

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed

src/control/controller.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ boost::optional<Control> Control_from_string(const std::string& text)
6666
return boost::none;
6767
}
6868

69-
Controller::Controller()
69+
Controller::Controller():
70+
m_touchscreen(false)
7071
{
7172
reset();
7273
}
@@ -81,6 +82,7 @@ Controller::reset()
8182
m_controls[i] = false;
8283
m_old_controls[i] = false;
8384
}
85+
m_touchscreen = false;
8486
}
8587

8688
void
@@ -89,6 +91,12 @@ Controller::set_control(Control control, bool value)
8991
m_controls[static_cast<int>(control)] = value;
9092
}
9193

94+
void
95+
Controller::set_touchscreen(bool value)
96+
{
97+
m_touchscreen = value;
98+
}
99+
92100
bool
93101
Controller::hold(Control control) const
94102
{
@@ -107,6 +115,12 @@ Controller::released(Control control) const
107115
return m_old_controls[static_cast<int>(control)] && !m_controls[static_cast<int>(control)];
108116
}
109117

118+
bool
119+
Controller::is_touchscreen() const
120+
{
121+
return m_touchscreen;
122+
}
123+
110124
void
111125
Controller::update()
112126
{

src/control/controller.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class Controller
6363

6464
void set_control(Control control, bool value);
6565

66+
/** Touchscreen flag is set by MobileController, cleared when starting the level */
67+
void set_touchscreen(bool value);
68+
6669
/** returns true if the control is pressed down */
6770
bool hold(Control control) const;
6871

@@ -74,13 +77,19 @@ class Controller
7477

7578
void reset();
7679

80+
/** returns true if the controller event has been generated by touchscreen */
81+
bool is_touchscreen() const;
82+
7783
protected:
7884
/** current control status */
7985
bool m_controls[static_cast<int>(Control::CONTROLCOUNT)];
8086

8187
/** control status at last frame */
8288
bool m_old_controls[static_cast<int>(Control::CONTROLCOUNT)];
8389

90+
/** the event has been generated by touchscreen */
91+
bool m_touchscreen;
92+
8493
private:
8594
Controller(const Controller&) = delete;
8695
Controller& operator=(const Controller&) = delete;

src/control/mobile_controller.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ MobileController::apply(Controller& controller) const
212212
controller.set_control(Control::DEBUG_MENU, m_debug);
213213
if (m_escape != m_old_escape)
214214
controller.set_control(Control::ESCAPE, m_escape);
215+
216+
if (m_up || m_down || m_left || m_right || m_jump || m_action || m_cheats || m_debug || m_escape)
217+
{
218+
controller.set_touchscreen(true);
219+
}
215220
}
216221

217222
bool

src/object/player.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ Player::Player(PlayerStatus& player_status, const std::string& name_) :
189189
m_visible(true),
190190
m_grabbed_object(nullptr),
191191
m_grabbed_object_remove_listener(new GrabListener(*this)),
192+
m_released_object(false),
192193
// if/when we have complete penny gfx, we can
193194
// load those instead of Tux's sprite in the
194195
// constructor
@@ -1135,9 +1136,12 @@ Player::handle_input()
11351136
/* Handle vertical movement: */
11361137
if (!m_stone && !m_swimming) handle_vertical_input();
11371138

1139+
/* grabbing */
1140+
bool just_grabbed = try_grab();
1141+
11381142
/* Shoot! */
11391143
auto active_bullets = Sector::get().get_object_count<Bullet>();
1140-
if (m_controller->pressed(Control::ACTION) && (m_player_status.bonus == FIRE_BONUS || m_player_status.bonus == ICE_BONUS)) {
1144+
if (m_controller->pressed(Control::ACTION) && (m_player_status.bonus == FIRE_BONUS || m_player_status.bonus == ICE_BONUS) && !m_grabbed_object) {
11411145
if ((m_player_status.bonus == FIRE_BONUS &&
11421146
active_bullets < m_player_status.max_fire_bullets) ||
11431147
(m_player_status.bonus == ICE_BONUS &&
@@ -1200,10 +1204,9 @@ Player::handle_input()
12001204
do_standup(false);
12011205
}
12021206

1203-
/* grabbing */
1204-
try_grab();
1205-
1206-
if (!m_controller->hold(Control::ACTION) && m_grabbed_object) {
1207+
/* Drop grabbed object when releasing the Action button on keyboard or gamepad, and on the second button press when using touchscreen */
1208+
if ((m_controller->is_touchscreen() ? m_controller->pressed(Control::ACTION) : !m_controller->hold(Control::ACTION)) &&
1209+
m_grabbed_object && !just_grabbed) {
12071210
auto moving_object = dynamic_cast<MovingObject*> (m_grabbed_object);
12081211
if (moving_object) {
12091212
// move the grabbed object a bit away from tux
@@ -1256,12 +1259,17 @@ Player::handle_input()
12561259
}
12571260
moving_object->del_remove_listener(m_grabbed_object_remove_listener.get());
12581261
m_grabbed_object = nullptr;
1262+
m_released_object = true;
12591263
}
12601264
} else {
12611265
log_debug << "Non MovingObject grabbed?!?" << std::endl;
12621266
}
12631267
}
12641268

1269+
if (!m_controller->hold(Control::ACTION) && m_released_object) {
1270+
m_released_object = false;
1271+
}
1272+
12651273
/* stop backflipping at will */
12661274
if ( m_backflipping && ( !m_controller->hold(Control::JUMP) && !m_backflip_timer.started()) ){
12671275
stop_backflipping();
@@ -1293,10 +1301,10 @@ Player::position_grabbed_object()
12931301
}
12941302
}
12951303

1296-
void
1304+
bool
12971305
Player::try_grab()
12981306
{
1299-
if (m_controller->hold(Control::ACTION) && !m_grabbed_object && !m_duck)
1307+
if (m_controller->hold(Control::ACTION) && !m_grabbed_object && !m_duck && !m_released_object)
13001308
{
13011309

13021310
Vector pos(0.0f, 0.0f);
@@ -1335,11 +1343,12 @@ Player::try_grab()
13351343
moving_object.add_remove_listener(m_grabbed_object_remove_listener.get());
13361344

13371345
position_grabbed_object();
1338-
break;
1346+
return true;
13391347
}
13401348
}
13411349
}
13421350
}
1351+
return false;
13431352
}
13441353

13451354
void

src/object/player.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class Player final : public MovingObject,
216216
void stop_backflipping();
217217

218218
void position_grabbed_object();
219-
void try_grab();
219+
bool try_grab();
220220

221221
/** Boosts Tux in a certain direction, sideways. Useful for bumpers/walljumping. */
222222
void sideways_push(float delta);
@@ -326,6 +326,7 @@ class Player final : public MovingObject,
326326

327327
Portable* m_grabbed_object;
328328
std::unique_ptr<ObjectRemoveListener> m_grabbed_object_remove_listener;
329+
bool m_released_object;
329330

330331
SpritePtr m_sprite; /**< The main sprite representing Tux */
331332

0 commit comments

Comments
 (0)