Initial publish

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

Committer:
Noximilien
Date:
Mon Apr 15 12:59:51 2019 +0000
Revision:
29:579e00b7f118
Parent:
28:35af3843de8f
Child:
31:becb8f6bf7b7
Added a lot of comments.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Noximilien 21:0eb394495b8a 1 #ifndef STARS_H
Noximilien 21:0eb394495b8a 2 #define STARS_H
Noximilien 21:0eb394495b8a 3
Noximilien 23:240bc00ef25b 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 28:35af3843de8f 9 int stars_delay;
Noximilien 23:240bc00ef25b 10
Noximilien 29:579e00b7f118 11 /**Stars Class
Noximilien 29:579e00b7f118 12 * @brief A library for describing the background stars.
Noximilien 29:579e00b7f118 13 * @author Dmitrijs Griskovs
Noximilien 29:579e00b7f118 14 * @date 15/04/2019
Noximilien 29:579e00b7f118 15 */
Noximilien 27:f05f4e738ba9 16 class Stars {
Noximilien 23:240bc00ef25b 17 public:
Noximilien 23:240bc00ef25b 18 GameObject small_stars[max_small_stars];
Noximilien 23:240bc00ef25b 19 GameObject medium_stars[max_medium_stars];
Noximilien 23:240bc00ef25b 20
Noximilien 29:579e00b7f118 21 /** @brief Makes a small star active and gives it the start positions.
Noximilien 29:579e00b7f118 22 * @details Searches through the small stars array and if a star is not active,
Noximilien 29:579e00b7f118 23 * it becomes active and is given the position of x (which is 0) and.
Noximilien 29:579e00b7f118 24 * and random position of y.
Noximilien 29:579e00b7f118 25 */
Noximilien 23:240bc00ef25b 26 void newSmallStarFlies() {
Noximilien 23:240bc00ef25b 27 // Search the array of stars if inactive we can use it. - the same as with blasts
Noximilien 23:240bc00ef25b 28 int found = -1;
Noximilien 23:240bc00ef25b 29 for (int i = 0; i < max_small_stars; ++i) {
Noximilien 23:240bc00ef25b 30 if (!small_stars[i].active) {
Noximilien 23:240bc00ef25b 31 found = i;
Noximilien 23:240bc00ef25b 32 break;
Noximilien 23:240bc00ef25b 33 }
Noximilien 23:240bc00ef25b 34 }
Noximilien 23:240bc00ef25b 35
Noximilien 23:240bc00ef25b 36 if (found != -1) {
Noximilien 23:240bc00ef25b 37 small_stars[found].active = true;
Noximilien 23:240bc00ef25b 38 small_stars[found].pos.x = screen_width;
Noximilien 23:240bc00ef25b 39 small_stars[found].pos.y = rand() % screen_height + game_area_y;
Noximilien 21:0eb394495b8a 40 }
Noximilien 21:0eb394495b8a 41 }
Noximilien 29:579e00b7f118 42 /** @brief draws small stars on the screen.
Noximilien 29:579e00b7f118 43 * @details when a small star is active, this function updates the position and
Noximilien 29:579e00b7f118 44 * draws it on the screen. Also, When the star leaves the screen limits it
Noximilien 29:579e00b7f118 45 * deactivates the star for a new star to fly.
Noximilien 29:579e00b7f118 46 */
Noximilien 23:240bc00ef25b 47 void updateAndDrawSmallStars(){
Noximilien 23:240bc00ef25b 48 for (int i = 0; i < max_small_stars; ++i) {
Noximilien 23:240bc00ef25b 49 if (small_stars[i].active) {
Noximilien 23:240bc00ef25b 50 small_stars[i].pos.x -= small_star_speed;
Noximilien 23:240bc00ef25b 51 if (small_stars[i].pos.x <= 0){
Noximilien 23:240bc00ef25b 52 small_stars[i].active = false;
Noximilien 29:579e00b7f118 53 }
Noximilien 27:f05f4e738ba9 54 drawSprite(small_stars[i].pos, small_star_sprite);
Noximilien 21:0eb394495b8a 55 }
Noximilien 29:579e00b7f118 56 }
Noximilien 21:0eb394495b8a 57 }
Noximilien 29:579e00b7f118 58 /** @brief Makes a medium star active and gives it the start positions.
Noximilien 29:579e00b7f118 59 * @details Searches through the medium stars array and if a star is not active,
Noximilien 29:579e00b7f118 60 * it becomes active and is given the position of x (which is 0) and.
Noximilien 29:579e00b7f118 61 * and random position of y.
Noximilien 29:579e00b7f118 62 */
Noximilien 23:240bc00ef25b 63 void newMediumStarFlies() {
Noximilien 23:240bc00ef25b 64 // Search the array of stars if inactive we can use it. - the same as with blasts
Noximilien 23:240bc00ef25b 65 int found = -1;
Noximilien 23:240bc00ef25b 66 for (int i = 0; i < max_medium_stars; ++i) {
Noximilien 23:240bc00ef25b 67 if (!medium_stars[i].active) {
Noximilien 23:240bc00ef25b 68 found = i;
Noximilien 23:240bc00ef25b 69 break;
Noximilien 21:0eb394495b8a 70 }
Noximilien 23:240bc00ef25b 71 }
Noximilien 23:240bc00ef25b 72 if (found != -1) {
Noximilien 23:240bc00ef25b 73 medium_stars[found].active = true;
Noximilien 23:240bc00ef25b 74 medium_stars[found].pos.x = screen_width;
Noximilien 23:240bc00ef25b 75 medium_stars[found].pos.y = rand() % screen_height + game_area_y;
Noximilien 21:0eb394495b8a 76 }
Noximilien 21:0eb394495b8a 77 }
Noximilien 29:579e00b7f118 78 /** @brief draws medium stars on the screen.
Noximilien 29:579e00b7f118 79 * @details when a medium star is active, this function updates the position and
Noximilien 29:579e00b7f118 80 * draws it on the screen. Also, When the star leaves the screen limits it
Noximilien 29:579e00b7f118 81 * deactivates the star for a new star to fly.
Noximilien 29:579e00b7f118 82 */
Noximilien 23:240bc00ef25b 83 void updateAndDrawMediumStars(){
Noximilien 23:240bc00ef25b 84 for (int i = 0; i < max_medium_stars; ++i) {
Noximilien 23:240bc00ef25b 85 if (medium_stars[i].active) {
Noximilien 23:240bc00ef25b 86 medium_stars[i].pos.x -= medium_star_speed;
Noximilien 23:240bc00ef25b 87 if (medium_stars[i].pos.x <= -2){
Noximilien 23:240bc00ef25b 88 medium_stars[i].active = false;
Noximilien 23:240bc00ef25b 89 }
Noximilien 27:f05f4e738ba9 90 drawSprite(medium_stars[i].pos, medium_star_sprite);
Noximilien 23:240bc00ef25b 91 }
Noximilien 23:240bc00ef25b 92 }
Noximilien 23:240bc00ef25b 93 }
Noximilien 28:35af3843de8f 94
Noximilien 29:579e00b7f118 95 /** @brief A separate function for delaying the stars spawn time.*/
Noximilien 28:35af3843de8f 96 void starsSpawnDelay(){
Noximilien 28:35af3843de8f 97 if (stars_delay == stars_delay_max){
Noximilien 28:35af3843de8f 98 //This is dealy between stars generation.
Noximilien 28:35af3843de8f 99 newSmallStarFlies();
Noximilien 28:35af3843de8f 100 newMediumStarFlies();
Noximilien 28:35af3843de8f 101 stars_delay = 0;
Noximilien 28:35af3843de8f 102 }
Noximilien 28:35af3843de8f 103 else {stars_delay += 1;}
Noximilien 28:35af3843de8f 104 }
Noximilien 23:240bc00ef25b 105 };
Noximilien 21:0eb394495b8a 106 #endif