ELEC2645 (2015/16) / Mbed 2 deprecated L2_SpaceInvaders

Dependencies:   Joystick N5110 SDFileSystem mbed

Committer:
avi23
Date:
Mon May 02 16:28:07 2016 +0000
Revision:
7:babc367a3333
Child:
8:b2faec20ed8f
Before main.cpp doxygening

Who changed what in which revision?

UserRevisionLine numberNew contents of line
avi23 7:babc367a3333 1 /**
avi23 7:babc367a3333 2 @file main.h
avi23 7:babc367a3333 3 @brief Header file containing function prototypes, defines and global variables
avi23 7:babc367a3333 4 @author Avinash Patel
avi23 7:babc367a3333 5 @date April 2016
avi23 7:babc367a3333 6 */
avi23 7:babc367a3333 7
avi23 7:babc367a3333 8 #ifndef MAIN_H
avi23 7:babc367a3333 9 #define MAIN_H
avi23 7:babc367a3333 10
avi23 7:babc367a3333 11 #include "mbed.h"
avi23 7:babc367a3333 12 #include "N5110.h"
avi23 7:babc367a3333 13 #include "Joystick.h"
avi23 7:babc367a3333 14
avi23 7:babc367a3333 15 //Direction invaders are travelling
avi23 7:babc367a3333 16 #define LEFT 0
avi23 7:babc367a3333 17 #define RIGHT 1
avi23 7:babc367a3333 18
avi23 7:babc367a3333 19 /**
avi23 7:babc367a3333 20 @namespace r_led
avi23 7:babc367a3333 21 @brief K64F Red LED
avi23 7:babc367a3333 22 */
avi23 7:babc367a3333 23 DigitalOut r_led(LED_RED);
avi23 7:babc367a3333 24 /**
avi23 7:babc367a3333 25 @namespace g_led
avi23 7:babc367a3333 26 @brief K64F Green LED
avi23 7:babc367a3333 27 */
avi23 7:babc367a3333 28 DigitalOut g_led(LED_GREEN);
avi23 7:babc367a3333 29 /**
avi23 7:babc367a3333 30 @namespace b_led
avi23 7:babc367a3333 31 @brief K64F Blue LED
avi23 7:babc367a3333 32 */
avi23 7:babc367a3333 33 DigitalOut b_led(LED_BLUE);
avi23 7:babc367a3333 34
avi23 7:babc367a3333 35 /**
avi23 7:babc367a3333 36 @namespace sw2
avi23 7:babc367a3333 37 @brief Connects SW2 on the K64F as an Interrupt
avi23 7:babc367a3333 38 */
avi23 7:babc367a3333 39 InterruptIn sw2(SW2);
avi23 7:babc367a3333 40 /**
avi23 7:babc367a3333 41 @namespace sw3
avi23 7:babc367a3333 42 @brief Connects SW3 on the K64F as an Interrupt
avi23 7:babc367a3333 43 */
avi23 7:babc367a3333 44 InterruptIn sw3(SW3);
avi23 7:babc367a3333 45
avi23 7:babc367a3333 46 /**
avi23 7:babc367a3333 47 @namespace pc
avi23 7:babc367a3333 48 @brief Serial output to USB for debugging
avi23 7:babc367a3333 49 */
avi23 7:babc367a3333 50 Serial pc(USBTX,USBRX);
avi23 7:babc367a3333 51
avi23 7:babc367a3333 52 /**
avi23 7:babc367a3333 53 @namespace Joystick
avi23 7:babc367a3333 54 @brief Creates the joystick object
avi23 7:babc367a3333 55 @brief X potentiometer, Y potentiometer, joystick button
avi23 7:babc367a3333 56 */
avi23 7:babc367a3333 57 Joystick joystick(PTB3, PTB2, PTB11);
avi23 7:babc367a3333 58
avi23 7:babc367a3333 59 /**
avi23 7:babc367a3333 60 @namespace shoot_button
avi23 7:babc367a3333 61 @brief Connects the shoot button as an Interrupt
avi23 7:babc367a3333 62 */
avi23 7:babc367a3333 63 InterruptIn shoot_button(PTB18);
avi23 7:babc367a3333 64
avi23 7:babc367a3333 65 /**
avi23 7:babc367a3333 66 @namespace lcd
avi23 7:babc367a3333 67 @brief Interfaces the N5110 Screen with the mbed
avi23 7:babc367a3333 68 <<library /users/eencae/code/N5110/>>
avi23 7:babc367a3333 69 */
avi23 7:babc367a3333 70 // VCC, SCE, RST, D/C, MOSI, SCLK, LED
avi23 7:babc367a3333 71 N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
avi23 7:babc367a3333 72
avi23 7:babc367a3333 73 /**
avi23 7:babc367a3333 74 @namespace update_screen
avi23 7:babc367a3333 75 @brief Ticker object which will be used to update the screen every 0.05s
avi23 7:babc367a3333 76 */
avi23 7:babc367a3333 77 Ticker update_screen;
avi23 7:babc367a3333 78 /**
avi23 7:babc367a3333 79 @namespace move_joystick
avi23 7:babc367a3333 80 @brief Ticker object which will query the status of the joystick
avi23 7:babc367a3333 81 @brief Fires every 0.05s
avi23 7:babc367a3333 82 */
avi23 7:babc367a3333 83 Ticker move_joystick;
avi23 7:babc367a3333 84 /**
avi23 7:babc367a3333 85 @namespace move_cannon_missile
avi23 7:babc367a3333 86 @brief Ticker object which will move the cannon's missile up the screen
avi23 7:babc367a3333 87 @brief Is only attached when a missile is on the screen
avi23 7:babc367a3333 88 @brief Fires every 0.05s
avi23 7:babc367a3333 89 */
avi23 7:babc367a3333 90 Ticker move_cannon_missile;
avi23 7:babc367a3333 91 /**
avi23 7:babc367a3333 92 @namespace move_enemies
avi23 7:babc367a3333 93 @brief Ticker object which moves the enemies across and down the screen
avi23 7:babc367a3333 94 @brief Inital firing speed is 1s - Decreses as no of invaders alive decreses up to 0.1s
avi23 7:babc367a3333 95 */
avi23 7:babc367a3333 96 Ticker move_enemies;
avi23 7:babc367a3333 97 /**
avi23 7:babc367a3333 98 @namespace move_invader_normal_missile
avi23 7:babc367a3333 99 @brief Array of tickers which move the invaders missiles down the screen
avi23 7:babc367a3333 100 @brief 1 Ticker per missile
avi23 7:babc367a3333 101 @brief Only attached when the ticker's missile is visible
avi23 7:babc367a3333 102 @brief Fires every 0.1s
avi23 7:babc367a3333 103 */
avi23 7:babc367a3333 104 Ticker move_invader_normal_missile[2];
avi23 7:babc367a3333 105
avi23 7:babc367a3333 106
avi23 7:babc367a3333 107 /**
avi23 7:babc367a3333 108 @namespace joystick_cursor_regulator
avi23 7:babc367a3333 109 @brief Timeout object which is used to stop the menu cursor jumping
avi23 7:babc367a3333 110 @brief Fires 0.1s after being attached
avi23 7:babc367a3333 111 */
avi23 7:babc367a3333 112 Timeout joystick_cursor_regulator; //Stops the cursor from jumping
avi23 7:babc367a3333 113 /**
avi23 7:babc367a3333 114 @namespace shoot_button_debounce
avi23 7:babc367a3333 115 @brief Timeout object which is used to reduce noisy input from the shoot button
avi23 7:babc367a3333 116 @brief Fires 0.125s after being attached
avi23 7:babc367a3333 117 */
avi23 7:babc367a3333 118 Timeout shoot_button_debounce; //Stops the cannon from firing when transitioning from a menu to the game
avi23 7:babc367a3333 119
avi23 7:babc367a3333 120 int screen_buffer[84][48]; /*!< Screen Buffer. Each object on the screen has a different integer assigned to it */
avi23 7:babc367a3333 121 const int empty_pixel = 0 ; /*!< Empty Pixels */
avi23 7:babc367a3333 122 const int cannon_pixel = 1; /*!< Pixel number for cannon */
avi23 7:babc367a3333 123 const int first_barrier_pixel = 2; /*!< Pixel number of barrier no 1 */
avi23 7:babc367a3333 124 const int first_large_invader_pixel = 5; /*!< Pixel number of large invader no 1 */
avi23 7:babc367a3333 125 const int first_medium_invader_pixel = 10; /*!< Pixel number of medium invader no 1 */
avi23 7:babc367a3333 126 const int first_small_invader_pixel = 15; /*!< Pixel number of large invader no 1 */
avi23 7:babc367a3333 127 const int ufo_pixel = 20; /*!< Pixel number for UFO */
avi23 7:babc367a3333 128 const int cannon_missile_pixel = 21; /*!< Pixel number for the cannon's missile */
avi23 7:babc367a3333 129 const int invader_normal_missile_pixel = 22; /*!< Pixel number for the invader's normal missile */
avi23 7:babc367a3333 130
avi23 7:babc367a3333 131 //Booleans
avi23 7:babc367a3333 132 //Invader related bools
avi23 7:babc367a3333 133 bool invaders_in_state2 = true; /*!< Viewing state the invaders are in */
avi23 7:babc367a3333 134 bool invader_direction = RIGHT; /*!< Direction the invaders are travalling */
avi23 7:babc367a3333 135 //Cannon missile bool
avi23 7:babc367a3333 136 bool cannon_missile_on_screen = false; /*!< Stores whether the cannon has a missile on screen */
avi23 7:babc367a3333 137
avi23 7:babc367a3333 138 //Integers
avi23 7:babc367a3333 139 int score = 0; /*!< Current score*/
avi23 7:babc367a3333 140 int fsm_state = 0; /*!< Menu FSM state */
avi23 7:babc367a3333 141 int cursor_y_pos = 0; /*!< Cursor position */
avi23 7:babc367a3333 142 //Cannon related integers
avi23 7:babc367a3333 143 int number_of_lives = 3; /*!< Number of lives remaining */
avi23 7:babc367a3333 144 int cannon_xpos = 24; /*!< X position of the cannon */
avi23 7:babc367a3333 145 const int cannon_ypos = 43; /*!< Y position of the cannon */
avi23 7:babc367a3333 146 //Cannon missile related integers
avi23 7:babc367a3333 147 int cannon_missile_x_pos = 28; /*!< X position of the cannon's missile */
avi23 7:babc367a3333 148 int cannon_missile_y_pos = 40; /*!< Y position of the cannon's missile */
avi23 7:babc367a3333 149 //Invader related integers
avi23 7:babc367a3333 150 int no_of_alive_invaders = 15; /*!< Number of alive invaders */
avi23 7:babc367a3333 151 //Invader missile related integers
avi23 7:babc367a3333 152 int invader_strong_missile_x_pos;
avi23 7:babc367a3333 153 int invader_strong_missile_y_pos;
avi23 7:babc367a3333 154
avi23 7:babc367a3333 155 //Floats
avi23 7:babc367a3333 156 float ticker_period = 1; /*!< Time period the move enemies ticker refreshes */
avi23 7:babc367a3333 157
avi23 7:babc367a3333 158 //Enums
avi23 7:babc367a3333 159 /*! Enum containing invader status */
avi23 7:babc367a3333 160 enum Status {
avi23 7:babc367a3333 161 dead, /*!< Invader is dead - no longer on screen */
avi23 7:babc367a3333 162 dying, /*!< Invader is dying - has been hit with cannon missile but still on the screen */
avi23 7:babc367a3333 163 alive /*!< Invader is alive */
avi23 7:babc367a3333 164 };
avi23 7:babc367a3333 165 /*! Enum containing invader type */
avi23 7:babc367a3333 166 enum Invader {
avi23 7:babc367a3333 167 small, /*!< Small invader */
avi23 7:babc367a3333 168 medium, /*!< Medium invader */
avi23 7:babc367a3333 169 large, /*!< Large invader */
avi23 7:babc367a3333 170 none /*!< No invader */
avi23 7:babc367a3333 171 };
avi23 7:babc367a3333 172 /*! Enum containing game status */
avi23 7:babc367a3333 173 enum GameState {
avi23 7:babc367a3333 174 menu, /*!< Menu state */
avi23 7:babc367a3333 175 game, /*!< Game state */
avi23 7:babc367a3333 176 paused, /*!< Pause game state */
avi23 7:babc367a3333 177 save, /*!< Save game state */
avi23 7:babc367a3333 178 load, /*!< Load game state */
avi23 7:babc367a3333 179 scores, /*!< High scores state */
avi23 7:babc367a3333 180 settings /*!< Setting screen state */
avi23 7:babc367a3333 181 };
avi23 7:babc367a3333 182 GameState game_state = menu; /*!< Holds what state the game is currently in */
avi23 7:babc367a3333 183
avi23 7:babc367a3333 184 //Bit-maps
avi23 7:babc367a3333 185 /*! Bitmap of cannon */
avi23 7:babc367a3333 186 const bool cannon_bitmap[5][9] = {
avi23 7:babc367a3333 187 {0, 0, 0, 0, 1, 0, 0, 0, 0},
avi23 7:babc367a3333 188 {0, 0, 0, 1, 1, 1, 0, 0, 0},
avi23 7:babc367a3333 189 {0, 1, 1, 1, 1, 1, 1, 1, 0},
avi23 7:babc367a3333 190 {1, 1, 1, 1, 1, 1, 1, 1, 1},
avi23 7:babc367a3333 191 {1, 1, 1, 1, 1, 1, 1, 1, 1}
avi23 7:babc367a3333 192 };
avi23 7:babc367a3333 193 //Bitmaps for small invaders
avi23 7:babc367a3333 194 /*! Bitmap of 1st state small invader */
avi23 7:babc367a3333 195 const bool small_invader_bitmap_1[6][8] = { //First state
avi23 7:babc367a3333 196 {0, 0, 0, 1, 1, 0, 0, 0},
avi23 7:babc367a3333 197 {0, 1, 1, 1, 1, 1, 1, 0},
avi23 7:babc367a3333 198 {1, 1, 0, 1, 1, 0, 1, 1},
avi23 7:babc367a3333 199 {1, 1, 1, 1, 1, 1, 1, 1},
avi23 7:babc367a3333 200 {0, 1, 0, 1, 1, 0, 1, 0},
avi23 7:babc367a3333 201 {1, 0, 1, 0, 0, 1, 0, 1}
avi23 7:babc367a3333 202 };
avi23 7:babc367a3333 203 /*! Bitmap of 2nd state small invader */
avi23 7:babc367a3333 204 const bool small_invader_bitmap_2[6][8] = { //Second state
avi23 7:babc367a3333 205 {0, 0, 0, 1, 1, 0, 0, 0},
avi23 7:babc367a3333 206 {0, 1, 1, 1, 1, 1, 1, 0},
avi23 7:babc367a3333 207 {1, 1, 0, 1, 1, 0, 1, 1},
avi23 7:babc367a3333 208 {1, 1, 1, 1, 1, 1, 1, 1},
avi23 7:babc367a3333 209 {1, 0, 0, 0, 0, 0, 0, 1},
avi23 7:babc367a3333 210 {0, 1, 0, 0, 0, 0, 1, 0}
avi23 7:babc367a3333 211 };
avi23 7:babc367a3333 212 //Bitmaps for medium invaders
avi23 7:babc367a3333 213 /*! Bitmap of 1st state medium invader */
avi23 7:babc367a3333 214 const bool medium_invader_bitmap_1[6][10] = { //First state
avi23 7:babc367a3333 215 {1, 0, 0, 1, 0, 0, 1, 0, 0, 1},
avi23 7:babc367a3333 216 {1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
avi23 7:babc367a3333 217 {1, 1, 1, 0, 1, 1, 0, 1, 1, 1},
avi23 7:babc367a3333 218 {0, 1, 1, 1, 1, 1, 1, 1, 1, 0},
avi23 7:babc367a3333 219 {0, 0, 1, 0, 0, 0, 0, 1, 0, 0},
avi23 7:babc367a3333 220 {0, 1, 0, 0, 0, 0, 0, 0, 1, 0}
avi23 7:babc367a3333 221 };
avi23 7:babc367a3333 222 /*! Bitmap of 2nd state medium invader */
avi23 7:babc367a3333 223 const bool medium_invader_bitmap_2[6][10] = { //Second state
avi23 7:babc367a3333 224 {0, 0, 0, 1, 0, 0, 1, 0, 0, 0},
avi23 7:babc367a3333 225 {0, 0, 1, 1, 1, 1, 1, 1, 0, 0},
avi23 7:babc367a3333 226 {0, 1, 1, 0, 1, 1, 0, 1, 1, 0},
avi23 7:babc367a3333 227 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
avi23 7:babc367a3333 228 {1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
avi23 7:babc367a3333 229 {0, 0, 0, 1, 1, 1, 1, 0, 0, 0}
avi23 7:babc367a3333 230 };
avi23 7:babc367a3333 231 //Bitmaps for large invaders
avi23 7:babc367a3333 232 /*! Bitmap of 1st state large invader */
avi23 7:babc367a3333 233 const bool large_invader_bitmap_1[6][12] = { //First state
avi23 7:babc367a3333 234 {0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0},
avi23 7:babc367a3333 235 {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
avi23 7:babc367a3333 236 {1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1},
avi23 7:babc367a3333 237 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
avi23 7:babc367a3333 238 {0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0},
avi23 7:babc367a3333 239 {0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0}
avi23 7:babc367a3333 240 };
avi23 7:babc367a3333 241 /*! Bitmap of 2nd state large invader */
avi23 7:babc367a3333 242 const bool large_invader_bitmap_2[6][12] = { //Second state
avi23 7:babc367a3333 243 {0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0},
avi23 7:babc367a3333 244 {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
avi23 7:babc367a3333 245 {1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1},
avi23 7:babc367a3333 246 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
avi23 7:babc367a3333 247 {0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0},
avi23 7:babc367a3333 248 {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}
avi23 7:babc367a3333 249 };
avi23 7:babc367a3333 250 /*! Bitmap of invaders normal missile */
avi23 7:babc367a3333 251 const bool invader_normal_missile_bitmap[4][3] = {
avi23 7:babc367a3333 252 {0, 1, 0},
avi23 7:babc367a3333 253 {1, 1, 1},
avi23 7:babc367a3333 254 {0, 1, 0},
avi23 7:babc367a3333 255 {0, 1, 0}
avi23 7:babc367a3333 256 };
avi23 7:babc367a3333 257 /*! Bitmap of 1st state invader's strong missile */
avi23 7:babc367a3333 258 const bool invader_strong_missile_bitmap_1[4][3] = { //First state
avi23 7:babc367a3333 259 {0, 0, 1},
avi23 7:babc367a3333 260 {0, 1, 0},
avi23 7:babc367a3333 261 {1, 0, 0},
avi23 7:babc367a3333 262 {0, 1, 0}
avi23 7:babc367a3333 263 };
avi23 7:babc367a3333 264 /*! Bitmap of 2nd state invader's strong missile */
avi23 7:babc367a3333 265 const bool invader_strong_missile_bitmap_2[4][3] = { //Second state
avi23 7:babc367a3333 266 {0, 1, 0},
avi23 7:babc367a3333 267 {1, 0, 0},
avi23 7:babc367a3333 268 {0, 1, 0},
avi23 7:babc367a3333 269 {0, 1, 1}
avi23 7:babc367a3333 270 };
avi23 7:babc367a3333 271 /*! Bitmap of barriers */
avi23 7:babc367a3333 272 const bool barrier_bitmap[8][14] = {
avi23 7:babc367a3333 273 {0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0},
avi23 7:babc367a3333 274 {0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0},
avi23 7:babc367a3333 275 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
avi23 7:babc367a3333 276 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
avi23 7:babc367a3333 277 {1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1},
avi23 7:babc367a3333 278 {1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1},
avi23 7:babc367a3333 279 {1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1},
avi23 7:babc367a3333 280 {1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1}
avi23 7:babc367a3333 281 };
avi23 7:babc367a3333 282 /*! Damage bitmap applied when cannon missile hits barrier */
avi23 7:babc367a3333 283 const bool barrier_cannon_missile_damage_bitmap[3][3] = {
avi23 7:babc367a3333 284 {0, 1, 0},
avi23 7:babc367a3333 285 {1, 0, 0},
avi23 7:babc367a3333 286 {1, 0, 1}
avi23 7:babc367a3333 287 };
avi23 7:babc367a3333 288 /*! Damage bitmap applied when normal invader missile hits barrier */
avi23 7:babc367a3333 289 const bool barrier_invader_normal_missile_damage_bitmap[2][3] = {
avi23 7:babc367a3333 290 {1, 0, 1},
avi23 7:babc367a3333 291 {0, 1, 0}
avi23 7:babc367a3333 292 };
avi23 7:babc367a3333 293 /*! UFO bitmap */
avi23 7:babc367a3333 294 const bool ufo_bitmap[5][14] = {
avi23 7:babc367a3333 295 {0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0},
avi23 7:babc367a3333 296 {0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0},
avi23 7:babc367a3333 297 {0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0},
avi23 7:babc367a3333 298 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
avi23 7:babc367a3333 299 {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}
avi23 7:babc367a3333 300 };
avi23 7:babc367a3333 301
avi23 7:babc367a3333 302 /**
avi23 7:babc367a3333 303 @struct Invaders
avi23 7:babc367a3333 304 @brief Holds data for each invader
avi23 7:babc367a3333 305 */
avi23 7:babc367a3333 306 struct Invaders {
avi23 7:babc367a3333 307 int x_pos; /*!< X position of invader */
avi23 7:babc367a3333 308 int y_pos; /*!< Y position of invader */
avi23 7:babc367a3333 309 enum Status status; /*!< Status of invader */
avi23 7:babc367a3333 310 };
avi23 7:babc367a3333 311 Invaders small_invader[5]; /*!< Declares 5 Small Invaders */
avi23 7:babc367a3333 312 Invaders medium_invader[5]; /*!< Declares 5 Medium Invaders */
avi23 7:babc367a3333 313 Invaders large_invader[5]; /*!< Declairs 5 Large Invaders */
avi23 7:babc367a3333 314 /**
avi23 7:babc367a3333 315 @struct InvaderNormalMissiles
avi23 7:babc367a3333 316 @brief Hold data for normal invader missiles
avi23 7:babc367a3333 317 */
avi23 7:babc367a3333 318 struct InvaderNormalMissiles {
avi23 7:babc367a3333 319 int x_pos; /*!< X position of missile */
avi23 7:babc367a3333 320 int y_pos; /*!< Y position of missile */
avi23 7:babc367a3333 321 bool fired; /*!< Whether missile is on the screen */
avi23 7:babc367a3333 322 };
avi23 7:babc367a3333 323 InvaderNormalMissiles invader_normal_missile[2]; /*!< Declares 2 normal invaders */
avi23 7:babc367a3333 324 /**
avi23 7:babc367a3333 325 @struct Barriers
avi23 7:babc367a3333 326 @brief Holds barrier data
avi23 7:babc367a3333 327 */
avi23 7:babc367a3333 328 struct Barriers {
avi23 7:babc367a3333 329 int x_pos; /*!< X position of barrier */
avi23 7:babc367a3333 330 int y_pos; /*!< Y position of barrier */
avi23 7:babc367a3333 331 bool before_bitmap[8][14]; /*!< State of the displayed barrier */
avi23 7:babc367a3333 332 bool after_bitmap[8][14]; /*!< State of barrier to be displayed */
avi23 7:babc367a3333 333 };
avi23 7:babc367a3333 334 Barriers barrier[3]; /*!< Declares 3 barriers */
avi23 7:babc367a3333 335 /**
avi23 7:babc367a3333 336 @struct FSMMenus
avi23 7:babc367a3333 337 @brief Hold the transition data for menus
avi23 7:babc367a3333 338 */
avi23 7:babc367a3333 339 struct FSMMenus {
avi23 7:babc367a3333 340 const GameState output; /*!< Output of the current state */
avi23 7:babc367a3333 341 /**
avi23 7:babc367a3333 342 @var next_state
avi23 7:babc367a3333 343 @brief Stores the next state of the FSM
avi23 7:babc367a3333 344 @brief Index 0 is when joystick is down
avi23 7:babc367a3333 345 @brief Index 1 is when joystick is up
avi23 7:babc367a3333 346 */
avi23 7:babc367a3333 347 const int next_state[2];
avi23 7:babc367a3333 348 };
avi23 7:babc367a3333 349 /*! 4 state FSM for main menu */
avi23 7:babc367a3333 350 FSMMenus fsm_main_menu[4] = {
avi23 7:babc367a3333 351 {game, {1, 3}},
avi23 7:babc367a3333 352 {load, {2, 0}},
avi23 7:babc367a3333 353 {scores, {3, 1}},
avi23 7:babc367a3333 354 {settings, {0, 2}}
avi23 7:babc367a3333 355 };
avi23 7:babc367a3333 356 /*! 3 state FSM for pause menu */
avi23 7:babc367a3333 357 FSMMenus fsm_paused[3] = {
avi23 7:babc367a3333 358 {game, {1, 2}},
avi23 7:babc367a3333 359 {save, {2, 0}},
avi23 7:babc367a3333 360 {menu, {0, 1}}
avi23 7:babc367a3333 361 };
avi23 7:babc367a3333 362
avi23 7:babc367a3333 363 //ISR Flags
avi23 7:babc367a3333 364 /** @name ISR Flags
avi23 7:babc367a3333 365 * Flags set in ISR's
avi23 7:babc367a3333 366 */
avi23 7:babc367a3333 367 ///@{
avi23 7:babc367a3333 368 volatile bool g_update_screen_flag = true; /*!< Update screen flag */
avi23 7:babc367a3333 369 volatile bool g_move_joystick_flag = true; /*!< Move joystick flag */
avi23 7:babc367a3333 370 volatile bool g_move_enemies_flag = true; /*!< Move enemies flag */
avi23 7:babc367a3333 371 volatile bool g_shoot_pressed_flag = false; /*!< Shoot pressed flag */
avi23 7:babc367a3333 372 volatile bool g_move_cannon_missile_flag = false; /*!< Move cannon missile flag */
avi23 7:babc367a3333 373 volatile bool g_move_invader_normal_missile_flag[2] = {false, false}; /*!< Move invader normal missile flags */
avi23 7:babc367a3333 374 volatile bool g_cannon_hit_flag = false; /*!< Cannon hit flag */
avi23 7:babc367a3333 375 volatile bool g_joystick_cursor_regulator_flag = false; /*!< Joystick regulator flag */
avi23 7:babc367a3333 376 volatile bool g_shoot_button_debounce_flag = false; /*!< Shoot button debounce flag */
avi23 7:babc367a3333 377 ///@}
avi23 7:babc367a3333 378
avi23 7:babc367a3333 379 //Function prototypes
avi23 7:babc367a3333 380 //ISR's
avi23 7:babc367a3333 381 /** @name ISR Functions
avi23 7:babc367a3333 382 * Functions to set ISR values
avi23 7:babc367a3333 383 */
avi23 7:babc367a3333 384 ///@{
avi23 7:babc367a3333 385 void update_screen_isr(); /*!< Sets the update screen flag when called */
avi23 7:babc367a3333 386 void move_joystick_isr(); /*!< Sets the move joystick flag when called */
avi23 7:babc367a3333 387 void move_enemies_isr(); /*!< Sets the move enemies flag when called */
avi23 7:babc367a3333 388 void shoot_pressed_isr(); /*!< Sets the shoot pressed flag when called */
avi23 7:babc367a3333 389 void move_cannon_missile_isr(); /*!< Sets the move cannon missile flag when called */
avi23 7:babc367a3333 390 void cannon_hit_isr(); /*!< Sets the cannon hit flag when called */
avi23 7:babc367a3333 391 void joystick_cursor_regulator_isr(); /*!< Sets the joystick regulator flag when called */
avi23 7:babc367a3333 392 void shoot_button_debounce_isr(); /*!< Sets the shoot button debounce flag when called */
avi23 7:babc367a3333 393 //Function pointer to move invader normal missile isr
avi23 7:babc367a3333 394 void (*move_invader_normal_missile_isr[2])(); /*!< Function pointer to help attach move invader normal missile isr's */
avi23 7:babc367a3333 395 void move_invader_normal_missile_0_isr(); /*!< Sets the move invader normal missile [0] flag when called */
avi23 7:babc367a3333 396 void move_invader_normal_missile_1_isr(); /*!< Sets the move invader normal missile [1] flag when called */
avi23 7:babc367a3333 397 ///@}
avi23 7:babc367a3333 398 //Other functions
avi23 7:babc367a3333 399 /** Hangs - flashing an LED
avi23 7:babc367a3333 400 */
avi23 7:babc367a3333 401 void error();
avi23 7:babc367a3333 402 /** Sets up serial port to 115200 baud
avi23 7:babc367a3333 403 */
avi23 7:babc367a3333 404 void init_serial();
avi23 7:babc367a3333 405 /** Set-up the on-board LEDs and switches
avi23 7:babc367a3333 406 */
avi23 7:babc367a3333 407 void init_K64F();
avi23 7:babc367a3333 408 /** Configures the shoot button
avi23 7:babc367a3333 409 @brief Sets the shoot button to PullUp mode.
avi23 7:babc367a3333 410 @brief Sets the falling edge of the swich to call the shoot_pressed_isr
avi23 7:babc367a3333 411 */
avi23 7:babc367a3333 412 void init_shoot();
avi23 7:babc367a3333 413 /** Seeds the random number generator with noise from an analog in pin
avi23 7:babc367a3333 414 */
avi23 7:babc367a3333 415 void init_rng();
avi23 7:babc367a3333 416 /** Clears the screen buffer and runs invader and barrier initalisation functions
avi23 7:babc367a3333 417 @brief Sets the flags so invaders show up straight away.
avi23 7:babc367a3333 418 @brief Clears the missile fired flags
avi23 7:babc367a3333 419 */
avi23 7:babc367a3333 420 void InitaliseGame();
avi23 7:babc367a3333 421 /** Contains game logic
avi23 7:babc367a3333 422 @brief Calls screen refresh, cannon movement, barrier drawing, invader movement, missile movement, pause and game over transition functions
avi23 7:babc367a3333 423 @brief Only calls functions when ISR flags are set
avi23 7:babc367a3333 424 */
avi23 7:babc367a3333 425 void Game();
avi23 7:babc367a3333 426 /** Loops through the screen buffer
avi23 7:babc367a3333 427 @brief If pixels are set in the screen buffer set them on the lcd
avi23 7:babc367a3333 428 @brief Otherwise clear them
avi23 7:babc367a3333 429 */
avi23 7:babc367a3333 430 void UpdateScreen();
avi23 7:babc367a3333 431 /** Clears the cannon from the buffer
avi23 7:babc367a3333 432 @brief Increments or decrements the x coordinate of the cannon depending on joystick position - Capped between 0 and 75
avi23 7:babc367a3333 433 @brief Redraws the cannon onto the buffer
avi23 7:babc367a3333 434 */
avi23 7:babc367a3333 435 void MoveCannon();
avi23 7:babc367a3333 436 /** @fn Sets the position and status of the small invaders
avi23 7:babc367a3333 437 */
avi23 7:babc367a3333 438 void InitSmallInvaders();
avi23 7:babc367a3333 439 /** @fn Cycles through small invaders
avi23 7:babc367a3333 440 @brief If their status is not equal to dead clear them from the buffer
avi23 7:babc367a3333 441 */
avi23 7:babc367a3333 442 void ClearSmallInvaders();
avi23 7:babc367a3333 443 /** Cycles through the small invader current bitmap and sets the pixels it occupys in the buffer to empty
avi23 7:babc367a3333 444 @param invader_no - Small invader object number between 0-4
avi23 7:babc367a3333 445 */
avi23 7:babc367a3333 446 void ClearSingleSmallInvader(int invader_no);
avi23 7:babc367a3333 447 /** Cycles through small invaders
avi23 7:babc367a3333 448 @brief If their status is equal to alive set them in the buffer
avi23 7:babc367a3333 449 */
avi23 7:babc367a3333 450 void DrawSmallInvaders();
avi23 7:babc367a3333 451 /** Cycles through the small invader current bitmap and sets the pixels it occupys in the buffer to first_small_invader_pixel + invader_no
avi23 7:babc367a3333 452 @param invader_no - Small invader object number between 0-4
avi23 7:babc367a3333 453 */
avi23 7:babc367a3333 454 void DrawSingleSmallInvader(int invader_no);
avi23 7:babc367a3333 455 /** Sets the position and status of the medium invaders
avi23 7:babc367a3333 456 */
avi23 7:babc367a3333 457 void InitMediumInvaders();
avi23 7:babc367a3333 458 /** Cycles through medium invaders
avi23 7:babc367a3333 459 @brief If their status is not equal to dead clear them from the buffer
avi23 7:babc367a3333 460 */
avi23 7:babc367a3333 461 void ClearMediumInvaders();
avi23 7:babc367a3333 462 /** Cycles through the medium invader current bitmap and sets the pixels it occupys in the buffer to empty
avi23 7:babc367a3333 463 @param invader_no - Medium invader object number between 0-4
avi23 7:babc367a3333 464 */
avi23 7:babc367a3333 465 void ClearSingleMediumInvader(int invader_no);
avi23 7:babc367a3333 466 /** Cycles through medium invaders
avi23 7:babc367a3333 467 @brief If their status is equal to alive set them in the buffer
avi23 7:babc367a3333 468 */
avi23 7:babc367a3333 469 void DrawMediumInvaders();
avi23 7:babc367a3333 470 /** Cycles through the medium invader current bitmap and sets the pixels it occupys in the buffer to first_medium_invader_pixel + invader_no
avi23 7:babc367a3333 471 @param invader_no - Medium invader object number between 0-4
avi23 7:babc367a3333 472 */
avi23 7:babc367a3333 473 void DrawSingleMediumInvader(int invader_no);
avi23 7:babc367a3333 474 /** Sets the position and status of the large invaders
avi23 7:babc367a3333 475 */
avi23 7:babc367a3333 476 void InitLargeInvaders();
avi23 7:babc367a3333 477 /** Cycles through large invaders
avi23 7:babc367a3333 478 @brief If their status is not equal to dead clear them from the buffer
avi23 7:babc367a3333 479 */
avi23 7:babc367a3333 480 void ClearLargeInvaders();
avi23 7:babc367a3333 481 /** Cycles through the large invader current bitmap and sets the pixels it occupys in the buffer to empty
avi23 7:babc367a3333 482 @param invader_no - Large invader object number between 0-4
avi23 7:babc367a3333 483 */
avi23 7:babc367a3333 484 void ClearSingleLargeInvader(int invader_no);
avi23 7:babc367a3333 485 /** Cycles through large invaders
avi23 7:babc367a3333 486 @brief If their status is equal to alive set them in the buffer
avi23 7:babc367a3333 487 */
avi23 7:babc367a3333 488 void DrawLargeInvaders();
avi23 7:babc367a3333 489 /** Cycles through the large invader current bitmap and sets the pixels it occupys in the buffer to first_large_invader_pixel + invader_no
avi23 7:babc367a3333 490 @param invader_no - Large invader object number between 0-4
avi23 7:babc367a3333 491 */
avi23 7:babc367a3333 492 void DrawSingleLargeInvader(int invader_no);
avi23 7:babc367a3333 493 /** Sets the position and loads the bitmap into the barrier objects
avi23 7:babc367a3333 494 */
avi23 7:babc367a3333 495 void InitBarriers();
avi23 7:babc367a3333 496 /** Clears the barrier with the before bitmap and redraw it from the after bitmap which has the damage applied
avi23 7:babc367a3333 497 @brief Copies the after bitmap to the before bitmap
avi23 7:babc367a3333 498 */
avi23 7:babc367a3333 499 void DrawBarriers();
avi23 7:babc367a3333 500 /** If the invader direction is right, calculate the right limit and move the invaders right
avi23 7:babc367a3333 501 @brief Otherwise calculate the left limit and move the invaders left
avi23 7:babc367a3333 502 */
avi23 7:babc367a3333 503 void MoveInvaderXPositions();
avi23 7:babc367a3333 504 /** Checks the status off the invaders per column, starting from the left
avi23 7:babc367a3333 505 @brief If they're alive return the row number
avi23 7:babc367a3333 506 @returns The left-most column invaders are alive
avi23 7:babc367a3333 507 */
avi23 7:babc367a3333 508 int CalculateInvaderLeftLimit();
avi23 7:babc367a3333 509 /** Checks the status off the invaders per column, starting from the right
avi23 7:babc367a3333 510 @brief If they're alive return the row number
avi23 7:babc367a3333 511 @returns The right-most column invaders are alive
avi23 7:babc367a3333 512 */
avi23 7:babc367a3333 513 int CalculateInvaderRightLimit();
avi23 7:babc367a3333 514 /** If there is still room for the invaders to move, move them 2 to the left
avi23 7:babc367a3333 515 @brief Otherwise move them down
avi23 7:babc367a3333 516 @param limit - Large invader object to check for space against
avi23 7:babc367a3333 517 */
avi23 7:babc367a3333 518 void MoveInvadersLeft(int limit);
avi23 7:babc367a3333 519 /** If there is still room for the invaders to move, move them 2 to the right
avi23 7:babc367a3333 520 @brief Otherwise move them down
avi23 7:babc367a3333 521 @param limit - Large invader object to check for space against
avi23 7:babc367a3333 522 */
avi23 7:babc367a3333 523 void MoveInvadersRight(int limit);
avi23 7:babc367a3333 524 /** Calculates the lowest alive invader
avi23 7:babc367a3333 525 @brief If there is still space for the invader to move, move them 3 pixels down
avi23 7:babc367a3333 526 @brief Otherwise end the game
avi23 7:babc367a3333 527 */
avi23 7:babc367a3333 528 void MoveInvaderYPositions(bool new_direction);
avi23 7:babc367a3333 529 /** Checks each row of invaders to find the lowest row which has an alive invader
avi23 7:babc367a3333 530 @returns The lowest row which has an alive invader
avi23 7:babc367a3333 531 */
avi23 7:babc367a3333 532 Invader CalculateInvaderYLimit();
avi23 7:babc367a3333 533 /** Queries the invaders on their status in the specified column
avi23 7:babc367a3333 534 @param column - The column to query
avi23 7:babc367a3333 535 @returns The lowest invader alive in that column
avi23 7:babc367a3333 536 */
avi23 7:babc367a3333 537 Invader LowestInvaderInColumn(int column);
avi23 7:babc367a3333 538 /** Sets the cannon fired flag to true
avi23 7:babc367a3333 539 @brief Configures the start coordinates
avi23 7:babc367a3333 540 @brief Attaches the move cannon missile ticker to fire every 0.05s
avi23 7:babc367a3333 541 */
avi23 7:babc367a3333 542 void FireCannonMissile();
avi23 7:babc367a3333 543 /** If the missile is not beyond the bounds of the screen, clear it, increment the shot and run collision detection
avi23 7:babc367a3333 544 @brief Otherwise clear the last 3 pixels of the missile, mark it as no longer on the screen and detach the ticker
avi23 7:babc367a3333 545 */
avi23 7:babc367a3333 546 void MoveCannonMissile();
avi23 7:babc367a3333 547 /** If the missile hits an invader it will clear it and increment the score or if it hits a barrier apply the appropiate damage to it
avi23 7:babc367a3333 548 @brief Otherwise push the missile to the screen buffer
avi23 7:babc367a3333 549 */
avi23 7:babc367a3333 550 void CollisionDetectionCannonMissile();
avi23 7:babc367a3333 551 /** Finds the invader number the missile hits, sets the hit invader to dying, decrements the no_of_alive_invaders
avi23 7:babc367a3333 552 @brief Stops the missile from travalling up the screen
avi23 7:babc367a3333 553 @param first_pixel - Start no of invader group hit
avi23 7:babc367a3333 554 @param row - What part of the missile hit the invader
avi23 7:babc367a3333 555 @param invader[5] - Struct of the invader group which was hit
avi23 7:babc367a3333 556 @returns The invader number which was hit
avi23 7:babc367a3333 557 */
avi23 7:babc367a3333 558 int CannonMissileHitInvader(int first_pixel, int row, struct Invaders (&invader)[5]);
avi23 7:babc367a3333 559 /** Calculates where to start drawing the damage bitmap over the barrier bitmap and performs the operation
avi23 7:babc367a3333 560 @param row - Which part of the missile hit the barrier
avi23 7:babc367a3333 561 */
avi23 7:babc367a3333 562 void CannonMissileHitBarrier(int row);
avi23 7:babc367a3333 563 /** Random chance of 1/18, decrementing to a 1/3 depending on no of alive invaders, to fire a missile
avi23 7:babc367a3333 564 @brief Only fires if they're are less than 2 on the screen
avi23 7:babc367a3333 565 */
avi23 7:babc367a3333 566 void AttemptToFireInvaderNormalMissiles();
avi23 7:babc367a3333 567 /** Finds the centre point of the chosen invader and fires the missile
avi23 7:babc367a3333 568 @brief Attaches the ticker to move the missile every 0.05s
avi23 7:babc367a3333 569 @param missile_no - Which missile will be fired
avi23 7:babc367a3333 570 @param source - Type of invader which missile will be fired from (Used to calculate start position)
avi23 7:babc367a3333 571 @param invader - Invader which missile will orginiate from
avi23 7:babc367a3333 572 */
avi23 7:babc367a3333 573 void FireNormalInvaderMissile(int missile_no, Invader source, const struct Invaders (&invader));
avi23 7:babc367a3333 574 /** Clears the missile from the screen buffer
avi23 7:babc367a3333 575 @brief Increments the missile up the screen and runs collision detection if within screen bounds, otherwise set fired flag as false and detach the ticker
avi23 7:babc367a3333 576 @param missile_no - Which missile is being moved
avi23 7:babc367a3333 577 */
avi23 7:babc367a3333 578 void MoveInvaderNormalMissile(int missile_no);
avi23 7:babc367a3333 579 /** Checks the bottom centre point for a collision. If it hasn't push the missile to the screen buffer
avi23 7:babc367a3333 580 @param missile_no - Which missile is being checked
avi23 7:babc367a3333 581 */
avi23 7:babc367a3333 582 void CollisionDetectionInvaderNormalMissile(int missile_no);
avi23 7:babc367a3333 583 /** Decrements the number of lives and pauses the game for 2 seconds
avi23 7:babc367a3333 584 */
avi23 7:babc367a3333 585 void InvaderNormalMissileHitCannon();
avi23 7:babc367a3333 586 /** Calculates where to start drawing the damage bitmap over the barrier bitmap and performs the operation
avi23 7:babc367a3333 587 @param missile - Missile which hit the barrier
avi23 7:babc367a3333 588 */
avi23 7:babc367a3333 589 void InvaderNormalMissileHitBarrier(const struct InvaderNormalMissiles (&missile));
avi23 7:babc367a3333 590 /** Detaches game related tickers
avi23 7:babc367a3333 591 */
avi23 7:babc367a3333 592 void DetachTickers();
avi23 7:babc367a3333 593 /** Attaches game related tickers
avi23 7:babc367a3333 594 */
avi23 7:babc367a3333 595 void AttachTickers();
avi23 7:babc367a3333 596 /** Prints the pause screen and draws the selection cursor
avi23 7:babc367a3333 597 */
avi23 7:babc367a3333 598 void PauseScreen();
avi23 7:babc367a3333 599 /** Prints the text on the pause screen
avi23 7:babc367a3333 600 */
avi23 7:babc367a3333 601 void PrintPauseScreen();
avi23 7:babc367a3333 602 /** Clears the cursor and sets the next state of the supplied fsm
avi23 7:babc367a3333 603 @brief Next state depends on joystick value
avi23 7:babc367a3333 604 @param fsm - Finite State Machine for the current menu
avi23 7:babc367a3333 605 */
avi23 7:babc367a3333 606 void MoveCursor(const struct FSMMenus *fsm);
avi23 7:babc367a3333 607
avi23 7:babc367a3333 608 //void InitUFO();
avi23 7:babc367a3333 609 void DrawUFO();
avi23 7:babc367a3333 610
avi23 7:babc367a3333 611 #endif