-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameState.cpp
More file actions
912 lines (785 loc) · 25.9 KB
/
Copy pathGameState.cpp
File metadata and controls
912 lines (785 loc) · 25.9 KB
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
/*
* GameState.cpp - part of 32Blox (revised edition!)
*
* Copyright (C) 2020 Pete Favelle <32blit@ahnlak.com>
*
* This file is released under the MIT License; see LICENSE for details
*
* This state contains the core gameplay logic; by definition, it is by far
* the largest and most complex state.
*/
/* System headers. */
#include <iterator>
/* Local headers. */
#include "32blit.hpp"
#include "32blox.hpp"
#include "assets_images.hpp"
#include "GameState.hpp"
#include "HighScore.hpp"
#include "Level.hpp"
#include "PowerUp.hpp"
/* Functions. */
/*
* constructor - create the contents of this State container.
*/
GameState::GameState( void )
{
/* The bat height will always be the same, but is bound by the screen size */
/* so can't quite be hard coded. */
bat_height = blit::screen.bounds.h - 10;
/* The font pen will be simpler. */
font_pen = blit::Pen( 255, 255, 0 );
number_pen = blit::Pen( 255, 255, 0 );
font_tween.init( blit::tween_sine, 255.0f, 100.0f, 500 );
/* And we'll need access to the high score table. */
high_score = new HighScore();
/* Prepare the tween for splashing messages. */
splash_tween.init( blit::tween_linear, 255.0f, 0.0f, 1750, 1 );
/* All done. */
return;
}
/*
* init - called whenever the game engine is switching to this state
*
* GameStateInterface * - the state we were most recently in
*/
void GameState::init( GameStateInterface *p_previous )
{
/* Select the game spritesheet into the screen. */
blit::screen.sprites = assets.spritesheet_game;
/* Fetch the current top score. */
const hiscore_t *l_top_entry = high_score->get_entry( 0 );
if ( l_top_entry == nullptr )
{
hiscore = 0;
}
else
{
hiscore = l_top_entry->score;
}
/* Load the first level. */
load_level( 1 );
/* Set the tweens running. */
font_tween.start();
/* Reset the lives count and score. */
lives = 3;
score = 0;
/* All done. */
return;
}
/*
* fini - called whenever the game engine turns off this state.
*
* GameStateInterface * - the state we were switching to
*/
void GameState::fini( GameStateInterface *p_next )
{
/* Turn oiff the tweens. */
font_tween.stop();
splash_tween.stop();
/* All done. */
return;
}
/*
* move_bat - updates the bat position, taking into account the bat size and
* the edges of the screen.
* float - the movement amount wanted; this may get clipped if the edge is hit
*/
void GameState::move_bat( float p_movement )
{
/* First up, let's apply the full movement. */
float l_last_pos = bat_position;
bat_position += p_movement;
/* And then clamp it, left and right, honouring any level margins. */
if ( bat_position < ( ( bat_width[bat_type] / 2 ) + level->get_margin() ) )
{
bat_position = ( bat_width[bat_type] / 2 ) + level->get_margin();
}
if ( bat_position > blit::screen.bounds.w - ( bat_width[bat_type] / 2 ) - level->get_margin() )
{
bat_position = blit::screen.bounds.w - ( bat_width[bat_type] / 2 ) - level->get_margin();
}
/* Now, ask our balls to respond to the current bat. */
for ( auto l_ball : balls )
{
/* Then we can just ask the ball to move itself, if it wants to. */
l_ball->move_bat( bat_bounds(), bat_position - l_last_pos, bat_type == BAT_STICKY );
}
/* All done! */
return;
}
/*
* brick_to_screen - returns a Rect of the position of the designated brick,
* to make rendering and collision detection nice and consistent.
* uint8_t * 2 - the row and column in the brick being queried.
*/
blit::Rect GameState::brick_to_screen( uint8_t p_row, uint8_t p_column )
{
return blit::Rect( ( p_column * 32 ) + level->get_margin(), p_row * 16 + 10, 32, 16 );
}
/*
* screen_to_brick - returns the x/y co-ordinates of the brick at the given
* screen location.
* blit::Point - the screen location being queried.
*/
blit::Point GameState::screen_to_brick( blit::Point p_location )
{
/* Clamp the location to the screen, in case a bounding box has slipped */
/* off somewhere, which can get ... messy. */
blit::Point l_location = blit::screen.clip.clamp( p_location );
return blit::Point( ( l_location.x - level->get_margin() ) / 32, ( l_location.y - 10 ) / 16 );
}
/*
* bat_bounds - returns a Rect defining the countaining bounds of the current
* bat. This take into account the bat type, so dynimcally changes
* if, for example, a powerup changes the bat.
*/
blit::Rect GameState::bat_bounds( void )
{
return blit::Rect(
bat_position - ( bat_width[bat_type] / 2 ),
bat_height,
bat_width[bat_type],
8
);
}
/*
* spawn_ball - creates a new ball; on the bat - either at the start of the
* level, or after a ball is lost - when the flag is set, or at
* the location of another ball in 'mid flight', when false.
*/
void GameState::spawn_ball( bool pBat )
{
Ball *l_ball;
blit::Point l_ballpos;
/* Work out the right place for the ball to be. */
if ( pBat )
{
/* The ball starts in the middle of the bat. */
l_ballpos = blit::Point( bat_position, bat_height - 3 );
/* But then we offset it a little one side or the other... */
switch( blit::random() % 4 )
{
case 0:
l_ballpos.x -= 4;
break;
case 1:
l_ballpos.x -= 2;
break;
case 2:
l_ballpos.x += 2;
break;
case 3:
l_ballpos.x += 4;
break;
}
/* create a new ball. */
l_ball = new Ball( l_ballpos, level->get_ball_speed() );
/* Stick it to the bat. */
l_ball->stuck = true;
}
else
{
/* Grab the location of the first ball in the queue. */
auto l_current_ball = balls.front();
l_ballpos = l_current_ball->get_bounds().center();
/* create a new ball. */
l_ball = new Ball( l_ballpos, level->get_ball_speed() );
l_ball->randomise();
}
/* And add it to the internal ball list. */
l_ball->move_bat( bat_bounds(), 0.0f, false );
balls.push_front( l_ball );
/* All done. */
return;
}
/*
* load_level - loads the required level data, resets the balls, the bats and
* everything else for the start of a whole new level
*/
void GameState::load_level( uint8_t p_level )
{
/* Load up the level data. */
level = new Level( p_level, assets.get_platform() );
/* Centre the bat, and set it to a default type. */
bat_position = blit::screen.bounds.w / 2;
bat_speed = 1.0f;
bat_type = BAT_NORMAL;
/* Clear out the list of balls, and spawn one on the bat. */
balls.clear();
spawn_ball( true );
/* Clear out the list of powerups, too. */
powerups.clear();
/* Let the user know what level they're on. */
snprintf( splash_message, 30, "%s\n%02d", assets.get_text( STR_LEVEL ), level->get_level() );
splash_tween.start();
/* All done. */
return;
}
/*
* get_score - exposes the current score for the current game
*/
uint16_t GameState::get_score( void )
{
return score;
}
/*
* update - called every tick (~10ms) to update the state of the game.
*
* uint32_t - the elapsed time (in ms) since the game launched.
*/
gamestate_t GameState::update( uint32_t p_time )
{
/* Calculate any bat movement that's required. */
float l_movement = 0.0f;
/* Handle the joystick, which is slightly more gradiated. */
if ( blit::joystick.x < -0.66f )
{
l_movement = bat_speed * -1;
}
else if ( blit::joystick.x > 0.66f )
{
l_movement = bat_speed;
}
else
{
l_movement = bat_speed * 1.5f * blit::joystick.x;
}
/* But if the player has moved the dpad, then use that instead. */
if ( blit::buttons.state & blit::Button::DPAD_LEFT )
{
l_movement = bat_speed * -1;
}
if ( blit::buttons.state & blit::Button::DPAD_RIGHT )
{
l_movement = bat_speed;
}
/* And lastly, apply that movement. */
if ( l_movement != 0.0f )
{
move_bat( l_movement );
}
/* Next, if the user presses B and there's a ball on the bat, fire it. */
/* And yes, if we've collected multiple balls, we launch them all! */
if ( blit::buttons.pressed & blit::Button::B && lives > 0 )
{
/* Work through all our balls then. */
for ( auto l_ball : balls )
{
/* Only interested in one that's stuck. */
if ( l_ball->stuck )
{
/* Balls know how to launch themselves, happily. */
l_ball->launch();
/* But only launch one... */
break;
}
}
}
/* Next up, we work our way through all the balls we have, and update their */
/* positions. We'll deal with any collisions in a little while... */
for ( auto l_ball : balls )
{
bool l_bounce_vertical = false;
bool l_bounce_horizontal = false;
bool l_brick_destroyed = false;
blit::Point l_brick_location;
/* Fetch the bounds of the ball's current position. */
blit::Rect l_old_bounds = l_ball->get_bounds();
/* Update the balls position. */
l_ball->update();
/* And fetch the bounds of the ball in it's new location. */
blit::Rect l_new_bounds = l_ball->get_bounds();
/* Collision detect on the top of the screen. */
if ( l_new_bounds.y <= 0 )
{
output.trigger_haptic( 0.25f, 50 );
output.play_effect_bounce( FREQ_BOUNDS );
l_ball->bounce( false );
}
/* And the edges of the screen, which gives some points too! */
if ( ( l_new_bounds.x <= level->get_margin() && l_ball->moving_left() )
||
( ( l_new_bounds.x + l_new_bounds.w ) >= ( blit::screen.bounds.w - level->get_margin() ) && !l_ball->moving_left() ) )
{
score++;
output.trigger_haptic( 0.25f, 50 );
output.play_effect_bounce( FREQ_BOUNDS );
l_ball->bounce( true );
}
/* Now, check to see if our bounds have crossed into a new brick area. */
if ( l_ball->moving_up() )
{
/* Check if the brick row has changed for the leading (top) edge. */
blit::Point l_old_tl = screen_to_brick( l_old_bounds.tl() );
blit::Point l_new_tl = screen_to_brick( l_new_bounds.tl() );
blit::Point l_new_tr = screen_to_brick( l_new_bounds.tr() );
if ( l_old_tl.y != l_new_tl.y )
{
/* We've crossed a line. See if it's occupied! */
if ( level->get_brick( l_new_tl ) > 0 )
{
/* Bounce and increment the score. */
score += level->hit_brick( l_new_tl );
l_bounce_vertical = true;
/* Check to see if the brick was destroyed. */
if ( level->get_brick( l_new_tl ) == 0 )
{
l_brick_destroyed = true;
l_brick_location = l_new_tl;
}
}
/* Also consider the top right corner, in case we bounced on a boundary. */
if ( ( l_new_tl.x != l_new_tr.x ) || ( l_new_tl.y != l_new_tr.y ) )
{
if ( level->get_brick( l_new_tr ) > 0 )
{
/* Bounce, and increment the score. */
score += level->hit_brick( l_new_tr );
l_bounce_vertical = true;
/* Check to see if the brick was destroyed. */
if ( level->get_brick( l_new_tr ) == 0 )
{
l_brick_destroyed = true;
l_brick_location = l_new_tr;
}
}
}
}
}
else
{
/* Moving down, check if the brick row has changed on the bottom edge. */
blit::Point l_old_bl = screen_to_brick( l_old_bounds.bl() );
blit::Point l_new_bl = screen_to_brick( l_new_bounds.bl() );
blit::Point l_new_br = screen_to_brick( l_new_bounds.br() );
if ( l_old_bl.y != l_new_bl.y )
{
/* We've crossed a line. See if it's occupied! */
if ( level->get_brick( l_new_bl ) > 0 )
{
/* Bounce, and increment the score. */
score += level->hit_brick( l_new_bl );
l_bounce_vertical = true;
/* Check to see if the brick was destroyed. */
if ( level->get_brick( l_new_bl ) == 0 )
{
l_brick_destroyed = true;
l_brick_location = l_new_bl;
}
}
/* Also consider the top right corner, in case we bounced on a boundary. */
if ( ( l_new_bl.x != l_new_br.x ) || ( l_new_bl.y != l_new_br.y ) )
{
if ( level->get_brick( l_new_br ) > 0 )
{
/* Bounce, and increment the score. */
score += level->hit_brick( l_new_br );
l_bounce_vertical = true;
/* Check to see if the brick was destroyed. */
if ( level->get_brick( l_new_br ) == 0 )
{
l_brick_destroyed = true;
l_brick_location = l_new_br;
}
}
}
}
}
if ( l_ball->moving_left() )
{
/* Check if the brick row has changed for the leading (left) edge. */
blit::Point l_old_tl = screen_to_brick( l_old_bounds.tl() );
blit::Point l_new_tl = screen_to_brick( l_new_bounds.tl() );
blit::Point l_new_bl = screen_to_brick( l_new_bounds.bl() );
if ( l_old_tl.x != l_new_tl.x )
{
/* We've crossed a line. See if it's occupied! */
if ( level->get_brick( l_new_tl ) > 0 )
{
/* Bounce, and increment the score. */
score += level->hit_brick( l_new_tl );
l_bounce_horizontal = true;
/* Check to see if the brick was destroyed. */
if ( level->get_brick( l_new_tl ) == 0 )
{
l_brick_destroyed = true;
l_brick_location = l_new_tl;
}
}
/* Also consider the top right corner, in case we bounced on a boundary. */
if ( ( l_new_tl.x != l_new_bl.x ) || ( l_new_tl.y != l_new_bl.y ) )
{
if ( level->get_brick( l_new_bl ) > 0 )
{
/* Bounce, and increment the score. */
score += level->hit_brick( l_new_bl );
l_bounce_horizontal = true;
/* Check to see if the brick was destroyed. */
if ( level->get_brick( l_new_bl ) == 0 )
{
l_brick_destroyed = true;
l_brick_location = l_new_bl;
}
}
}
}
}
else
{
/* Moving down, check if the brick row has changed on the bottom edge. */
blit::Point l_old_tr = screen_to_brick( l_old_bounds.tr() );
blit::Point l_new_tr = screen_to_brick( l_new_bounds.tr() );
blit::Point l_new_br = screen_to_brick( l_new_bounds.br() );
if ( l_old_tr.x != l_new_tr.x )
{
/* We've crossed a line. See if it's occupied! */
if ( level->get_brick( l_new_tr ) > 0 )
{
/* Bounce, and increment the score. */
score += level->hit_brick( l_new_tr );
l_bounce_horizontal = true;
/* Check to see if the brick was destroyed. */
if ( level->get_brick( l_new_tr ) == 0 )
{
l_brick_destroyed = true;
l_brick_location = l_new_tr;
}
}
/* Also consider the top right corner, in case we bounced on a boundary. */
if ( ( l_new_tr.x != l_new_br.x ) || ( l_new_tr.y != l_new_br.y ) )
{
if ( level->get_brick( l_new_br ) > 0 )
{
/* Bounce, and increment the score. */
score += level->hit_brick( l_new_br );
l_bounce_horizontal = true;
/* Check to see if the brick was destroyed. */
if ( level->get_brick( l_new_br ) == 0 )
{
l_brick_destroyed = true;
l_brick_location = l_new_br;
}
}
}
}
}
/* Apply the required ball bounces. */
if ( l_bounce_vertical )
{
output.trigger_haptic( 0.25f, 50 );
output.play_effect_bounce( FREQ_BRICK );
l_ball->bounce( false );
}
if ( l_bounce_horizontal )
{
output.trigger_haptic( 0.25f, 50 );
output.play_effect_bounce( FREQ_BRICK );
l_ball->bounce( true );
}
/* If a brick was fully destroyed, maybe spawn a powerup. */
if ( ( l_brick_destroyed ) && ( ( blit::random() % 10 ) <= ( level->get_level() / 3 ) ) )
{
/* Work out the screen location of the brick. */
blit::Rect l_brick = brick_to_screen( l_brick_location.y, l_brick_location.x );
powerups.push_front( new PowerUp( l_brick.center() ) );
}
/* And lastly, the bat itself. */
if ( ( l_new_bounds.x < ( bat_position + bat_width[bat_type] / 2 ) ) &&
( ( l_new_bounds.x + l_new_bounds.w ) > ( bat_position - bat_width[bat_type] / 2 ) ) &&
( ( l_new_bounds.y + l_new_bounds.h ) >= bat_height ) )
{
if ( l_ball->bat_bounce( bat_height, bat_type == BAT_STICKY ) )
{
output.trigger_haptic( 0.25f, 50 );
output.play_effect_bounce( FREQ_BOUNDS );
}
}
}
/* Clean up any balls that drop off the bottom of the screen. */
balls.remove_if( [](auto l_ball) { return l_ball->get_bounds().y > blit::screen.bounds.h; } );
/* Work through all the powerups. */
for ( auto l_powerup : powerups )
{
/* Update the powerup position. */
l_powerup->update();
blit::Rect l_powerup_bounds = l_powerup->get_bounds();
/* Update the falling noise effect. */
output.play_effect_falling( l_powerup_bounds.center().y );
/* And then check to see if there's a collision with the bat. */
if ( l_powerup_bounds.intersects( bat_bounds() ) )
{
/* Apply the amazing power up. */
switch( l_powerup->get_type() )
{
case POWERUP_SPEED:
snprintf( splash_message, 30, "%s", assets.get_text( STR_POWERUP_SPEED ) );
bat_speed += 0.8f;
break;
case POWERUP_SLOW:
snprintf( splash_message, 30, "%s", assets.get_text( STR_POWERUP_SLOW ) );
bat_speed -= 0.6f;
if ( bat_speed < 0.5f )
{
bat_speed = 0.5f;
}
break;
case POWERUP_STICKY:
snprintf( splash_message, 30, "%s", assets.get_text( STR_POWERUP_STICKY ) );
bat_type = BAT_STICKY;
break;
case POWERUP_GROW:
snprintf( splash_message, 30, "%s", assets.get_text( STR_POWERUP_GROW ) );
if ( bat_type == BAT_NARROW )
{
bat_type = BAT_NORMAL;
}
else
{
bat_type = BAT_WIDE;
}
break;
case POWERUP_SHRINK:
snprintf( splash_message, 30, "%s", assets.get_text( STR_POWERUP_SHRINK ) );
if ( bat_type == BAT_WIDE )
{
bat_type = BAT_NORMAL;
}
else
{
bat_type = BAT_NARROW;
}
break;
case POWERUP_MULTI:
snprintf( splash_message, 30, "%s", assets.get_text( STR_POWERUP_MULTI ) );
spawn_ball( false );
spawn_ball( false );
break;
case POWERUP_EXTRA:
snprintf( splash_message, 30, "%s", assets.get_text( STR_POWERUP_EXTRA ) );
lives++;
break;
}
/* Splash a message for it. */
splash_tween.start();
/* Grant some points for it! */
score += 15;
/* Turn off the falling sound, and Ping! */
output.play_effect_falling( 0 );
output.play_effect_pickup();
/* And just drop the thing off the bottom of the screen; it'll get */
/* then get cleared up in a little while. */
l_powerup->remove();
}
}
/* And clean up any powerups that are off the screen too. */
powerups.remove_if( [](auto l_powerup) { return l_powerup->get_bounds().y > blit::screen.bounds.h; } );
/* If there are no more balls in play, then we lose a life. */
if ( std::distance( balls.begin(), balls.end() ) == 0 )
{
/* Switch the bat back to standard type and speed, too. */
bat_speed = 1.0f;
bat_type = BAT_NORMAL;
/* Reduce our lives, spawn a fresh ball if we can. */
lives--;
spawn_ball( true );
if ( lives == 0 )
{
snprintf( splash_message, 30, "%s", assets.get_text( STR_GAME_OVER ) );
}
else
{
snprintf( splash_message, 30, "%s", assets.get_text( STR_BALL_LOST ) );
}
splash_tween.start();
}
/* If after all that we have no more lives, it's game over. */
if ( lives == 0 && splash_tween.is_finished() )
{
return STATE_DEATH;
}
/* Lastly, check the level - if we've cleared all the clearable bricks, */
/* then it's time to move onto the next one! */
if ( level->get_brick_count() == 0 )
{
output.play_effect_level_complete();
load_level( level->get_level() + 1 );
}
/* The font pen we use will pulse more subtlely. */
font_pen.g = font_tween.value;
/* All done, remain in our current state */
return STATE_GAME;
}
/*
* render - called every frame (~20ms) to render the screen.
*
* uint32_t - the elapsed time (in ms) since the game launched.
*/
void GameState::render( uint32_t p_time )
{
uint8_t l_brick;
uint8_t l_lives_offset = 0;
bool l_stuck_ball = false;
char l_buffer[32];
/* Clear the screen down. */
blit::screen.clear();
/* Draw a smooth gradient backdrop. */
for ( uint16_t i = 0; i < blit::screen.bounds.h; i++ )
{
blit::screen.pen = blit::Pen( 10, 10, ( ( blit::screen.bounds.h - i ) / 2 ) );
blit::screen.h_span( blit::Point( 0, i ), blit::screen.bounds.w );
}
/* Draw in the score line. */
snprintf( l_buffer, 30, "%s: %05d", assets.get_text( STR_SCORE ), score );
blit::screen.pen = number_pen;
blit::screen.text(
l_buffer,
assets.number_font,
blit::Point( 1, 1 ),
true,
blit::TextAlign::top_left
);
snprintf( l_buffer, 30, "%s: %05d", assets.get_text( STR_HISCORE ), hiscore );
blit::screen.text(
l_buffer,
assets.number_font,
blit::Point( blit::screen.bounds.w - 1, 1 ),
true,
blit::TextAlign::top_right
);
/* And a display of the remaining lives. */
if ( TARGET_PICOSYSTEM == assets.get_platform() )
{
l_lives_offset = 10;
}
blit::screen.sprite(
blit::Rect( 0, SPRITE_ROW_BAT, 1, 1 ),
blit::Point( blit::screen.bounds.w / 2 - 24 + l_lives_offset, 1 )
);
blit::screen.sprite(
blit::Rect( 2, SPRITE_ROW_BAT, 1, 1 ),
blit::Point( blit::screen.bounds.w / 2 - 16 + l_lives_offset, 1 )
);
snprintf( l_buffer, 30, "x%d", lives );
blit::screen.text(
l_buffer,
assets.number_font,
blit::Point( blit::screen.bounds.w / 2 - 4 + l_lives_offset, 1 ),
true, blit::TextAlign::top_left
);
/* If we have a margin, we need to draw some walls in. */
if ( level->get_margin() > 0 )
{
for ( int l_index = level->get_margin() - 1; l_index >= 0; l_index-- )
{
blit::screen.alpha = 255 - ( l_index * ( 200 / level->get_margin() ) );
blit::screen.v_span(
blit::Point( l_index, 10 ), blit::screen.bounds.h - 10
);
blit::screen.v_span(
blit::Point( blit::screen.bounds.w - l_index, 10 ), blit::screen.bounds.h - 10
);
}
/* Reset the alpha. */
blit::screen.alpha = 255;
}
/* Now we work through the level one brick at a time... */
for ( uint8_t l_row = 0; l_row < level->get_height(); l_row++ )
{
for ( uint8_t l_column = 0; l_column < level->get_width(); l_column++ )
{
/* Fetch the brick for this location. */
l_brick = level->get_brick( l_row, l_column );
/* Move on if it's empty. */
if ( l_brick == 0 )
{
continue;
}
/* Then draw the appropriate brick from the spritesheet. */
blit::screen.sprite(
blit::Rect( ( l_brick - 1 ) * 4, SPRITE_ROW_BRICK, 4, 2 ),
blit::Point( ( l_column * 32 ) + level->get_margin(), l_row * 16 + 10 )
);
}
}
/* Add in the bat; the position is the centre location. */
switch( bat_type )
{
case BAT_NORMAL: /* Simple bat, three sprites wide. */
blit::screen.sprite(
blit::Rect( 0, SPRITE_ROW_BAT, 3, 1 ),
blit::Point( bat_position - ( bat_width[bat_type] / 2 ), bat_height )
);
break;
case BAT_NARROW: /* Shortened bat, two sprites wide. */
blit::screen.sprite(
blit::Rect( 0, SPRITE_ROW_BAT, 1, 1 ),
blit::Point( bat_position - ( bat_width[bat_type] / 2 ), bat_height )
);
blit::screen.sprite(
blit::Rect( 2, SPRITE_ROW_BAT, 1, 1 ),
blit::Point( bat_position - ( bat_width[bat_type] / 2 ) + 8, bat_height )
);
break;
case BAT_WIDE: /* Stretched bat, four sprites wide. */
blit::screen.sprite(
blit::Rect( 0, SPRITE_ROW_BAT, 2, 1 ),
blit::Point( bat_position - ( bat_width[bat_type] / 2 ), bat_height )
);
blit::screen.sprite(
blit::Rect( 1, SPRITE_ROW_BAT, 2, 1 ),
blit::Point( bat_position - ( bat_width[bat_type] / 2 ) + 16, bat_height )
);
break;
case BAT_STICKY: /* Sticky bat, three sprites wide. */
blit::screen.sprite(
blit::Rect( 3, SPRITE_ROW_BAT, 3, 1 ),
blit::Point( bat_position - ( bat_width[bat_type] / 2 ), bat_height )
);
break;
}
/* Render the powerups, too - these go behind (before) the balls. */
for ( auto l_powerup : powerups )
{
l_powerup->render();
}
/* Balls next; we could have a number of them, in a handy container. */
for ( auto l_ball : balls )
{
/* Render the ball. */
l_ball->render();
/* And remember if it's stuck to the bat... */
if ( l_ball->stuck )
{
l_stuck_ball = true;
}
}
/* So, if we have a stuck ball, explain what the user needs to do... */
if ( l_stuck_ball && lives > 0 )
{
blit::screen.pen = font_pen;
blit::screen.text(
assets.get_text( STR_B_TO_LAUNCH ),
assets.message_font,
blit::Point( blit::screen.bounds.w / 2, blit::screen.bounds.h - 45 ),
true,
blit::TextAlign::center_center
);
}
/* If there's a splash tween running, we have a message to display. */
if ( splash_tween.is_running() )
{
blit::screen.pen = font_pen;
blit::screen.pen.a = splash_tween.value;
blit::screen.text(
splash_message,
assets.splash_font,
blit::Point( blit::screen.bounds.w / 2, blit::screen.bounds.h / 2 ),
false,
blit::TextAlign::center_center
);
}
/* All done. */
return;
}
/* End of GameState.cpp */