-
Notifications
You must be signed in to change notification settings - Fork 0
/
Actor.cpp
437 lines (392 loc) · 11.8 KB
/
Actor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*******************************************************************************
CommanderTux
Penguin In Space
Released under the GNU Public License
2005 by André Schnabel ([email protected])
*******************************************************************************/
// Actor.cpp
// For both player and enemies
// Based upon the CSprite class
#include "Actor.h"
//
// CActor
// Constructor
//
CActor::CActor( int s_x, int s_y, SLevel *level,
SDL_Texture *images[NUM_ACTOR_IMAGES], int fall_speed )
: CSprite( NULL )
{
int i;
// Init vars
m_level = level;
m_s_x = s_x;
m_s_y = s_y;
m_fall_speed = fall_speed;
// Load all images
for( i=0; i<NUM_ACTOR_IMAGES; i++ )
m_images[i] = images[i];
// Setup the rectangle
m_rect.w = TILE_W;
m_rect.h = TILE_H;
SetPos( m_s_x, m_s_y );
// Normally the actor stands
SetImage( m_images[STANDING_RIGHT] );
// Some var initializing
m_flip = true;
m_fall.active = m_jump.active = false;
m_dir = DIR_RIGHT;
m_standing = false;
m_reset_image = false;
m_jump_increase = 0;
m_dying = false;
}
//
// GetDying
//
bool CActor::GetDying()
{
return m_dying;
}
//
// Die
//
void CActor::Die()
{
if( !m_dying ) // Prevent from dying more than once
{
m_dying = true;
// Set suitable dying image
if( m_dir == DIR_RIGHT )
SetImage( m_images[DEAD_RIGHT] );
else
SetImage( m_images[DEAD_LEFT] );
}
}
//
// Respawn
//
void CActor::Respawn()
{
// Only respawn when dying
if( m_dying )
{
SetImage(m_images[STANDING_RIGHT] );
m_dying = false;
// Get back to old start position
m_rect.y = m_s_y;
m_rect.x = m_s_x;
}
}
//
// Walk
//
void CActor::Walk( Direction dir, int walk_speed )
{
int i;
// Dead actors can't walk
if( !m_dying )
{
// We're not standing anymore
m_standing = false;
// Update image to current direction if neccessary
if( !m_jump.active && !m_fall.active )
{
// Walk left
if( dir == DIR_LEFT )
{
// Flip the image after a specific time
if( g_framework->GetTicks() - m_last_flip > FLIP_TIME )
{
// Flip from false to true
// Standing left image to walking left image
if( !m_flip )
{
SetImage( m_images[WALKING_LEFT] );
m_flip = true;
}
// Flip from true to false
// Walking left image to standing left image
else
{
SetImage( m_images[STANDING_LEFT] );
m_flip = false;
}
// Refresh the time
m_last_flip = g_framework->GetTicks();
}
}
// Walk right
else
{
// Flip the image after a specific time
if( g_framework->GetTicks() - m_last_flip > FLIP_TIME )
{
// Flip from false to true
// Standing right image to walking right image
if( !m_flip )
{
SetImage( m_images[WALKING_RIGHT] );
m_flip = true;
}
// Flip from true to false
// Walking right image to standing right image
else
{
SetImage( m_images[STANDING_RIGHT] );
m_flip = false;
}
// Refresh the time
m_last_flip = g_framework->GetTicks();
}
}
}
else
{
// Direction changed?
if( m_dir != dir )
{
// Update jump image
if( dir == DIR_LEFT )
SetImage( m_images[JUMPING_LEFT] );
else
SetImage( m_images[JUMPING_RIGHT] );
}
}
// Walk if possible
for( i=0; i<walk_speed; i++ )
{
// Approximate
if( dir == DIR_RIGHT )
{
if( WalkPossible( dir ) )
m_rect.x++;
else
break;
}
else
{
if( WalkPossible( dir ) )
m_rect.x--;
else
break;
}
}
// Copy to member var
m_dir = dir;
} // !m_dying
}
//
// WalkPossible
// Check whether walking is possible
//
// At all 2 Tiles can be beside the player
// The player can walk when both of them are empty
//
bool CActor::WalkPossible( Direction dir )
{
// Can the actor walk right?
if( dir == DIR_RIGHT )
{
if( m_level->content[m_rect.y/TILE_H]
[(m_rect.x+m_rect.w+1)/TILE_W]
== TILE_EMPTY &&
m_level->content[(m_rect.y+m_rect.h-1)/TILE_H]
[(m_rect.x+m_rect.w+1)/TILE_W]
== TILE_EMPTY &&
m_rect.x + m_rect.w < LVL_W_MAX * TILE_W )
return true; // Walking right possible
}
else // Or left?
{
if( m_level->content[m_rect.y/TILE_H]
[(m_rect.x-1)/TILE_W]
== TILE_EMPTY &&
m_level->content[(m_rect.y+m_rect.h-1)/TILE_H]
[(m_rect.x-1)/TILE_W]
== TILE_EMPTY &&
m_rect.x > 0 )
return true; // Walking left possible
}
// Walking not possible
return false;
}
//
// Jump
//
void CActor::Jump()
{
// Only jump when not falling or jumping yet
if( !m_jump.active && !m_fall.active && !m_dying )
{
// Set all values for a new jump
// Collisions stuff is all handled in the Update() func!
// Since we are not in UT 2004 actors can't start jumping in the air...
{
m_jump.active = true;
// Refresh time
m_jump.s_time = g_framework->GetTicks();
// Set correct image
if( m_dir == DIR_RIGHT )
SetImage( m_images[JUMPING_RIGHT] );
else
SetImage( m_images[JUMPING_LEFT] );
}
}
}
//
// StopJump
//
void CActor::StopJump( bool bump )
{
// Can't stop when not actually jumping
if( m_jump.active )
{
// Reset vars
m_jump.active = false;
m_reset_image = false;
m_jump_increase = 0;
}
}
//
// IncreaseJump
// So holding the jump key longer increases the jump
//
void CActor::IncreaseJump( bool ignore_max )
{
// Only increase up to a specific value except ignore max is true
if( m_jump.active )
{
if( m_jump_increase < JUMP_INCREASE_MAX ||
ignore_max )
m_jump_increase+=JUMP_INCREASE_POWER;
}
}
//
// Update
//
void CActor::Update( int *scroll_x, int *scroll_y, bool enemy )
{
unsigned int i;
// Don't update dying actors in most cases
if( !m_dying )
{
// Jumping
// As when walking you need to consider that at most two tiles
// are near the actor
if( m_jump.active )
{
// Not standing anymore
m_standing = false;
// Prevent from negative values and wrong numbers!
if( ( (g_framework->GetTicks()-m_jump.s_time) / 20 ) < JUMP_POWER + m_jump_increase )
{
// Don't jump inside of the tiles above
// Approximation
for( i=0;
i<(JUMP_POWER+m_jump_increase)-((g_framework->GetTicks()-m_jump.s_time)/20);
i++ )
{
// Jump possible
if( m_level->content[(m_rect.y-1)/TILE_H]
[(m_rect.x+1)/TILE_W]
== TILE_EMPTY &&
m_level->content[(m_rect.y-1)/TILE_H]
[(m_rect.x+m_rect.w-1)/TILE_W]
== TILE_EMPTY )
{
m_rect.y--;
}
// Bump against the roof
else
{
StopJump( true );
break;
}
}
}
// Jump power is exceeded, the end is reached
else
StopJump( false );
}
// Not jumping anymore... reset image!
if( m_standing )
{
m_reset_image = true;
// Once is enough!
m_standing = false;
}
// Look whether the actor needs to fall
if( !m_jump.active && !m_fall.active )
{
// Is the tile under us empty?
if( m_level->content[(m_rect.y+m_rect.h+OVER)/TILE_H]
[(m_rect.x+1)/TILE_W]
== TILE_EMPTY &&
m_level->content[(m_rect.y+m_rect.h+OVER)/TILE_H]
[(m_rect.x+m_rect.w-1)/TILE_W]
== TILE_EMPTY )
{
m_fall.active = true;
m_fall.s_time = g_framework->GetTicks();
// Set to the correct image
if( m_dir == DIR_LEFT )
SetImage( m_images[JUMPING_LEFT] );
else
SetImage( m_images[JUMPING_RIGHT] );
}
}
// Reset image after falling or something
if( m_reset_image )
{
// Only if not falling
if( !m_fall.active )
{
// Make tux only look jumping when he really is!
if( m_dir == DIR_LEFT )
SetImage( m_images[STANDING_LEFT] );
else
SetImage( m_images[STANDING_RIGHT] );
}
m_reset_image = false;
}
} // !m_dying
// Falling
if( m_fall.active && !m_jump.active )
{
// Enemies still fall when dead
if( !m_dying || enemy )
{
// Approximation
for( i=0;
i<m_fall_speed+( (g_framework->GetTicks() - m_fall.s_time) / 100 );
i++ )
{
// Is the tile under us empty?
if( m_level->content[(m_rect.y+m_rect.h+OVER)/TILE_H]
[(m_rect.x+1)/TILE_W]
== TILE_EMPTY &&
m_level->content[(m_rect.y+m_rect.h+OVER)/TILE_H]
[(m_rect.x+m_rect.w-1)/TILE_W]
== TILE_EMPTY &&
m_rect.y + m_rect.h < LVL_H_MAX * TILE_H )
{
m_rect.y++;
}
// Actor is falling out of the level
else if( m_rect.y + m_rect.h >= LVL_H_MAX * TILE_H )
{
// Dying...
Die();
}
// Actor bumps into the ground
else
{
// Stopping the fall
m_fall.active = false;
m_reset_image = true;
break;
}
}
}
}
}