harry rance
/
Revised_Space_Invaders
Harry Rance 200925395 Embedded Systems Project
GameEngine.h@7:569f3fc70ac5, 2017-05-03 (annotated)
- Committer:
- harryrance
- Date:
- Wed May 03 16:33:20 2017 +0000
- Revision:
- 7:569f3fc70ac5
- Parent:
- 6:dca8b5e2ebe5
Committed with full documentation generated.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
harryrance | 0:c9bf674fe0c7 | 1 | #ifndef GAMEENGINE_H |
harryrance | 0:c9bf674fe0c7 | 2 | #define GAMEENGINE_H |
harryrance | 0:c9bf674fe0c7 | 3 | |
harryrance | 0:c9bf674fe0c7 | 4 | #include "mbed.h" |
harryrance | 0:c9bf674fe0c7 | 5 | #include "N5110.h" |
harryrance | 0:c9bf674fe0c7 | 6 | #include "Gamepad.h" |
harryrance | 2:50feb42b982c | 7 | #include "Boss.h" |
harryrance | 0:c9bf674fe0c7 | 8 | #include "UserShip.h" |
harryrance | 0:c9bf674fe0c7 | 9 | #include "AliensArray.h" |
harryrance | 1:95d7dd44bb0d | 10 | #include "Bullet.h" |
harryrance | 3:43970d8d642e | 11 | #include "Menu.h" |
harryrance | 0:c9bf674fe0c7 | 12 | |
harryrance | 6:dca8b5e2ebe5 | 13 | /** GameEngine Class |
harryrance | 6:dca8b5e2ebe5 | 14 | @brief Library used for bringing together all of the individual libraries and implementing the full game mechanics. |
harryrance | 6:dca8b5e2ebe5 | 15 | @brief Can be used to draw all of the game objects together at their correct times. |
harryrance | 6:dca8b5e2ebe5 | 16 | @brief Also used to update all game parameters, check for different object collisions, and move to different stages in the game. |
harryrance | 6:dca8b5e2ebe5 | 17 | |
harryrance | 6:dca8b5e2ebe5 | 18 | @author Harry Rance |
harryrance | 6:dca8b5e2ebe5 | 19 | @date 2nd May 2017 |
harryrance | 6:dca8b5e2ebe5 | 20 | |
harryrance | 6:dca8b5e2ebe5 | 21 | */ |
harryrance | 0:c9bf674fe0c7 | 22 | class GameEngine |
harryrance | 0:c9bf674fe0c7 | 23 | { |
harryrance | 0:c9bf674fe0c7 | 24 | public: |
harryrance | 6:dca8b5e2ebe5 | 25 | |
harryrance | 6:dca8b5e2ebe5 | 26 | //Constructor |
harryrance | 0:c9bf674fe0c7 | 27 | GameEngine(); |
harryrance | 6:dca8b5e2ebe5 | 28 | |
harryrance | 6:dca8b5e2ebe5 | 29 | //Destructor |
harryrance | 0:c9bf674fe0c7 | 30 | ~GameEngine(); |
harryrance | 6:dca8b5e2ebe5 | 31 | |
harryrance | 6:dca8b5e2ebe5 | 32 | /** Initialise |
harryrance | 6:dca8b5e2ebe5 | 33 | * Initialises the origin for the user ship, original alien array and speed of the game. |
harryrance | 6:dca8b5e2ebe5 | 34 | * @param ship_x_origin - controls the starting origin of the user ship in the x direction (0-83). |
harryrance | 6:dca8b5e2ebe5 | 35 | * @param alien_x_origin - controls the starting origin of the alien array in the x direction (0-83). |
harryrance | 6:dca8b5e2ebe5 | 36 | * @param speed - initialises the starting speed of the alien movement array. |
harryrance | 6:dca8b5e2ebe5 | 37 | */ |
harryrance | 2:50feb42b982c | 38 | void initialise(int ship_x_origin, int alien_x_origin, int speed); |
harryrance | 6:dca8b5e2ebe5 | 39 | |
harryrance | 6:dca8b5e2ebe5 | 40 | /** Read Input |
harryrance | 6:dca8b5e2ebe5 | 41 | * Reads the user inputs to the Game Pad so that they can be used in order to control the game. |
harryrance | 6:dca8b5e2ebe5 | 42 | */ |
harryrance | 0:c9bf674fe0c7 | 43 | void read_input(Gamepad &pad); |
harryrance | 6:dca8b5e2ebe5 | 44 | |
harryrance | 6:dca8b5e2ebe5 | 45 | /** Update |
harryrance | 6:dca8b5e2ebe5 | 46 | * Updates all of the game parameters, such as checking if the boss fight should be active, or calling collision checks. |
harryrance | 6:dca8b5e2ebe5 | 47 | */ |
harryrance | 0:c9bf674fe0c7 | 48 | void update(Gamepad &pad); |
harryrance | 6:dca8b5e2ebe5 | 49 | |
harryrance | 6:dca8b5e2ebe5 | 50 | /** Draw |
harryrance | 6:dca8b5e2ebe5 | 51 | * Draws all of the required game elements at the correct times in the game. |
harryrance | 6:dca8b5e2ebe5 | 52 | * Also draws with regards to the updates game parameters. |
harryrance | 6:dca8b5e2ebe5 | 53 | */ |
harryrance | 0:c9bf674fe0c7 | 54 | void draw(N5110 &lcd); |
harryrance | 0:c9bf674fe0c7 | 55 | |
harryrance | 0:c9bf674fe0c7 | 56 | private: |
harryrance | 6:dca8b5e2ebe5 | 57 | /** Check Alien-Wall Collision |
harryrance | 6:dca8b5e2ebe5 | 58 | * Checks whether the alien array has reached the edge of the screen. |
harryrance | 6:dca8b5e2ebe5 | 59 | * Changes x direction movement accordingly, and drops the array down a few pixels each time this happens. |
harryrance | 6:dca8b5e2ebe5 | 60 | */ |
harryrance | 0:c9bf674fe0c7 | 61 | void check_alien_wall_collision(Gamepad &pad); |
harryrance | 6:dca8b5e2ebe5 | 62 | |
harryrance | 6:dca8b5e2ebe5 | 63 | /** Check 'End' Game |
harryrance | 6:dca8b5e2ebe5 | 64 | * Checks whether the initial array of aliens has reached the bottom of the screen, or has been eliminated. |
harryrance | 6:dca8b5e2ebe5 | 65 | * After either of these conditions are satisfied, the game moves into the boss fight section. |
harryrance | 6:dca8b5e2ebe5 | 66 | */ |
harryrance | 0:c9bf674fe0c7 | 67 | void check_end_game(N5110 &lcd); |
harryrance | 6:dca8b5e2ebe5 | 68 | |
harryrance | 6:dca8b5e2ebe5 | 69 | /** Set Bullet Position |
harryrance | 6:dca8b5e2ebe5 | 70 | * Used to disconnect the x direction movement of the ship from the bullet. |
harryrance | 6:dca8b5e2ebe5 | 71 | * This means that the ship can move in the x direction without changing the bullet's x position. |
harryrance | 6:dca8b5e2ebe5 | 72 | * This was implemented due to the original x position of the bullet being referenced from the ship x position causing issues. |
harryrance | 6:dca8b5e2ebe5 | 73 | */ |
harryrance | 1:95d7dd44bb0d | 74 | void set_bullet_position(Gamepad &pad); |
harryrance | 6:dca8b5e2ebe5 | 75 | |
harryrance | 6:dca8b5e2ebe5 | 76 | /** Check Bullet-Alien Collision |
harryrance | 6:dca8b5e2ebe5 | 77 | * Checks whether a collision has occurred between the alien sprites and the bullet sprite. |
harryrance | 6:dca8b5e2ebe5 | 78 | * Does this by checking the x and y position of both the bullet and each alien in the array. |
harryrance | 6:dca8b5e2ebe5 | 79 | */ |
harryrance | 2:50feb42b982c | 80 | void check_bullet_alien_collision(Gamepad &pad); |
harryrance | 6:dca8b5e2ebe5 | 81 | |
harryrance | 6:dca8b5e2ebe5 | 82 | /** Eliminate Aliens in line 1 |
harryrance | 6:dca8b5e2ebe5 | 83 | * Scans the first line of the _aliens_active array to see whether an alien sprite needs to be eliminated. |
harryrance | 6:dca8b5e2ebe5 | 84 | * Does this by changing the value from a 1 to a 0 in the array, and 'reset_bullet' is also called here. |
harryrance | 6:dca8b5e2ebe5 | 85 | */ |
harryrance | 3:43970d8d642e | 86 | void eliminate_alien_line_0(Gamepad &pad); |
harryrance | 6:dca8b5e2ebe5 | 87 | |
harryrance | 6:dca8b5e2ebe5 | 88 | /** Eliminate Aliens in line 2 |
harryrance | 6:dca8b5e2ebe5 | 89 | * Scans the second line of the _aliens_active array to see whether an alien sprite needs to be eliminated. |
harryrance | 6:dca8b5e2ebe5 | 90 | * Does this by changing the value from a 1 to a 0 in the array, and 'reset_bullet' is also called here. |
harryrance | 6:dca8b5e2ebe5 | 91 | */ |
harryrance | 3:43970d8d642e | 92 | void eliminate_alien_line_1_1(Gamepad &pad); |
harryrance | 6:dca8b5e2ebe5 | 93 | |
harryrance | 6:dca8b5e2ebe5 | 94 | /** Reset Bullet (Aliens Section) |
harryrance | 6:dca8b5e2ebe5 | 95 | * Resets the bullet speed and position variables in the aliens array section of the game. |
harryrance | 6:dca8b5e2ebe5 | 96 | */ |
harryrance | 2:50feb42b982c | 97 | void reset_bullet(); |
harryrance | 6:dca8b5e2ebe5 | 98 | |
harryrance | 6:dca8b5e2ebe5 | 99 | /** Play Elimination tone |
harryrance | 6:dca8b5e2ebe5 | 100 | * Plays a small tone every time the bullet collides with the aliens or if a bomb collides with the user ship. |
harryrance | 6:dca8b5e2ebe5 | 101 | */ |
harryrance | 3:43970d8d642e | 102 | void play_elimination_tone(Gamepad &pad); |
harryrance | 6:dca8b5e2ebe5 | 103 | |
harryrance | 6:dca8b5e2ebe5 | 104 | /** Reset Bullet (For Boss) |
harryrance | 6:dca8b5e2ebe5 | 105 | * Resets the bullet speed and position variables for the boss fight section of the game. |
harryrance | 6:dca8b5e2ebe5 | 106 | */ |
harryrance | 2:50feb42b982c | 107 | void reset_bullet_for_boss(); |
harryrance | 6:dca8b5e2ebe5 | 108 | |
harryrance | 6:dca8b5e2ebe5 | 109 | /** Print Score & Coins |
harryrance | 6:dca8b5e2ebe5 | 110 | * Prints the score and coins at the top left and right of the screen respectively. |
harryrance | 6:dca8b5e2ebe5 | 111 | * Gets the current score and coins from the Bullet.h file. |
harryrance | 6:dca8b5e2ebe5 | 112 | * Updates in realtime on the screen in order for the user to see their score and balance as the game progresses. |
harryrance | 6:dca8b5e2ebe5 | 113 | */ |
harryrance | 2:50feb42b982c | 114 | void print_score_coins(N5110 &lcd); |
harryrance | 6:dca8b5e2ebe5 | 115 | |
harryrance | 6:dca8b5e2ebe5 | 116 | /** Move Boss |
harryrance | 6:dca8b5e2ebe5 | 117 | * Function to control movement of the boss sprite with regards to its x and y position and velocity. |
harryrance | 6:dca8b5e2ebe5 | 118 | * Changes direction of movement if the boss has reached the edge of its movement boundary. |
harryrance | 6:dca8b5e2ebe5 | 119 | */ |
harryrance | 2:50feb42b982c | 120 | void move_boss(Gamepad &pad); |
harryrance | 6:dca8b5e2ebe5 | 121 | |
harryrance | 6:dca8b5e2ebe5 | 122 | /** Boss Fight Health Bar |
harryrance | 6:dca8b5e2ebe5 | 123 | * Used to decrement the boss's health and health bar when the bullet collides with the boss sprite. |
harryrance | 6:dca8b5e2ebe5 | 124 | * Also checks whether the fight should be complete, or whether it should carry on. |
harryrance | 6:dca8b5e2ebe5 | 125 | */ |
harryrance | 2:50feb42b982c | 126 | void boss_fight_health_bar(N5110 &lcd); |
harryrance | 6:dca8b5e2ebe5 | 127 | |
harryrance | 6:dca8b5e2ebe5 | 128 | /** Check Fight Complete |
harryrance | 6:dca8b5e2ebe5 | 129 | * Checks whether the user has beaten the game, and prints strings containing a message and the user score accordingly. |
harryrance | 6:dca8b5e2ebe5 | 130 | */ |
harryrance | 2:50feb42b982c | 131 | void check_fight_complete(N5110 &lcd); |
harryrance | 6:dca8b5e2ebe5 | 132 | |
harryrance | 6:dca8b5e2ebe5 | 133 | /** Check Bomb-Ship Collision |
harryrance | 6:dca8b5e2ebe5 | 134 | * Checks whether the Boss Fight bombs have collided with the user ship, and updates game parameters accordingly. |
harryrance | 6:dca8b5e2ebe5 | 135 | */ |
harryrance | 2:50feb42b982c | 136 | void check_bomb_ship_collision(Gamepad &pad); |
harryrance | 6:dca8b5e2ebe5 | 137 | |
harryrance | 6:dca8b5e2ebe5 | 138 | /** End Game |
harryrance | 6:dca8b5e2ebe5 | 139 | * Ends the game if all of the user lives have been depleted. |
harryrance | 6:dca8b5e2ebe5 | 140 | * Prints strings on the screen telling the user the game is over, and their score. |
harryrance | 6:dca8b5e2ebe5 | 141 | */ |
harryrance | 2:50feb42b982c | 142 | void end_game(N5110 &lcd); |
harryrance | 6:dca8b5e2ebe5 | 143 | |
harryrance | 6:dca8b5e2ebe5 | 144 | /** Buy Lives |
harryrance | 6:dca8b5e2ebe5 | 145 | * Function to buy lives if the user pressed the L button whilst having >= 100 coins. |
harryrance | 6:dca8b5e2ebe5 | 146 | * Used for the boss fight section of the game. |
harryrance | 6:dca8b5e2ebe5 | 147 | */ |
harryrance | 3:43970d8d642e | 148 | void buy_lives(Gamepad &pad); |
harryrance | 6:dca8b5e2ebe5 | 149 | |
harryrance | 6:dca8b5e2ebe5 | 150 | /** Buy Ships |
harryrance | 6:dca8b5e2ebe5 | 151 | * Function to define which button presses, combined with the amount of user coins, can be used to buy different ships. |
harryrance | 6:dca8b5e2ebe5 | 152 | */ |
harryrance | 3:43970d8d642e | 153 | void buy_ships(Gamepad &pad); |
harryrance | 6:dca8b5e2ebe5 | 154 | |
harryrance | 6:dca8b5e2ebe5 | 155 | /** Give 'Cheat' Coins |
harryrance | 6:dca8b5e2ebe5 | 156 | * Developer type function used to give coins every time the joystick is pressed in. |
harryrance | 6:dca8b5e2ebe5 | 157 | * Used for demonstration purposes to give coins to demonstrate shop functionality. |
harryrance | 6:dca8b5e2ebe5 | 158 | * Saves time instead of having to play the game multiple times to get the correct amount of coins. |
harryrance | 6:dca8b5e2ebe5 | 159 | */ |
harryrance | 3:43970d8d642e | 160 | void give_cheat_coins(Gamepad &pad); |
harryrance | 6:dca8b5e2ebe5 | 161 | |
harryrance | 6:dca8b5e2ebe5 | 162 | // The following 4 functions are used to set either 6, 4, 2 or 0 of the onboard LEDs active. |
harryrance | 3:43970d8d642e | 163 | void set_six_leds(Gamepad &pad); |
harryrance | 3:43970d8d642e | 164 | void set_four_leds(Gamepad &pad); |
harryrance | 3:43970d8d642e | 165 | void set_two_leds(Gamepad &pad); |
harryrance | 3:43970d8d642e | 166 | void set_zero_leds(Gamepad &pad); |
harryrance | 6:dca8b5e2ebe5 | 167 | |
harryrance | 6:dca8b5e2ebe5 | 168 | /**Set Lives LEDs |
harryrance | 6:dca8b5e2ebe5 | 169 | * Used to set the onboard LEDs high. |
harryrance | 6:dca8b5e2ebe5 | 170 | * Number of LEDs set high corresponds to how many lives the user has left. |
harryrance | 6:dca8b5e2ebe5 | 171 | */ |
harryrance | 3:43970d8d642e | 172 | void set_lives_leds(Gamepad &pad); |
harryrance | 0:c9bf674fe0c7 | 173 | |
harryrance | 0:c9bf674fe0c7 | 174 | int _us_x_origin; |
harryrance | 0:c9bf674fe0c7 | 175 | int _aa_x_origin; |
harryrance | 0:c9bf674fe0c7 | 176 | int _x; |
harryrance | 0:c9bf674fe0c7 | 177 | int _xa; |
harryrance | 0:c9bf674fe0c7 | 178 | int _speed; |
harryrance | 2:50feb42b982c | 179 | int _set_bullet_x; |
harryrance | 2:50feb42b982c | 180 | int _boss_check; |
harryrance | 2:50feb42b982c | 181 | int _end_game_const; |
harryrance | 3:43970d8d642e | 182 | int _ship_number; |
harryrance | 0:c9bf674fe0c7 | 183 | |
harryrance | 2:50feb42b982c | 184 | Boss _boss; |
harryrance | 1:95d7dd44bb0d | 185 | Bullet _bullet; |
harryrance | 0:c9bf674fe0c7 | 186 | AliensArray _aliens; |
harryrance | 0:c9bf674fe0c7 | 187 | UserShip _ship; |
harryrance | 3:43970d8d642e | 188 | Menu _menu; |
harryrance | 0:c9bf674fe0c7 | 189 | |
harryrance | 0:c9bf674fe0c7 | 190 | Direction _d; |
harryrance | 0:c9bf674fe0c7 | 191 | float _mag; |
harryrance | 0:c9bf674fe0c7 | 192 | }; |
harryrance | 0:c9bf674fe0c7 | 193 | |
harryrance | 0:c9bf674fe0c7 | 194 | #endif |
harryrance | 0:c9bf674fe0c7 | 195 |