Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of el17dg by
game/stars.h@31:becb8f6bf7b7, 2019-04-23 (annotated)
- Committer:
- Noximilien
- Date:
- Tue Apr 23 18:18:57 2019 +0000
- Revision:
- 31:becb8f6bf7b7
- Parent:
- 29:579e00b7f118
- Child:
- 32:5403bb974294
Have changed comments structure as well as the content. Finished intro music. Have cleaned a code in some places to make it readable.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Noximilien | 21:0eb394495b8a | 1 | #ifndef STARS_H |
| Noximilien | 21:0eb394495b8a | 2 | #define STARS_H |
| Noximilien | 21:0eb394495b8a | 3 | |
| Noximilien | 31:becb8f6bf7b7 | 4 | /*const int max_small_stars = 5; |
| Noximilien | 23:240bc00ef25b | 5 | const int max_medium_stars = 5; |
| Noximilien | 27:f05f4e738ba9 | 6 | const int small_star_speed = 2; |
| Noximilien | 27:f05f4e738ba9 | 7 | const int medium_star_speed = 6; |
| Noximilien | 28:35af3843de8f | 8 | const int stars_delay_max = 5; |
| Noximilien | 31:becb8f6bf7b7 | 9 | int stars_delay;*/ |
| Noximilien | 23:240bc00ef25b | 10 | |
| Noximilien | 31:becb8f6bf7b7 | 11 | /** |
| Noximilien | 31:becb8f6bf7b7 | 12 | * Stars Class |
| Noximilien | 31:becb8f6bf7b7 | 13 | * @brief Manages, updates and draws the background stars. |
| Noximilien | 29:579e00b7f118 | 14 | * @author Dmitrijs Griskovs |
| Noximilien | 29:579e00b7f118 | 15 | * @date 15/04/2019 |
| Noximilien | 29:579e00b7f118 | 16 | */ |
| Noximilien | 27:f05f4e738ba9 | 17 | class Stars { |
| Noximilien | 23:240bc00ef25b | 18 | public: |
| Noximilien | 31:becb8f6bf7b7 | 19 | static const int max_small_stars = 10; |
| Noximilien | 31:becb8f6bf7b7 | 20 | static const int max_medium_stars = 5; |
| Noximilien | 31:becb8f6bf7b7 | 21 | static const int small_star_speed = 2; |
| Noximilien | 31:becb8f6bf7b7 | 22 | static const int medium_star_speed = 6; |
| Noximilien | 31:becb8f6bf7b7 | 23 | static const int stars_delay_max = 5; |
| Noximilien | 31:becb8f6bf7b7 | 24 | int stars_delay; |
| Noximilien | 31:becb8f6bf7b7 | 25 | |
| Noximilien | 23:240bc00ef25b | 26 | GameObject small_stars[max_small_stars]; |
| Noximilien | 23:240bc00ef25b | 27 | GameObject medium_stars[max_medium_stars]; |
| Noximilien | 23:240bc00ef25b | 28 | |
| Noximilien | 31:becb8f6bf7b7 | 29 | /** |
| Noximilien | 31:becb8f6bf7b7 | 30 | * @brief Makes a small star active and gives it the start positions. |
| Noximilien | 31:becb8f6bf7b7 | 31 | * @details Searches through the small stars array and if a star is not active, |
| Noximilien | 31:becb8f6bf7b7 | 32 | * it becomes active and is given the position of x (which is 0) and. |
| Noximilien | 31:becb8f6bf7b7 | 33 | * and random position of y. |
| Noximilien | 31:becb8f6bf7b7 | 34 | */ |
| Noximilien | 23:240bc00ef25b | 35 | void newSmallStarFlies() { |
| Noximilien | 23:240bc00ef25b | 36 | // Search the array of stars if inactive we can use it. - the same as with blasts |
| Noximilien | 23:240bc00ef25b | 37 | int found = -1; |
| Noximilien | 23:240bc00ef25b | 38 | for (int i = 0; i < max_small_stars; ++i) { |
| Noximilien | 23:240bc00ef25b | 39 | if (!small_stars[i].active) { |
| Noximilien | 23:240bc00ef25b | 40 | found = i; |
| Noximilien | 23:240bc00ef25b | 41 | break; |
| Noximilien | 23:240bc00ef25b | 42 | } |
| Noximilien | 23:240bc00ef25b | 43 | } |
| Noximilien | 23:240bc00ef25b | 44 | |
| Noximilien | 23:240bc00ef25b | 45 | if (found != -1) { |
| Noximilien | 23:240bc00ef25b | 46 | small_stars[found].active = true; |
| Noximilien | 23:240bc00ef25b | 47 | small_stars[found].pos.x = screen_width; |
| Noximilien | 23:240bc00ef25b | 48 | small_stars[found].pos.y = rand() % screen_height + game_area_y; |
| Noximilien | 21:0eb394495b8a | 49 | } |
| Noximilien | 21:0eb394495b8a | 50 | } |
| Noximilien | 31:becb8f6bf7b7 | 51 | /** |
| Noximilien | 31:becb8f6bf7b7 | 52 | * @brief draws small stars on the screen. |
| Noximilien | 31:becb8f6bf7b7 | 53 | * @details when a small star is active, this function updates the position and |
| Noximilien | 31:becb8f6bf7b7 | 54 | * draws it on the screen. Also, When the star leaves the screen limits it |
| Noximilien | 31:becb8f6bf7b7 | 55 | * deactivates the star for a new star to fly. |
| Noximilien | 31:becb8f6bf7b7 | 56 | */ |
| Noximilien | 23:240bc00ef25b | 57 | void updateAndDrawSmallStars(){ |
| Noximilien | 23:240bc00ef25b | 58 | for (int i = 0; i < max_small_stars; ++i) { |
| Noximilien | 23:240bc00ef25b | 59 | if (small_stars[i].active) { |
| Noximilien | 23:240bc00ef25b | 60 | small_stars[i].pos.x -= small_star_speed; |
| Noximilien | 23:240bc00ef25b | 61 | if (small_stars[i].pos.x <= 0){ |
| Noximilien | 23:240bc00ef25b | 62 | small_stars[i].active = false; |
| Noximilien | 29:579e00b7f118 | 63 | } |
| Noximilien | 27:f05f4e738ba9 | 64 | drawSprite(small_stars[i].pos, small_star_sprite); |
| Noximilien | 21:0eb394495b8a | 65 | } |
| Noximilien | 29:579e00b7f118 | 66 | } |
| Noximilien | 21:0eb394495b8a | 67 | } |
| Noximilien | 31:becb8f6bf7b7 | 68 | /** |
| Noximilien | 31:becb8f6bf7b7 | 69 | * @brief Makes a medium star active and gives it the start positions. |
| Noximilien | 31:becb8f6bf7b7 | 70 | * @details Searches through the medium stars array and if a star is not active, |
| Noximilien | 31:becb8f6bf7b7 | 71 | * it becomes active and is given the position of x (which is 0) and. |
| Noximilien | 31:becb8f6bf7b7 | 72 | * and random position of y. |
| Noximilien | 31:becb8f6bf7b7 | 73 | */ |
| Noximilien | 23:240bc00ef25b | 74 | void newMediumStarFlies() { |
| Noximilien | 23:240bc00ef25b | 75 | // Search the array of stars if inactive we can use it. - the same as with blasts |
| Noximilien | 23:240bc00ef25b | 76 | int found = -1; |
| Noximilien | 23:240bc00ef25b | 77 | for (int i = 0; i < max_medium_stars; ++i) { |
| Noximilien | 23:240bc00ef25b | 78 | if (!medium_stars[i].active) { |
| Noximilien | 23:240bc00ef25b | 79 | found = i; |
| Noximilien | 23:240bc00ef25b | 80 | break; |
| Noximilien | 21:0eb394495b8a | 81 | } |
| Noximilien | 23:240bc00ef25b | 82 | } |
| Noximilien | 23:240bc00ef25b | 83 | if (found != -1) { |
| Noximilien | 23:240bc00ef25b | 84 | medium_stars[found].active = true; |
| Noximilien | 23:240bc00ef25b | 85 | medium_stars[found].pos.x = screen_width; |
| Noximilien | 23:240bc00ef25b | 86 | medium_stars[found].pos.y = rand() % screen_height + game_area_y; |
| Noximilien | 21:0eb394495b8a | 87 | } |
| Noximilien | 21:0eb394495b8a | 88 | } |
| Noximilien | 31:becb8f6bf7b7 | 89 | /** |
| Noximilien | 31:becb8f6bf7b7 | 90 | * @brief draws medium stars on the screen. |
| Noximilien | 31:becb8f6bf7b7 | 91 | * @details when a medium star is active, this function updates the position and |
| Noximilien | 31:becb8f6bf7b7 | 92 | * draws it on the screen. Also, When the star leaves the screen limits it |
| Noximilien | 31:becb8f6bf7b7 | 93 | * deactivates the star for a new star to fly. |
| Noximilien | 31:becb8f6bf7b7 | 94 | */ |
| Noximilien | 23:240bc00ef25b | 95 | void updateAndDrawMediumStars(){ |
| Noximilien | 23:240bc00ef25b | 96 | for (int i = 0; i < max_medium_stars; ++i) { |
| Noximilien | 23:240bc00ef25b | 97 | if (medium_stars[i].active) { |
| Noximilien | 23:240bc00ef25b | 98 | medium_stars[i].pos.x -= medium_star_speed; |
| Noximilien | 23:240bc00ef25b | 99 | if (medium_stars[i].pos.x <= -2){ |
| Noximilien | 23:240bc00ef25b | 100 | medium_stars[i].active = false; |
| Noximilien | 23:240bc00ef25b | 101 | } |
| Noximilien | 27:f05f4e738ba9 | 102 | drawSprite(medium_stars[i].pos, medium_star_sprite); |
| Noximilien | 23:240bc00ef25b | 103 | } |
| Noximilien | 23:240bc00ef25b | 104 | } |
| Noximilien | 23:240bc00ef25b | 105 | } |
| Noximilien | 28:35af3843de8f | 106 | |
| Noximilien | 31:becb8f6bf7b7 | 107 | /** @brief A separate function for delaying the stars spawn time.*/ |
| Noximilien | 28:35af3843de8f | 108 | void starsSpawnDelay(){ |
| Noximilien | 28:35af3843de8f | 109 | if (stars_delay == stars_delay_max){ |
| Noximilien | 31:becb8f6bf7b7 | 110 | //This is dealy between stars generation. |
| Noximilien | 31:becb8f6bf7b7 | 111 | newSmallStarFlies(); |
| Noximilien | 31:becb8f6bf7b7 | 112 | newMediumStarFlies(); |
| Noximilien | 31:becb8f6bf7b7 | 113 | stars_delay = 0; |
| Noximilien | 28:35af3843de8f | 114 | } |
| Noximilien | 31:becb8f6bf7b7 | 115 | else { |
| Noximilien | 31:becb8f6bf7b7 | 116 | stars_delay += 1; |
| Noximilien | 31:becb8f6bf7b7 | 117 | } |
| Noximilien | 28:35af3843de8f | 118 | } |
| Noximilien | 31:becb8f6bf7b7 | 119 | private: |
| Noximilien | 23:240bc00ef25b | 120 | }; |
| Noximilien | 21:0eb394495b8a | 121 | #endif |
