Initial publish
Dependencies: mbed
Fork of el17dg by
game/stars.h@35:172db1608332, 2019-04-30 (annotated)
- Committer:
- Noximilien
- Date:
- Tue Apr 30 20:51:39 2019 +0000
- Revision:
- 35:172db1608332
- Parent:
- 34:754915ce9de5
- Child:
- 36:207ec7db8648
Have finished added comments. Cleaned the code. added a note to the player. added intro credits.
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 | /** |
Noximilien | 31:becb8f6bf7b7 | 5 | * Stars Class |
Noximilien | 31:becb8f6bf7b7 | 6 | * @brief Manages, updates and draws the background stars. |
Noximilien | 29:579e00b7f118 | 7 | * @author Dmitrijs Griskovs |
Noximilien | 29:579e00b7f118 | 8 | * @date 15/04/2019 |
Noximilien | 29:579e00b7f118 | 9 | */ |
Noximilien | 27:f05f4e738ba9 | 10 | class Stars { |
Noximilien | 23:240bc00ef25b | 11 | public: |
Noximilien | 32:5403bb974294 | 12 | /** |
Noximilien | 32:5403bb974294 | 13 | * @var static const int max_small_stars variable; |
Noximilien | 32:5403bb974294 | 14 | * @brief Sets the limit of maximum small stars allowed on the screen. |
Noximilien | 32:5403bb974294 | 15 | */ |
Noximilien | 31:becb8f6bf7b7 | 16 | static const int max_small_stars = 10; |
Noximilien | 32:5403bb974294 | 17 | /** |
Noximilien | 32:5403bb974294 | 18 | * @var static const int max_medium_stars variable; |
Noximilien | 32:5403bb974294 | 19 | * @brief Sets the limit of maximum medium stars allowed on the screen. |
Noximilien | 32:5403bb974294 | 20 | */ |
Noximilien | 31:becb8f6bf7b7 | 21 | static const int max_medium_stars = 5; |
Noximilien | 31:becb8f6bf7b7 | 22 | |
Noximilien | 23:240bc00ef25b | 23 | GameObject small_stars[max_small_stars]; |
Noximilien | 23:240bc00ef25b | 24 | GameObject medium_stars[max_medium_stars]; |
Noximilien | 23:240bc00ef25b | 25 | |
Noximilien | 31:becb8f6bf7b7 | 26 | /** |
Noximilien | 31:becb8f6bf7b7 | 27 | * @brief Makes a small star active and gives it the start positions. |
Noximilien | 31:becb8f6bf7b7 | 28 | * @details Searches through the small stars array and if a star is not active, |
Noximilien | 31:becb8f6bf7b7 | 29 | * it becomes active and is given the position of x (which is 0) and. |
Noximilien | 31:becb8f6bf7b7 | 30 | * and random position of y. |
Noximilien | 31:becb8f6bf7b7 | 31 | */ |
Noximilien | 23:240bc00ef25b | 32 | void newSmallStarFlies() { |
Noximilien | 23:240bc00ef25b | 33 | // Search the array of stars if inactive we can use it. - the same as with blasts |
Noximilien | 23:240bc00ef25b | 34 | int found = -1; |
Noximilien | 23:240bc00ef25b | 35 | for (int i = 0; i < max_small_stars; ++i) { |
Noximilien | 23:240bc00ef25b | 36 | if (!small_stars[i].active) { |
Noximilien | 23:240bc00ef25b | 37 | found = i; |
Noximilien | 23:240bc00ef25b | 38 | break; |
Noximilien | 23:240bc00ef25b | 39 | } |
Noximilien | 23:240bc00ef25b | 40 | } |
Noximilien | 23:240bc00ef25b | 41 | |
Noximilien | 23:240bc00ef25b | 42 | if (found != -1) { |
Noximilien | 23:240bc00ef25b | 43 | small_stars[found].active = true; |
Noximilien | 23:240bc00ef25b | 44 | small_stars[found].pos.x = screen_width; |
Noximilien | 23:240bc00ef25b | 45 | small_stars[found].pos.y = rand() % screen_height + game_area_y; |
Noximilien | 21:0eb394495b8a | 46 | } |
Noximilien | 21:0eb394495b8a | 47 | } |
Noximilien | 31:becb8f6bf7b7 | 48 | /** |
Noximilien | 31:becb8f6bf7b7 | 49 | * @brief draws small stars on the screen. |
Noximilien | 31:becb8f6bf7b7 | 50 | * @details when a small star is active, this function updates the position and |
Noximilien | 31:becb8f6bf7b7 | 51 | * draws it on the screen. Also, When the star leaves the screen limits it |
Noximilien | 31:becb8f6bf7b7 | 52 | * deactivates the star for a new star to fly. |
Noximilien | 31:becb8f6bf7b7 | 53 | */ |
Noximilien | 23:240bc00ef25b | 54 | void updateAndDrawSmallStars(){ |
Noximilien | 23:240bc00ef25b | 55 | for (int i = 0; i < max_small_stars; ++i) { |
Noximilien | 23:240bc00ef25b | 56 | if (small_stars[i].active) { |
Noximilien | 23:240bc00ef25b | 57 | small_stars[i].pos.x -= small_star_speed; |
Noximilien | 23:240bc00ef25b | 58 | if (small_stars[i].pos.x <= 0){ |
Noximilien | 23:240bc00ef25b | 59 | small_stars[i].active = false; |
Noximilien | 29:579e00b7f118 | 60 | } |
Noximilien | 32:5403bb974294 | 61 | drawSpriteOnTop(small_stars[i].pos, small_star_sprite); |
Noximilien | 21:0eb394495b8a | 62 | } |
Noximilien | 29:579e00b7f118 | 63 | } |
Noximilien | 21:0eb394495b8a | 64 | } |
Noximilien | 31:becb8f6bf7b7 | 65 | /** |
Noximilien | 31:becb8f6bf7b7 | 66 | * @brief Makes a medium star active and gives it the start positions. |
Noximilien | 31:becb8f6bf7b7 | 67 | * @details Searches through the medium stars array and if a star is not active, |
Noximilien | 31:becb8f6bf7b7 | 68 | * it becomes active and is given the position of x (which is 0) and. |
Noximilien | 31:becb8f6bf7b7 | 69 | * and random position of y. |
Noximilien | 31:becb8f6bf7b7 | 70 | */ |
Noximilien | 23:240bc00ef25b | 71 | void newMediumStarFlies() { |
Noximilien | 23:240bc00ef25b | 72 | // Search the array of stars if inactive we can use it. - the same as with blasts |
Noximilien | 23:240bc00ef25b | 73 | int found = -1; |
Noximilien | 23:240bc00ef25b | 74 | for (int i = 0; i < max_medium_stars; ++i) { |
Noximilien | 23:240bc00ef25b | 75 | if (!medium_stars[i].active) { |
Noximilien | 23:240bc00ef25b | 76 | found = i; |
Noximilien | 23:240bc00ef25b | 77 | break; |
Noximilien | 21:0eb394495b8a | 78 | } |
Noximilien | 23:240bc00ef25b | 79 | } |
Noximilien | 23:240bc00ef25b | 80 | if (found != -1) { |
Noximilien | 23:240bc00ef25b | 81 | medium_stars[found].active = true; |
Noximilien | 23:240bc00ef25b | 82 | medium_stars[found].pos.x = screen_width; |
Noximilien | 23:240bc00ef25b | 83 | medium_stars[found].pos.y = rand() % screen_height + game_area_y; |
Noximilien | 21:0eb394495b8a | 84 | } |
Noximilien | 21:0eb394495b8a | 85 | } |
Noximilien | 31:becb8f6bf7b7 | 86 | /** |
Noximilien | 31:becb8f6bf7b7 | 87 | * @brief draws medium stars on the screen. |
Noximilien | 31:becb8f6bf7b7 | 88 | * @details when a medium star is active, this function updates the position and |
Noximilien | 31:becb8f6bf7b7 | 89 | * draws it on the screen. Also, When the star leaves the screen limits it |
Noximilien | 31:becb8f6bf7b7 | 90 | * deactivates the star for a new star to fly. |
Noximilien | 31:becb8f6bf7b7 | 91 | */ |
Noximilien | 23:240bc00ef25b | 92 | void updateAndDrawMediumStars(){ |
Noximilien | 23:240bc00ef25b | 93 | for (int i = 0; i < max_medium_stars; ++i) { |
Noximilien | 23:240bc00ef25b | 94 | if (medium_stars[i].active) { |
Noximilien | 23:240bc00ef25b | 95 | medium_stars[i].pos.x -= medium_star_speed; |
Noximilien | 23:240bc00ef25b | 96 | if (medium_stars[i].pos.x <= -2){ |
Noximilien | 23:240bc00ef25b | 97 | medium_stars[i].active = false; |
Noximilien | 23:240bc00ef25b | 98 | } |
Noximilien | 32:5403bb974294 | 99 | drawSpriteOnTop(medium_stars[i].pos, medium_star_sprite); |
Noximilien | 23:240bc00ef25b | 100 | } |
Noximilien | 23:240bc00ef25b | 101 | } |
Noximilien | 23:240bc00ef25b | 102 | } |
Noximilien | 28:35af3843de8f | 103 | |
Noximilien | 31:becb8f6bf7b7 | 104 | /** @brief A separate function for delaying the stars spawn time.*/ |
Noximilien | 28:35af3843de8f | 105 | void starsSpawnDelay(){ |
Noximilien | 28:35af3843de8f | 106 | if (stars_delay == stars_delay_max){ |
Noximilien | 31:becb8f6bf7b7 | 107 | //This is dealy between stars generation. |
Noximilien | 31:becb8f6bf7b7 | 108 | newSmallStarFlies(); |
Noximilien | 31:becb8f6bf7b7 | 109 | newMediumStarFlies(); |
Noximilien | 31:becb8f6bf7b7 | 110 | stars_delay = 0; |
Noximilien | 28:35af3843de8f | 111 | } |
Noximilien | 31:becb8f6bf7b7 | 112 | else { |
Noximilien | 31:becb8f6bf7b7 | 113 | stars_delay += 1; |
Noximilien | 31:becb8f6bf7b7 | 114 | } |
Noximilien | 28:35af3843de8f | 115 | } |
Noximilien | 35:172db1608332 | 116 | /** |
Noximilien | 35:172db1608332 | 117 | * @var int stars delay; |
Noximilien | 35:172db1608332 | 118 | * @brief The value is incremented every time until it reaches stars_delay_max |
Noximilien | 35:172db1608332 | 119 | * value, which triggers the stars spawn and then is reseted to zero. . |
Noximilien | 35:172db1608332 | 120 | */ |
Noximilien | 35:172db1608332 | 121 | int stars_delay; |
Noximilien | 35:172db1608332 | 122 | private: |
Noximilien | 35:172db1608332 | 123 | /** |
Noximilien | 35:172db1608332 | 124 | * @var static const int small_star_speed variable; |
Noximilien | 35:172db1608332 | 125 | * @brief Sets the speed of the small stars. |
Noximilien | 35:172db1608332 | 126 | */ |
Noximilien | 35:172db1608332 | 127 | static const int small_star_speed = 3; |
Noximilien | 35:172db1608332 | 128 | /** |
Noximilien | 35:172db1608332 | 129 | * @var static const int medium_star_speed variable; |
Noximilien | 35:172db1608332 | 130 | * @brief Sets the speed of the medium stars. |
Noximilien | 35:172db1608332 | 131 | */ |
Noximilien | 35:172db1608332 | 132 | static const int medium_star_speed = 6; |
Noximilien | 35:172db1608332 | 133 | /** |
Noximilien | 35:172db1608332 | 134 | * @var static const int stars_delay_max variable; |
Noximilien | 35:172db1608332 | 135 | * @brief Sets the limit of the stars delay spawn. |
Noximilien | 35:172db1608332 | 136 | */ |
Noximilien | 35:172db1608332 | 137 | static const int stars_delay_max = 5; |
Noximilien | 23:240bc00ef25b | 138 | }; |
Noximilien | 21:0eb394495b8a | 139 | #endif |