Game For ECE 2035

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
nasiromar
Date:
Tue Nov 30 00:48:48 2021 +0000
Revision:
9:cbb9cfb1f6c5
Parent:
8:fcc333a8f9e1
Child:
10:e18685911e84
Sound Working;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rconnorlawson 0:35660d7952f7 1 // Project includes
rconnorlawson 0:35660d7952f7 2 #include "globals.h"
rconnorlawson 0:35660d7952f7 3 #include "hardware.h"
rconnorlawson 0:35660d7952f7 4 #include "map.h"
rconnorlawson 0:35660d7952f7 5 #include "graphics.h"
rconnorlawson 0:35660d7952f7 6 #include "speech.h"
nasiromar 6:c9695079521d 7 #include "spells.h"
nasiromar 6:c9695079521d 8 #include "actions.h"
nasiromar 9:cbb9cfb1f6c5 9 #include "SDFileSystem.h"
rconnorlawson 0:35660d7952f7 10
rconnorlawson 0:35660d7952f7 11 // Functions in this file
rconnorlawson 0:35660d7952f7 12 int get_action (GameInputs inputs);
rconnorlawson 0:35660d7952f7 13 int update_game (int action);
rconnorlawson 0:35660d7952f7 14 void draw_game (int init);
rconnorlawson 0:35660d7952f7 15 void init_main_map ();
rconnorlawson 0:35660d7952f7 16 int main ();
rconnorlawson 0:35660d7952f7 17
nasiromar 9:cbb9cfb1f6c5 18
nasiromar 9:cbb9cfb1f6c5 19 FILE *wave_file;
nasiromar 9:cbb9cfb1f6c5 20
rconnorlawson 0:35660d7952f7 21 /**
rconnorlawson 0:35660d7952f7 22 * The main game state. Must include Player locations and previous locations for
rconnorlawson 0:35660d7952f7 23 * drawing to work properly. Other items can be added as needed.
rconnorlawson 0:35660d7952f7 24 */
rconnorlawson 0:35660d7952f7 25 struct {
rconnorlawson 0:35660d7952f7 26 int x,y; // Current locations
rconnorlawson 0:35660d7952f7 27 int px, py; // Previous locations
rconnorlawson 0:35660d7952f7 28 int has_key;
nasiromar 6:c9695079521d 29 int spell;
nasiromar 6:c9695079521d 30 int god;
nasiromar 8:fcc333a8f9e1 31 int won;
nasiromar 9:cbb9cfb1f6c5 32 int HP,MP;
rashidi1saeed 2:1ca97843a334 33 // You can add other properties for the player here
rconnorlawson 0:35660d7952f7 34 } Player;
rconnorlawson 0:35660d7952f7 35
nasiromar 9:cbb9cfb1f6c5 36
nasiromar 9:cbb9cfb1f6c5 37
rconnorlawson 0:35660d7952f7 38 /**
rconnorlawson 0:35660d7952f7 39 * Given the game inputs, determine what kind of update needs to happen.
rconnorlawson 0:35660d7952f7 40 * Possbile return values are defined below.
rconnorlawson 0:35660d7952f7 41 */
rconnorlawson 0:35660d7952f7 42 #define NO_ACTION 0
rconnorlawson 0:35660d7952f7 43 #define ACTION_BUTTON 1
rconnorlawson 0:35660d7952f7 44 #define MENU_BUTTON 2
rconnorlawson 0:35660d7952f7 45 #define GO_LEFT 3
rconnorlawson 0:35660d7952f7 46 #define GO_RIGHT 4
rconnorlawson 0:35660d7952f7 47 #define GO_UP 5
rconnorlawson 0:35660d7952f7 48 #define GO_DOWN 6
nasiromar 6:c9695079521d 49 #define GOD_MODE 7
nasiromar 6:c9695079521d 50
nasiromar 6:c9695079521d 51 #define TILT .25
rconnorlawson 0:35660d7952f7 52 int get_action(GameInputs inputs)
rconnorlawson 0:35660d7952f7 53 {
nasiromar 6:c9695079521d 54
nasiromar 6:c9695079521d 55 if(inputs.ay > TILT) { //Go in north direction if it is not blocked
nasiromar 6:c9695079521d 56 return GO_UP;
nasiromar 6:c9695079521d 57 }
nasiromar 6:c9695079521d 58 if(inputs.ay < TILT *-1) { //Go in south direction if it is not blocked
nasiromar 6:c9695079521d 59 return GO_DOWN;
nasiromar 6:c9695079521d 60 }
nasiromar 6:c9695079521d 61 if(inputs.ax > TILT) { //Go in north direction if it is not blocked
nasiromar 6:c9695079521d 62 return GO_RIGHT;
nasiromar 6:c9695079521d 63 }
nasiromar 6:c9695079521d 64 if(inputs.ax < TILT *-1) { //Go in north direction if it is not blocked
nasiromar 6:c9695079521d 65 return GO_LEFT;
nasiromar 6:c9695079521d 66 }
nasiromar 6:c9695079521d 67 if(!inputs.b1) {
nasiromar 6:c9695079521d 68 wait(.5);
nasiromar 6:c9695079521d 69 return ACTION_BUTTON;
nasiromar 6:c9695079521d 70 }
nasiromar 6:c9695079521d 71 if(!inputs.b2) {
nasiromar 6:c9695079521d 72 wait(.5);
nasiromar 6:c9695079521d 73 return MENU_BUTTON;
nasiromar 6:c9695079521d 74 }
nasiromar 6:c9695079521d 75 if (!inputs.b3) {
nasiromar 6:c9695079521d 76 wait(.5);
nasiromar 6:c9695079521d 77 return GOD_MODE;
nasiromar 6:c9695079521d 78 } else {
nasiromar 6:c9695079521d 79 return NO_ACTION;
nasiromar 6:c9695079521d 80 }
rconnorlawson 0:35660d7952f7 81 }
rconnorlawson 0:35660d7952f7 82
nasiromar 6:c9695079521d 83
rconnorlawson 0:35660d7952f7 84 /**
rconnorlawson 0:35660d7952f7 85 * Update the game state based on the user action. For example, if the user
rconnorlawson 0:35660d7952f7 86 * requests GO_UP, then this function should determine if that is possible by
rconnorlawson 0:35660d7952f7 87 * consulting the map, and update the Player position accordingly.
nasiromar 6:c9695079521d 88 *
rconnorlawson 0:35660d7952f7 89 * Return values are defined below. FULL_DRAW indicates that for this frame,
rconnorlawson 0:35660d7952f7 90 * draw_game should not optimize drawing and should draw every tile, even if
rconnorlawson 0:35660d7952f7 91 * the player has not moved.
rconnorlawson 0:35660d7952f7 92 */
nasiromar 9:cbb9cfb1f6c5 93 // Game Life and Magic Points
nasiromar 6:c9695079521d 94
nasiromar 6:c9695079521d 95
nasiromar 6:c9695079521d 96
rconnorlawson 0:35660d7952f7 97 #define NO_RESULT 0
rconnorlawson 0:35660d7952f7 98 #define GAME_OVER 1
rconnorlawson 0:35660d7952f7 99 #define FULL_DRAW 2
rconnorlawson 0:35660d7952f7 100 int update_game(int action)
rconnorlawson 0:35660d7952f7 101 {
rconnorlawson 0:35660d7952f7 102 // Save player previous location before updating
rconnorlawson 0:35660d7952f7 103 Player.px = Player.x;
rconnorlawson 0:35660d7952f7 104 Player.py = Player.y;
nasiromar 9:cbb9cfb1f6c5 105
nasiromar 9:cbb9cfb1f6c5 106
nasiromar 6:c9695079521d 107
nasiromar 6:c9695079521d 108
rconnorlawson 0:35660d7952f7 109 // Do different things based on the each action.
rconnorlawson 0:35660d7952f7 110 // You can define functions like "go_up()" that get called for each case.
nasiromar 6:c9695079521d 111
nasiromar 6:c9695079521d 112 // Need to check if Claue has changed as well if whats in front of me is able to be walked into
nasiromar 6:c9695079521d 113 switch(action) {
nasiromar 6:c9695079521d 114 case GO_UP: {
nasiromar 6:c9695079521d 115 MapItem* north = get_north(Player.x,Player.y);
nasiromar 6:c9695079521d 116 if (north->walkable && north == NULL || north->type == (PORTAL || PLANT) && Player.y != 0 || Player.god ) {
nasiromar 6:c9695079521d 117 Player.y--;
nasiromar 9:cbb9cfb1f6c5 118
nasiromar 6:c9695079521d 119 }
nasiromar 6:c9695079521d 120 break;
nasiromar 6:c9695079521d 121 }
nasiromar 6:c9695079521d 122 case GO_RIGHT: {
nasiromar 6:c9695079521d 123 MapItem* east = get_east(Player.x,Player.y);
nasiromar 6:c9695079521d 124 if (east->walkable && east == NULL || east->type == (PORTAL || PLANT) && Player.x != 0 || Player.god ) {
nasiromar 6:c9695079521d 125 Player.x++;
nasiromar 6:c9695079521d 126 }
nasiromar 6:c9695079521d 127 break;
nasiromar 6:c9695079521d 128 }
nasiromar 6:c9695079521d 129 case GO_DOWN: {
nasiromar 6:c9695079521d 130 MapItem* south = get_south(Player.x,Player.y);
nasiromar 6:c9695079521d 131 if (south->walkable && south == NULL || south->type ==(PORTAL || PLANT) && Player.y != 0 || Player.god ) {
nasiromar 6:c9695079521d 132 Player.y++;
nasiromar 6:c9695079521d 133 }
nasiromar 6:c9695079521d 134 break;
nasiromar 6:c9695079521d 135 }
nasiromar 6:c9695079521d 136 case GO_LEFT: {
nasiromar 6:c9695079521d 137 MapItem* west = get_west(Player.x,Player.y);
nasiromar 6:c9695079521d 138 if (west->walkable && west == NULL || west->type == (PORTAL || PLANT) && Player.x != 0 || Player.god ) {
nasiromar 6:c9695079521d 139 Player.x--;
nasiromar 6:c9695079521d 140 }
nasiromar 6:c9695079521d 141 break;
nasiromar 6:c9695079521d 142 }
nasiromar 6:c9695079521d 143 case ACTION_BUTTON: {
nasiromar 6:c9695079521d 144 MapItem* west = get_west(Player.x,Player.y);
nasiromar 6:c9695079521d 145 MapItem* south = get_south(Player.x,Player.y);
nasiromar 6:c9695079521d 146 MapItem* north = get_north(Player.x,Player.y);
nasiromar 6:c9695079521d 147 MapItem* east = get_east(Player.x,Player.y);
nasiromar 6:c9695079521d 148 MapItem* curr = get_here(Player.x,Player.y);
nasiromar 9:cbb9cfb1f6c5 149
nasiromar 9:cbb9cfb1f6c5 150 if((east->type == NPC||west->type == NPC||north->type == NPC||south->type == NPC)&& Player.has_key == 1) {
nasiromar 7:862062ffca62 151 npc_speech2();
nasiromar 9:cbb9cfb1f6c5 152 }
nasiromar 9:cbb9cfb1f6c5 153
nasiromar 9:cbb9cfb1f6c5 154 if(east->type == NPC||west->type == NPC||north->type == NPC||south->type == NPC && Player.has_key == 0 ) {
nasiromar 6:c9695079521d 155 npc_speech1();
nasiromar 9:cbb9cfb1f6c5 156 }
nasiromar 9:cbb9cfb1f6c5 157
nasiromar 9:cbb9cfb1f6c5 158 if(north->type == PORTAL||south->type == PORTAL||east->type == PORTAL||west->type == PORTAL) {
nasiromar 7:862062ffca62 159 init_dung();
nasiromar 7:862062ffca62 160 Player.x = Player.y = 7;
nasiromar 9:cbb9cfb1f6c5 161 }
nasiromar 9:cbb9cfb1f6c5 162
nasiromar 9:cbb9cfb1f6c5 163 if((north->type == DRAGON||south->type == DRAGON||east->type == DRAGON||west->type == DRAGON )&& Player.spell == WATER) {
nasiromar 7:862062ffca62 164 slay_dragon();
nasiromar 7:862062ffca62 165 Player.has_key = 1;
nasiromar 9:cbb9cfb1f6c5 166 }
rconnorlawson 0:35660d7952f7 167
nasiromar 9:cbb9cfb1f6c5 168 if(north->type == PORTAl||south->type == PORTAl||east->type == PORTAl||west->type == PORTAl) {
nasiromar 9:cbb9cfb1f6c5 169 init_main_map();
nasiromar 9:cbb9cfb1f6c5 170 }
nasiromar 6:c9695079521d 171
nasiromar 9:cbb9cfb1f6c5 172 if((north->type == DOOR||south->type == DOOR||east->type == DOOR||west->type == DOOR)&&Player.has_key == 1) {
nasiromar 9:cbb9cfb1f6c5 173 Player.won = door_open();
rconnorlawson 0:35660d7952f7 174 }
rconnorlawson 0:35660d7952f7 175
nasiromar 9:cbb9cfb1f6c5 176 if((north->type == PLANT||south->type == PLANT||east->type == PLANT||west->type == PLANT||curr->type == PLANT)&&
nasiromar 9:cbb9cfb1f6c5 177 (north->data == FRUIT||south->data == FRUIT||east->data == FRUIT||west->data == FRUIT||curr->data == FRUIT)){
nasiromar 9:cbb9cfb1f6c5 178 if(Player.HP < 100) {
nasiromar 9:cbb9cfb1f6c5 179 fruit();
nasiromar 9:cbb9cfb1f6c5 180 Player.HP += 5;
nasiromar 9:cbb9cfb1f6c5 181 north->data = NULL;
nasiromar 9:cbb9cfb1f6c5 182 south->data = NULL;
nasiromar 9:cbb9cfb1f6c5 183 east->data = NULL;
nasiromar 9:cbb9cfb1f6c5 184 west->data = NULL;
nasiromar 9:cbb9cfb1f6c5 185 curr->data = NULL;
nasiromar 9:cbb9cfb1f6c5 186 } else
nasiromar 9:cbb9cfb1f6c5 187 no_fruit();
nasiromar 9:cbb9cfb1f6c5 188 wave_file = fopen("/sd/rzelda-1s.wav","r");
nasiromar 9:cbb9cfb1f6c5 189 waver.play(wave_file);
nasiromar 9:cbb9cfb1f6c5 190 fclose(wave_file);
nasiromar 9:cbb9cfb1f6c5 191
nasiromar 9:cbb9cfb1f6c5 192
nasiromar 9:cbb9cfb1f6c5 193
nasiromar 9:cbb9cfb1f6c5 194
nasiromar 9:cbb9cfb1f6c5 195 }
nasiromar 9:cbb9cfb1f6c5 196 }
nasiromar 9:cbb9cfb1f6c5 197 break;
nasiromar 9:cbb9cfb1f6c5 198 case MENU_BUTTON: {
nasiromar 9:cbb9cfb1f6c5 199 init_spells();
nasiromar 9:cbb9cfb1f6c5 200 Player.spell = spell();
nasiromar 9:cbb9cfb1f6c5 201 }
nasiromar 9:cbb9cfb1f6c5 202 break;
nasiromar 9:cbb9cfb1f6c5 203 case GOD_MODE: {
nasiromar 9:cbb9cfb1f6c5 204
nasiromar 9:cbb9cfb1f6c5 205 Player.god = !Player.god;
nasiromar 9:cbb9cfb1f6c5 206 if (Player.god) {
nasiromar 9:cbb9cfb1f6c5 207 god_modeOn();
nasiromar 9:cbb9cfb1f6c5 208 } else
nasiromar 9:cbb9cfb1f6c5 209 god_modeOff();
nasiromar 9:cbb9cfb1f6c5 210
nasiromar 9:cbb9cfb1f6c5 211 }
nasiromar 9:cbb9cfb1f6c5 212 break;
nasiromar 9:cbb9cfb1f6c5 213
nasiromar 9:cbb9cfb1f6c5 214 default:
nasiromar 9:cbb9cfb1f6c5 215
nasiromar 9:cbb9cfb1f6c5 216 break;
nasiromar 9:cbb9cfb1f6c5 217 }
nasiromar 9:cbb9cfb1f6c5 218 return NO_RESULT;
rconnorlawson 0:35660d7952f7 219 }
rconnorlawson 0:35660d7952f7 220
nasiromar 9:cbb9cfb1f6c5 221 /**
nasiromar 9:cbb9cfb1f6c5 222 * Entry point for frame drawing. This should be called once per iteration of
nasiromar 9:cbb9cfb1f6c5 223 * the game loop. This draws all tiles on the screen, followed by the status
nasiromar 9:cbb9cfb1f6c5 224 * bars. Unless init is nonzero, this function will optimize drawing by only
nasiromar 9:cbb9cfb1f6c5 225 * drawing tiles that have changed from the previous frame.
nasiromar 9:cbb9cfb1f6c5 226 */
nasiromar 9:cbb9cfb1f6c5 227 void draw_game(int init) {
nasiromar 9:cbb9cfb1f6c5 228 // Draw game border first
nasiromar 9:cbb9cfb1f6c5 229 if(init) draw_border();
nasiromar 9:cbb9cfb1f6c5 230
nasiromar 9:cbb9cfb1f6c5 231 // Iterate over all visible map tiles
nasiromar 9:cbb9cfb1f6c5 232 for (int i = -5; i <= 5; i++) { // Iterate over columns of tiles
nasiromar 9:cbb9cfb1f6c5 233 for (int j = -4; j <= 4; j++) { // Iterate over one column of tiles
nasiromar 9:cbb9cfb1f6c5 234 // Here, we have a given (i,j)
nasiromar 9:cbb9cfb1f6c5 235
nasiromar 9:cbb9cfb1f6c5 236 // Compute the current map (x,y) of this tile
nasiromar 9:cbb9cfb1f6c5 237 int x = i + Player.x;
nasiromar 9:cbb9cfb1f6c5 238 int y = j + Player.y;
nasiromar 9:cbb9cfb1f6c5 239
nasiromar 9:cbb9cfb1f6c5 240 // Compute the previous map (px, py) of this tile
nasiromar 9:cbb9cfb1f6c5 241 int px = i + Player.px;
nasiromar 9:cbb9cfb1f6c5 242 int py = j + Player.py;
nasiromar 9:cbb9cfb1f6c5 243
nasiromar 9:cbb9cfb1f6c5 244 // Compute u,v coordinates for drawing
nasiromar 9:cbb9cfb1f6c5 245 int u = (i+5)*11 + 3;
nasiromar 9:cbb9cfb1f6c5 246 int v = (j+4)*11 + 15;
nasiromar 9:cbb9cfb1f6c5 247
nasiromar 9:cbb9cfb1f6c5 248 // Figure out what to draw
nasiromar 9:cbb9cfb1f6c5 249 DrawFunc draw = NULL;
nasiromar 9:cbb9cfb1f6c5 250 if (init && i == 0 && j == 0) { // Only draw the player on init
nasiromar 9:cbb9cfb1f6c5 251 draw_player(u, v, Player.has_key);
nasiromar 9:cbb9cfb1f6c5 252 continue;
nasiromar 9:cbb9cfb1f6c5 253 } else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) { // Current (i,j) in the map
nasiromar 9:cbb9cfb1f6c5 254 MapItem* curr_item = get_here(x, y);
nasiromar 9:cbb9cfb1f6c5 255 MapItem* prev_item = get_here(px, py);
nasiromar 9:cbb9cfb1f6c5 256 if (init || curr_item != prev_item) { // Only draw if they're different
nasiromar 9:cbb9cfb1f6c5 257 if (curr_item) { // There's something here! Draw it
nasiromar 9:cbb9cfb1f6c5 258 draw = curr_item->draw;
nasiromar 9:cbb9cfb1f6c5 259 } else { // There used to be something, but now there isn't
nasiromar 9:cbb9cfb1f6c5 260 draw = draw_nothing;
nasiromar 9:cbb9cfb1f6c5 261 }
nasiromar 9:cbb9cfb1f6c5 262 }
nasiromar 9:cbb9cfb1f6c5 263 } else if (init) { // If doing a full draw, but we're out of bounds, draw the walls.
nasiromar 9:cbb9cfb1f6c5 264 draw = draw_wall;
nasiromar 9:cbb9cfb1f6c5 265 }
nasiromar 9:cbb9cfb1f6c5 266
nasiromar 9:cbb9cfb1f6c5 267 // Actually draw the tile
nasiromar 9:cbb9cfb1f6c5 268 if (draw) draw(u, v);
nasiromar 9:cbb9cfb1f6c5 269 }
nasiromar 9:cbb9cfb1f6c5 270 }
nasiromar 9:cbb9cfb1f6c5 271
nasiromar 9:cbb9cfb1f6c5 272 // Draw status bars
nasiromar 9:cbb9cfb1f6c5 273 draw_upper_status(Player.x,Player.y);
nasiromar 9:cbb9cfb1f6c5 274 draw_lower_status(Player.HP,Player.MP);
nasiromar 9:cbb9cfb1f6c5 275 }
rconnorlawson 0:35660d7952f7 276
rconnorlawson 0:35660d7952f7 277
nasiromar 9:cbb9cfb1f6c5 278 /**
nasiromar 9:cbb9cfb1f6c5 279 * Initialize the main world map. Add walls around the edges, interior chambers,
nasiromar 9:cbb9cfb1f6c5 280 * and plants in the background so you can see motion. Note: using the similar
nasiromar 9:cbb9cfb1f6c5 281 * procedure you can init the secondary map(s).
nasiromar 9:cbb9cfb1f6c5 282 */
nasiromar 9:cbb9cfb1f6c5 283 void init_main_map() {
nasiromar 9:cbb9cfb1f6c5 284 // "Random" plants
nasiromar 9:cbb9cfb1f6c5 285 Map* map = set_active_map(0);
nasiromar 9:cbb9cfb1f6c5 286 for(int i = map_width() + 3; i < map_area(); i += 39) {
nasiromar 9:cbb9cfb1f6c5 287 add_plant(i % map_width(), i / map_width());
nasiromar 9:cbb9cfb1f6c5 288 }
nasiromar 9:cbb9cfb1f6c5 289 pc.printf("plants\r\n");
nasiromar 6:c9695079521d 290
nasiromar 9:cbb9cfb1f6c5 291 pc.printf("Adding walls!\r\n");
nasiromar 9:cbb9cfb1f6c5 292 add_wall(0, 0, HORIZONTAL, map_width());
nasiromar 9:cbb9cfb1f6c5 293 add_wall(0, map_height()-1, HORIZONTAL, map_width());
nasiromar 9:cbb9cfb1f6c5 294 add_wall(0, 0, VERTICAL, map_height());
nasiromar 9:cbb9cfb1f6c5 295 add_wall(map_width()-1, 0, VERTICAL, map_height());
nasiromar 9:cbb9cfb1f6c5 296 pc.printf("Walls done!\r\n");
rconnorlawson 0:35660d7952f7 297
nasiromar 6:c9695079521d 298
nasiromar 9:cbb9cfb1f6c5 299
nasiromar 9:cbb9cfb1f6c5 300 for(int i = 7; i < 13; i++) {
nasiromar 9:cbb9cfb1f6c5 301 for (int j = 23; j <31; j++) {
nasiromar 9:cbb9cfb1f6c5 302 add_kindom(j,i);
nasiromar 9:cbb9cfb1f6c5 303 }
nasiromar 9:cbb9cfb1f6c5 304 }
nasiromar 7:862062ffca62 305
nasiromar 9:cbb9cfb1f6c5 306
nasiromar 9:cbb9cfb1f6c5 307
nasiromar 9:cbb9cfb1f6c5 308 set_npc(9,9);
nasiromar 9:cbb9cfb1f6c5 309
nasiromar 9:cbb9cfb1f6c5 310 //add_chest(10,10);
nasiromar 9:cbb9cfb1f6c5 311
nasiromar 9:cbb9cfb1f6c5 312 add_door(26,12);
nasiromar 9:cbb9cfb1f6c5 313
nasiromar 9:cbb9cfb1f6c5 314 add_portal(20,35);
nasiromar 9:cbb9cfb1f6c5 315
nasiromar 9:cbb9cfb1f6c5 316 print_map();
nasiromar 9:cbb9cfb1f6c5 317 }
nasiromar 9:cbb9cfb1f6c5 318
nasiromar 6:c9695079521d 319
nasiromar 9:cbb9cfb1f6c5 320 /**
nasiromar 9:cbb9cfb1f6c5 321 * Program entry point! This is where it all begins.
nasiromar 9:cbb9cfb1f6c5 322 * This function orchestrates all the parts of the game. Most of your
nasiromar 9:cbb9cfb1f6c5 323 * implementation should be elsewhere - this holds the game loop, and should
nasiromar 9:cbb9cfb1f6c5 324 * read like a road map for the rest of the code.
nasiromar 9:cbb9cfb1f6c5 325 */
nasiromar 9:cbb9cfb1f6c5 326 int main() {
nasiromar 6:c9695079521d 327
nasiromar 9:cbb9cfb1f6c5 328 Player.HP = 100;
nasiromar 9:cbb9cfb1f6c5 329 Player.MP = 100;
nasiromar 9:cbb9cfb1f6c5 330
nasiromar 9:cbb9cfb1f6c5 331 // MapItem* north = get_north(Player.x,Player.y);
nasiromar 9:cbb9cfb1f6c5 332
nasiromar 9:cbb9cfb1f6c5 333
nasiromar 9:cbb9cfb1f6c5 334 // First things first: initialize hardware
nasiromar 9:cbb9cfb1f6c5 335 ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!");
rconnorlawson 0:35660d7952f7 336
nasiromar 9:cbb9cfb1f6c5 337 // Initialize the maps
nasiromar 9:cbb9cfb1f6c5 338 maps_init();
nasiromar 9:cbb9cfb1f6c5 339 init_main_map();
nasiromar 9:cbb9cfb1f6c5 340
nasiromar 9:cbb9cfb1f6c5 341 // Initialize game state
nasiromar 9:cbb9cfb1f6c5 342 set_active_map(0);
nasiromar 9:cbb9cfb1f6c5 343 Player.x = Player.y = 5;
nasiromar 9:cbb9cfb1f6c5 344
nasiromar 9:cbb9cfb1f6c5 345 // Initial drawing
nasiromar 9:cbb9cfb1f6c5 346 draw_game(true);
nasiromar 9:cbb9cfb1f6c5 347 // Main game loop
nasiromar 9:cbb9cfb1f6c5 348 while(1) {
nasiromar 9:cbb9cfb1f6c5 349 // Timer to measure game update speed
nasiromar 9:cbb9cfb1f6c5 350 Timer t;
nasiromar 9:cbb9cfb1f6c5 351 t.start();
nasiromar 6:c9695079521d 352
nasiromar 9:cbb9cfb1f6c5 353 // Actually do the game update:
nasiromar 9:cbb9cfb1f6c5 354 // 1. Read inputs
nasiromar 9:cbb9cfb1f6c5 355 GameInputs input = read_inputs();
nasiromar 9:cbb9cfb1f6c5 356 // 2. Determine action (get_action)
nasiromar 9:cbb9cfb1f6c5 357 int action = get_action(input);
nasiromar 9:cbb9cfb1f6c5 358 // 3. Update game (update_game)
nasiromar 9:cbb9cfb1f6c5 359 update_game(action);
nasiromar 9:cbb9cfb1f6c5 360 // 3b. Check for game over
nasiromar 9:cbb9cfb1f6c5 361 if(Player.won == 1) {
nasiromar 9:cbb9cfb1f6c5 362 draw_gameover();
nasiromar 9:cbb9cfb1f6c5 363 break;
nasiromar 8:fcc333a8f9e1 364 }
nasiromar 9:cbb9cfb1f6c5 365 // 4. Draw frame (draw_game)
nasiromar 9:cbb9cfb1f6c5 366 draw_game(true);
nasiromar 6:c9695079521d 367
nasiromar 9:cbb9cfb1f6c5 368 // 5. Frame delay
nasiromar 9:cbb9cfb1f6c5 369 t.stop();
nasiromar 9:cbb9cfb1f6c5 370 int dt = t.read_ms();
nasiromar 9:cbb9cfb1f6c5 371 if (dt < 100) wait_ms(100 - dt);
nasiromar 9:cbb9cfb1f6c5 372 }
nasiromar 9:cbb9cfb1f6c5 373 //
nasiromar 9:cbb9cfb1f6c5 374
nasiromar 9:cbb9cfb1f6c5 375
rconnorlawson 0:35660d7952f7 376 }