20
20
#include " supertux/sector.hpp"
21
21
#include " util/reader_mapping.hpp"
22
22
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
+ }
31
37
32
38
Ghoul::Ghoul (const ReaderMapping& reader) :
33
39
BadGuy(reader, " images/creatures/ghoul/ghoul.sprite" ),
@@ -56,10 +62,11 @@ Ghoul::to_target()
56
62
if (!player)
57
63
return Vector (0 .0f , 0 .0f );
58
64
59
- Vector p1 = get_bbox ().get_middle ();
65
+ const Vector p1 = get_bbox ().get_middle ();
60
66
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;
63
70
return dist;
64
71
}
65
72
@@ -68,112 +75,142 @@ Ghoul::active_update(float dt_sec)
68
75
{
69
76
BadGuy::active_update (dt_sec);
70
77
auto player = get_nearest_player ();
71
- if (!player) {
78
+ if (!player)
79
+ {
72
80
m_physic.set_acceleration (0 .0f , 0 .0f );
73
81
return ;
74
82
}
75
83
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;
80
88
81
89
switch (m_state)
82
90
{
83
91
case ROAMING_DOWN:
84
92
roaming_decel_check ();
85
- if (dir_changed) {
93
+ if (dir_changed)
94
+ {
86
95
set_action (new_dir == Direction::LEFT ? " left" : " right" );
87
96
m_dir = new_dir;
88
97
}
89
- if (chase) {
98
+ if (should_chase)
99
+ {
90
100
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
+ {
92
104
set_state (ROAMING_ACCEL1);
93
105
}
94
106
break ;
95
107
case CHASING_DOWN:
96
108
update_speed (dist);
97
- if (dir_changed) {
109
+ if (dir_changed)
110
+ {
98
111
set_action (new_dir == Direction::LEFT ? " left" : " right" );
99
112
m_dir = new_dir;
100
113
}
101
- if (!chase) {
114
+ if (!should_chase)
115
+ {
102
116
m_home_pos = get_pos ();
103
117
set_state (ROAMING_DOWN);
104
- } else if (dist.y < -VERT_OFFSET) {
118
+ }
119
+ else if (dist.y < -VERT_OFFSET)
120
+ {
105
121
set_state (CHASING_ACCEL1);
106
122
}
107
123
break ;
108
124
case ROAMING_ACCEL1:
109
125
roaming_decel_check ();
110
- if (m_sprite->animation_done ()) {
126
+ if (m_sprite->animation_done ())
127
+ {
111
128
set_state (ROAMING_ACCEL2);
112
129
}
113
130
break ;
114
131
case CHASING_ACCEL1:
115
132
update_speed (dist);
116
- if (m_sprite->animation_done ()) {
133
+ if (m_sprite->animation_done ())
134
+ {
117
135
set_state (CHASING_ACCEL2);
118
136
}
119
137
break ;
120
138
case ROAMING_ACCEL2:
121
139
roaming_decel_check ();
122
- if (m_sprite->animation_done ()) {
140
+ if (m_sprite->animation_done ())
141
+ {
123
142
set_state (ROAMING_UP);
124
143
}
125
144
break ;
126
145
case CHASING_ACCEL2:
127
146
update_speed (dist);
128
- if (m_sprite->animation_done ()) {
147
+ if (m_sprite->animation_done ())
148
+ {
129
149
set_state (CHASING_UP);
130
150
}
131
151
break ;
132
152
case ROAMING_UP:
133
153
roaming_decel_check ();
134
- if (dir_changed) {
154
+ if (dir_changed)
155
+ {
135
156
set_action (new_dir == Direction::LEFT ? " left-up" : " right-up" );
136
157
m_dir = new_dir;
137
158
}
138
- if (chase) {
159
+ if (should_chase)
160
+ {
139
161
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
+ {
142
167
set_state (ROAMING_ACCEL1);
143
- } else {
168
+ }
169
+ else
170
+ {
144
171
set_state (ROAMING_DOWN);
145
172
}
146
173
}
147
174
break ;
148
175
case CHASING_UP:
149
176
update_speed (dist);
150
- if (dir_changed) {
177
+ if (dir_changed)
178
+ {
151
179
set_action (new_dir == Direction::LEFT ? " left-up" : " right-up" );
152
180
m_dir = new_dir;
153
181
}
154
- if (!chase) {
182
+ if (!should_chase)
183
+ {
155
184
m_home_pos = get_pos ();
156
185
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
+ {
159
191
set_state (CHASING_ACCEL1);
160
- } else {
192
+ }
193
+ else
194
+ {
161
195
set_state (CHASING_DOWN);
162
196
}
163
197
}
164
198
break ;
165
199
case STUNNED:
166
- if (m_sprite->animation_done ()) {
200
+ if (m_sprite->animation_done ())
201
+ {
167
202
set_state (INVISIBLE);
168
203
}
169
204
break ;
170
205
case INVISIBLE:
171
- if (m_respawn_timer.check ()) {
206
+ if (m_respawn_timer.check ())
207
+ {
172
208
set_state (RECOVERING);
173
209
}
174
210
break ;
175
211
case RECOVERING:
176
- if (m_sprite->animation_done ()) {
212
+ if (m_sprite->animation_done ())
213
+ {
177
214
set_state (ROAMING_DOWN);
178
215
}
179
216
break ;
@@ -186,13 +223,17 @@ void
186
223
Ghoul::update_speed (const Vector& dist)
187
224
{
188
225
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;
192
230
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
+ {
194
233
m_physic.set_velocity_x (dist.x / t);
195
- } else {
234
+ }
235
+ else
236
+ {
196
237
m_physic.set_velocity_x (dist.x < 0 .0f ? -HORZ_SPEED : HORZ_SPEED);
197
238
}
198
239
}
@@ -201,9 +242,8 @@ Ghoul::update_speed(const Vector& dist)
201
242
void
202
243
Ghoul::draw (DrawingContext& context)
203
244
{
204
- if (m_state != INVISIBLE) {
245
+ if (m_state != INVISIBLE)
205
246
BadGuy::draw (context);
206
- }
207
247
}
208
248
209
249
void
@@ -217,11 +257,16 @@ Ghoul::horizontal_thrust()
217
257
218
258
const float a = dist.x > 0 .0f ? -HORZ_ACCELERATION : HORZ_ACCELERATION;
219
259
m_physic.set_acceleration_x (a);
260
+
220
261
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
+ {
223
266
m_physic.set_velocity_x (dist.x > 0 .0f ? vx - vx_diff : vx + vx_diff);
224
- } else {
267
+ }
268
+ else
269
+ {
225
270
const float vx_needed = dist.x / t - a * t / 2 .0f ;
226
271
m_physic.set_velocity_x (std::clamp (vx_needed, vx - vx_diff, vx + vx_diff));
227
272
}
@@ -231,9 +276,12 @@ void
231
276
Ghoul::start_roaming_decel ()
232
277
{
233
278
const float vx = m_physic.get_velocity_x ();
234
- if (vx > 0 ) {
279
+ if (vx > 0 )
280
+ {
235
281
m_physic.set_acceleration_x (-HORZ_ACCELERATION);
236
- } else if (vx < 0 ) {
282
+ }
283
+ else if (vx < 0 )
284
+ {
237
285
m_physic.set_acceleration_x (HORZ_ACCELERATION);
238
286
}
239
287
}
@@ -243,7 +291,8 @@ Ghoul::roaming_decel_check()
243
291
{
244
292
const float vx = m_physic.get_velocity_x ();
245
293
const float ax = m_physic.get_acceleration_x ();
246
- if (vx * ax > 0 ) {
294
+ if (vx * ax > 0 )
295
+ {
247
296
m_physic.set_velocity_x (0 .0f );
248
297
m_physic.set_acceleration_x (0 .0f );
249
298
}
@@ -252,6 +301,9 @@ Ghoul::roaming_decel_check()
252
301
void
253
302
Ghoul::set_state (GhoulState new_state)
254
303
{
304
+ if (m_state == new_state)
305
+ return ;
306
+
255
307
switch (new_state)
256
308
{
257
309
case ROAMING_DOWN:
@@ -339,6 +391,7 @@ Ghoul::collision_squished(MovingObject& object)
339
391
auto player = Sector::get ().get_nearest_player (m_col.m_bbox );
340
392
if (player)
341
393
player->bounce (*this );
394
+
342
395
kill_fall ();
343
396
return true ;
344
397
}
0 commit comments