Adventure game written for ECE2035 at the Georgia Institute of Technology
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
Diff: main.cpp
- Revision:
- 2:0876296d9473
- Parent:
- 0:35660d7952f7
- Child:
- 3:289762133fd6
diff -r 399033d39feb -r 0876296d9473 main.cpp --- a/main.cpp Wed Apr 04 21:11:07 2018 +0000 +++ b/main.cpp Tue Apr 17 17:17:20 2018 +0000 @@ -4,6 +4,7 @@ #include "map.h" #include "graphics.h" #include "speech.h" +#include "startmenu.h" // Functions in this file int get_action (GameInputs inputs); @@ -20,21 +21,46 @@ int x,y; // Current locations int px, py; // Previous locations int has_key; + int quest_complete; } Player; +int door_open = false; +int game_over = false; +int health = 0; + /** * Given the game inputs, determine what kind of update needs to happen. * Possbile return values are defined below. */ #define NO_ACTION 0 -#define ACTION_BUTTON 1 -#define MENU_BUTTON 2 -#define GO_LEFT 3 -#define GO_RIGHT 4 -#define GO_UP 5 -#define GO_DOWN 6 +#define BUTTON1 1 +#define BUTTON2 2 +#define BUTTON3 3 +#define MENU_BUTTON 4 +#define GO_LEFT 5 +#define GO_RIGHT 6 +#define GO_UP 7 +#define GO_DOWN 8 int get_action(GameInputs inputs) { + if(!inputs.b1){ + return BUTTON1; + } + if(!inputs.b2){ + return BUTTON2; + } + if (!inputs.b3){ + return MENU_BUTTON; + } + if ((inputs.ay > 0.05 && inputs.ay < 0.5) && abs(inputs.ax) < .5){ + return GO_LEFT; + } else if ((inputs.ay < -0.05 && inputs.ay > -0.5) && abs(inputs.ax) < .5){ + return GO_RIGHT; + }else if (inputs.ax > 0.05 && inputs.ax < 0.5){ + return GO_DOWN; + }else if (inputs.ax < -0.05 && inputs.ax > -0.5){ + return GO_UP; + } return NO_ACTION; } @@ -58,14 +84,102 @@ // Do different things based on the each action. // You can define functions like "go_up()" that get called for each case. - switch(action) + switch(action) //have a function called for each type of object. Pass a pointer to the object to the interact function { - case GO_UP: break; - case GO_LEFT: break; - case GO_DOWN: break; - case GO_RIGHT: break; - case ACTION_BUTTON: break; - case MENU_BUTTON: break; + case GO_UP: + if (MapItem* object = get_north(Player.x, Player.y)){ + if (object -> type == NPC){ + NonPlayer* n = (NonPlayer*)object -> data; + if(n -> quest_requested == false){ + speech("Get dat money", "its dat way"); + n -> quest_requested = true; + } else if (Player.quest_complete == false){ //should quest complete be in the player struct? + speech("What are u waitin for?", "get it boi"); + } else if (n -> has_key == true) { + speech("Congrats!", "Here's the keyyyy"); + n -> has_key = false; + Player.has_key = true; + } else if(n -> has_key == false){ + speech("Congrats!", "Quest complete."); + } + // else if (object -> type == DOOR){ + // bool o = object -> data; + // if (Player.has_key == true){ + // o = true; + // } + //} + + return NO_RESULT; + } + else if (object -> walkable == false){ + return NO_RESULT; + } + } + Player.y = Player.y - 1; + return FULL_DRAW; + case GO_LEFT: + if (MapItem* object = get_west(Player.x, Player.y)){ + if (object -> type == ROCK){ + add_rock(Player.x + 2, Player.y); + map_erase(Player.x+1, Player.y); + Player.x = Player.x + 1; + Player.quest_complete = true; + return FULL_DRAW; + + } else if(object -> type == SPIKE){ + //lower health bar + health++; + return NO_RESULT; + + } + if (object -> walkable == false){ + return NO_RESULT; + } + } + Player.x = Player.x + 1; + return FULL_DRAW; + case GO_DOWN: + if (MapItem* object = get_south(Player.x, Player.y)){ + if (object -> type == DOOR){ + if (Player.has_key == true){ + map_erase(Player.x, Player.y + 1); + Player.y = Player.y + 1; + door_open = true; + return FULL_DRAW; + } + return NO_RESULT; + } + if (object -> walkable == false){ + return NO_RESULT; + } + } + Player.y = Player.y + 1; + return FULL_DRAW; + + case GO_RIGHT: + if (MapItem* object = get_east(Player.x, Player.y)){ + if (object -> type == GOAL){ + if(door_open == true){ + game_over = true; + return NO_RESULT; + } + + } + if (object -> walkable == false){ + return NO_RESULT; + } + } + Player.x = Player.x - 1; + return FULL_DRAW; + + case BUTTON1: + omni(); + break; + case BUTTON2: + break; + case MENU_BUTTON: + + break; default: break; } return NO_RESULT; @@ -79,6 +193,8 @@ */ void draw_game(int init) { + // int param1; + //int param2; // Draw game border first if(init) draw_border(); @@ -103,9 +219,10 @@ // Figure out what to draw DrawFunc draw = NULL; - if (init && i == 0 && j == 0) // Only draw the player on init + if (init && i == 0 && j == 0) // Only draw the player on init (this is when the loop hits the center of the screen) { draw_player(u, v, Player.has_key); + continue; } else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) // Current (i,j) in the map @@ -116,6 +233,12 @@ { if (curr_item) // There's something here! Draw it { + //if (curr_item -> type == ROCK){ + //set parameters + // Rock* r = (Rock*) curr_item -> data; + //param1 = r -> x; + //param2 = r -> y; + //} draw = curr_item->draw; } else // There used to be something, but now there isn't @@ -130,13 +253,22 @@ } // Actually draw the tile - if (draw) draw(u, v); + //MapItem* curr_item = get_here(x, y); + //MapItem* prev_item = get_here(px, py); + if (draw){ + // if(curr_item){ + // if(curr_item -> type == ROCK){ + // draw(param1, param2); + //} + //} + draw(u,v); + } } } // Draw status bars - draw_upper_status(); - draw_lower_status(); + draw_upper_status(health); + draw_lower_status(Player.x, Player.y); } @@ -152,6 +284,15 @@ { add_plant(i % map_width(), i / map_width()); } + + srand(time(NULL)); + for(int i = 0; i < 30; i++) + { + int x = rand()%((map_width()+1)-1) + 1; + int y = rand()%((map_height()+1)-1) + 1; + add_spike(x, y); + } + pc.printf("plants\r\n"); pc.printf("Adding walls!\r\n"); @@ -159,6 +300,11 @@ add_wall(0, map_height()-1, HORIZONTAL, map_width()); add_wall(0, 0, VERTICAL, map_height()); add_wall(map_width()-1, 0, VERTICAL, map_height()); + add_wall(20, 20, HORIZONTAL, 15); + add_npc(13, 13); + add_rock(6, 42); + add_door(7, 48); + add_goal(43, 5); pc.printf("Walls done!\r\n"); print_map(); @@ -172,6 +318,7 @@ */ int main() { + int start = menu(); // First things first: initialize hardware ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!"); @@ -182,6 +329,7 @@ // Initialize game state set_active_map(0); Player.x = Player.y = 5; + Player.quest_complete = false; // Initial drawing draw_game(true); @@ -192,12 +340,20 @@ // Timer to measure game update speed Timer t; t.start(); - // Actuall do the game update: - // 1. Read inputs - // 2. Determine action (get_action) + // Actually do the game update: + // 1. Read inputs + GameInputs in = read_inputs(); + // 2. Determine action (get_action) + int a = get_action(in); // 3. Update game (update_game) + int u = update_game(a); // 3b. Check for game over + if (game_over == true){ + draw_game_over(); + }else{ // 4. Draw frame (draw_game) + draw_game(u); + } // 5. Frame delay t.stop();