Skip to content

Commit 6ac4d7e

Browse files
Tweak Ghoul's speeds (#3333)
* Tweak `Ghoul`'s speed + code style * Slow by 15% * Slow down ghoul by a lot --------- Co-authored-by: Daniel Ward <[email protected]>
1 parent 9040d3c commit 6ac4d7e

File tree

1 file changed

+105
-52
lines changed

1 file changed

+105
-52
lines changed

src/badguy/ghoul.cpp

Lines changed: 105 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@
2020
#include "supertux/sector.hpp"
2121
#include "util/reader_mapping.hpp"
2222

23-
static const float DEFAULT_TRACK_RANGE = 2500.0f;
24-
static const float RESPAWN_TIME = 4.0f;
25-
static const float DOWN_VELOCITY = 32.0f;
26-
static const float UP_VELOCITY = -256.0f;
27-
static const float UP_ACCELERATION = 256.0f;
28-
static const float HORZ_SPEED = 40.0f;
29-
static const float HORZ_ACCELERATION = 256.0f;
30-
static const float VERT_OFFSET = 48.0f;
23+
namespace
24+
{
25+
static constexpr float DEFAULT_TRACK_RANGE = 2500.f;
26+
static constexpr float RESPAWN_TIME = 4.f;
27+
28+
static constexpr float DOWN_VELOCITY = 40.f;
29+
static constexpr float UP_VELOCITY = -150.f;
30+
static constexpr float UP_ACCELERATION = 150.f;
31+
static constexpr float HORZ_SPEED = 60.f;
32+
static constexpr float HORZ_ACCELERATION = 190.f;
33+
static constexpr float HORZ_TRUST_MULTIPLIER = 3.f;
34+
35+
static constexpr float VERT_OFFSET = 48.f;
36+
}
3137

3238
Ghoul::Ghoul(const ReaderMapping& reader) :
3339
BadGuy(reader, "images/creatures/ghoul/ghoul.sprite"),
@@ -56,10 +62,11 @@ Ghoul::to_target()
5662
if (!player)
5763
return Vector(0.0f, 0.0f);
5864

59-
Vector p1 = get_bbox().get_middle();
65+
const Vector p1 = get_bbox().get_middle();
6066
Vector p2 = player->get_bbox().get_middle();
61-
p2.y -= 32; //a little offset, so he doesn't hit Tux from below
62-
Vector dist = p2 - p1;
67+
p2.y -= 32.f; //a little offset, so he doesn't hit Tux from below
68+
69+
const Vector dist = p2 - p1;
6370
return dist;
6471
}
6572

@@ -68,112 +75,142 @@ Ghoul::active_update(float dt_sec)
6875
{
6976
BadGuy::active_update(dt_sec);
7077
auto player = get_nearest_player();
71-
if (!player) {
78+
if (!player)
79+
{
7280
m_physic.set_acceleration(0.0f, 0.0f);
7381
return;
7482
}
7583

76-
Vector dist = to_target();
77-
Direction new_dir = dist.x < 0 ? Direction::LEFT : Direction::RIGHT;
78-
bool dir_changed = new_dir != m_dir;
79-
bool chase = glm::length(dist) < m_track_range;
84+
const Vector dist = to_target();
85+
const Direction new_dir = dist.x < 0 ? Direction::LEFT : Direction::RIGHT;
86+
const bool dir_changed = new_dir != m_dir;
87+
const bool should_chase = glm::length(dist) < m_track_range;
8088

8189
switch (m_state)
8290
{
8391
case ROAMING_DOWN:
8492
roaming_decel_check();
85-
if (dir_changed) {
93+
if (dir_changed)
94+
{
8695
set_action(new_dir == Direction::LEFT ? "left" : "right");
8796
m_dir = new_dir;
8897
}
89-
if (chase) {
98+
if (should_chase)
99+
{
90100
set_state(CHASING_DOWN);
91-
} else if (get_pos().y > m_home_pos.y + VERT_OFFSET) {
101+
}
102+
else if (get_pos().y > m_home_pos.y + VERT_OFFSET)
103+
{
92104
set_state(ROAMING_ACCEL1);
93105
}
94106
break;
95107
case CHASING_DOWN:
96108
update_speed(dist);
97-
if (dir_changed) {
109+
if (dir_changed)
110+
{
98111
set_action(new_dir == Direction::LEFT ? "left" : "right");
99112
m_dir = new_dir;
100113
}
101-
if (!chase) {
114+
if (!should_chase)
115+
{
102116
m_home_pos = get_pos();
103117
set_state(ROAMING_DOWN);
104-
} else if (dist.y < -VERT_OFFSET) {
118+
}
119+
else if (dist.y < -VERT_OFFSET)
120+
{
105121
set_state(CHASING_ACCEL1);
106122
}
107123
break;
108124
case ROAMING_ACCEL1:
109125
roaming_decel_check();
110-
if (m_sprite->animation_done()) {
126+
if (m_sprite->animation_done())
127+
{
111128
set_state(ROAMING_ACCEL2);
112129
}
113130
break;
114131
case CHASING_ACCEL1:
115132
update_speed(dist);
116-
if (m_sprite->animation_done()) {
133+
if (m_sprite->animation_done())
134+
{
117135
set_state(CHASING_ACCEL2);
118136
}
119137
break;
120138
case ROAMING_ACCEL2:
121139
roaming_decel_check();
122-
if (m_sprite->animation_done()) {
140+
if (m_sprite->animation_done())
141+
{
123142
set_state(ROAMING_UP);
124143
}
125144
break;
126145
case CHASING_ACCEL2:
127146
update_speed(dist);
128-
if (m_sprite->animation_done()) {
147+
if (m_sprite->animation_done())
148+
{
129149
set_state(CHASING_UP);
130150
}
131151
break;
132152
case ROAMING_UP:
133153
roaming_decel_check();
134-
if (dir_changed) {
154+
if (dir_changed)
155+
{
135156
set_action(new_dir == Direction::LEFT ? "left-up" : "right-up");
136157
m_dir = new_dir;
137158
}
138-
if (chase) {
159+
if (should_chase)
160+
{
139161
set_state(CHASING_UP);
140-
} else if (m_physic.get_velocity_y() > DOWN_VELOCITY) {
141-
if (get_pos().y > m_home_pos.y + VERT_OFFSET) {
162+
}
163+
else if (m_physic.get_velocity_y() > DOWN_VELOCITY)
164+
{
165+
if (get_pos().y > m_home_pos.y + VERT_OFFSET)
166+
{
142167
set_state(ROAMING_ACCEL1);
143-
} else {
168+
}
169+
else
170+
{
144171
set_state(ROAMING_DOWN);
145172
}
146173
}
147174
break;
148175
case CHASING_UP:
149176
update_speed(dist);
150-
if (dir_changed) {
177+
if (dir_changed)
178+
{
151179
set_action(new_dir == Direction::LEFT ? "left-up" : "right-up");
152180
m_dir = new_dir;
153181
}
154-
if (!chase) {
182+
if (!should_chase)
183+
{
155184
m_home_pos = get_pos();
156185
set_state(ROAMING_UP);
157-
} else if (m_physic.get_velocity_y() > DOWN_VELOCITY) {
158-
if (dist.y < -VERT_OFFSET) {
186+
}
187+
else if (m_physic.get_velocity_y() > DOWN_VELOCITY)
188+
{
189+
if (dist.y < -VERT_OFFSET)
190+
{
159191
set_state(CHASING_ACCEL1);
160-
} else {
192+
}
193+
else
194+
{
161195
set_state(CHASING_DOWN);
162196
}
163197
}
164198
break;
165199
case STUNNED:
166-
if (m_sprite->animation_done()) {
200+
if (m_sprite->animation_done())
201+
{
167202
set_state(INVISIBLE);
168203
}
169204
break;
170205
case INVISIBLE:
171-
if (m_respawn_timer.check()) {
206+
if (m_respawn_timer.check())
207+
{
172208
set_state(RECOVERING);
173209
}
174210
break;
175211
case RECOVERING:
176-
if (m_sprite->animation_done()) {
212+
if (m_sprite->animation_done())
213+
{
177214
set_state(ROAMING_DOWN);
178215
}
179216
break;
@@ -186,13 +223,17 @@ void
186223
Ghoul::update_speed(const Vector& dist)
187224
{
188225
const float vx = m_physic.get_velocity_x();
189-
if (vx >= -HORZ_SPEED && vx <= HORZ_SPEED) {
190-
m_physic.set_acceleration_x(0.0f);
191-
const float vy = dist.y < 0.0f ? (UP_VELOCITY + DOWN_VELOCITY) / 2.0f : DOWN_VELOCITY;
226+
if (vx >= -HORZ_SPEED && vx <= HORZ_SPEED)
227+
{
228+
m_physic.set_acceleration_x(0.0f);
229+
const float vy = dist.y < 0.0f ? (UP_VELOCITY + DOWN_VELOCITY) / 2.0f : DOWN_VELOCITY;
192230
const float t = dist.y / vy;
193-
if (t * HORZ_SPEED > std::abs(dist.x)) {
231+
if (t * HORZ_SPEED > std::abs(dist.x))
232+
{
194233
m_physic.set_velocity_x(dist.x / t);
195-
} else {
234+
}
235+
else
236+
{
196237
m_physic.set_velocity_x(dist.x < 0.0f ? -HORZ_SPEED : HORZ_SPEED);
197238
}
198239
}
@@ -201,9 +242,8 @@ Ghoul::update_speed(const Vector& dist)
201242
void
202243
Ghoul::draw(DrawingContext& context)
203244
{
204-
if (m_state != INVISIBLE) {
245+
if (m_state != INVISIBLE)
205246
BadGuy::draw(context);
206-
}
207247
}
208248

209249
void
@@ -217,11 +257,16 @@ Ghoul::horizontal_thrust()
217257

218258
const float a = dist.x > 0.0f ? -HORZ_ACCELERATION : HORZ_ACCELERATION;
219259
m_physic.set_acceleration_x(a);
260+
220261
const float vx = m_physic.get_velocity_x();
221-
const float vx_diff = HORZ_SPEED * 9.0f;
222-
if (t == 0.0f) {
262+
const float vx_diff = HORZ_SPEED * HORZ_TRUST_MULTIPLIER;
263+
264+
if (t == 0.0f)
265+
{
223266
m_physic.set_velocity_x(dist.x > 0.0f ? vx - vx_diff : vx + vx_diff);
224-
} else {
267+
}
268+
else
269+
{
225270
const float vx_needed = dist.x / t - a * t / 2.0f;
226271
m_physic.set_velocity_x(std::clamp(vx_needed, vx - vx_diff, vx + vx_diff));
227272
}
@@ -231,9 +276,12 @@ void
231276
Ghoul::start_roaming_decel()
232277
{
233278
const float vx = m_physic.get_velocity_x();
234-
if (vx > 0) {
279+
if (vx > 0)
280+
{
235281
m_physic.set_acceleration_x(-HORZ_ACCELERATION);
236-
} else if (vx < 0) {
282+
}
283+
else if (vx < 0)
284+
{
237285
m_physic.set_acceleration_x(HORZ_ACCELERATION);
238286
}
239287
}
@@ -243,7 +291,8 @@ Ghoul::roaming_decel_check()
243291
{
244292
const float vx = m_physic.get_velocity_x();
245293
const float ax = m_physic.get_acceleration_x();
246-
if (vx * ax > 0) {
294+
if (vx * ax > 0)
295+
{
247296
m_physic.set_velocity_x(0.0f);
248297
m_physic.set_acceleration_x(0.0f);
249298
}
@@ -252,6 +301,9 @@ Ghoul::roaming_decel_check()
252301
void
253302
Ghoul::set_state(GhoulState new_state)
254303
{
304+
if (m_state == new_state)
305+
return;
306+
255307
switch (new_state)
256308
{
257309
case ROAMING_DOWN:
@@ -339,6 +391,7 @@ Ghoul::collision_squished(MovingObject& object)
339391
auto player = Sector::get().get_nearest_player(m_col.m_bbox);
340392
if (player)
341393
player->bounce(*this);
394+
342395
kill_fall();
343396
return true;
344397
}

0 commit comments

Comments
 (0)