Adventure game written for ECE2035 at the Georgia Institute of Technology

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
trmontgomery
Date:
Tue May 22 19:13:03 2018 +0000
Revision:
4:cdc54191ff07
Parent:
3:289762133fd6
Child:
5:93a4c396c1af
Menu class complete

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"
trmontgomery 2:0876296d9473 7 #include "startmenu.h"
trmontgomery 4:cdc54191ff07 8 #include "ingamemenu.h"
trmontgomery 4:cdc54191ff07 9 #include "gameover.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);
trmontgomery 3:289762133fd6 15 void init_main_map();
trmontgomery 3:289762133fd6 16 void init_house_map();
rconnorlawson 0:35660d7952f7 17 int main ();
rconnorlawson 0:35660d7952f7 18
trmontgomery 4:cdc54191ff07 19 Player p1;
trmontgomery 4:cdc54191ff07 20 int omni_en = 0;
trmontgomery 4:cdc54191ff07 21 int in_house;
trmontgomery 3:289762133fd6 22 int house_enabled = false;
trmontgomery 2:0876296d9473 23 int door_open = false;
trmontgomery 2:0876296d9473 24 int game_over = false;
trmontgomery 4:cdc54191ff07 25 int switch_axis = 0;
trmontgomery 4:cdc54191ff07 26
trmontgomery 4:cdc54191ff07 27
trmontgomery 4:cdc54191ff07 28 /*Functions to for player to interact with the map
trmontgomery 4:cdc54191ff07 29 */
trmontgomery 4:cdc54191ff07 30
trmontgomery 4:cdc54191ff07 31 void interact_npc(MapItem* object){
trmontgomery 4:cdc54191ff07 32 if (p1.rock_pushed && p1.gems >= 3){
trmontgomery 4:cdc54191ff07 33 p1.quest_complete = true;
trmontgomery 4:cdc54191ff07 34 }
trmontgomery 4:cdc54191ff07 35 NonPlayer* n = (NonPlayer*)object -> data;
trmontgomery 4:cdc54191ff07 36 if(n -> quest_requested == false){
trmontgomery 4:cdc54191ff07 37 speech("Find 3 red gems", "and move the rock", "in the backyard", "then I will give you gold!");
trmontgomery 4:cdc54191ff07 38 n -> quest_requested = true;
trmontgomery 4:cdc54191ff07 39 } else if (p1.quest_complete == false){ //should quest complete be in the player struct?
trmontgomery 4:cdc54191ff07 40 speech("get it boi! What", "are u waitin for?", "You must finish ", "the quest!!!!");
trmontgomery 4:cdc54191ff07 41 } else if (n -> has_key == true) {
trmontgomery 4:cdc54191ff07 42 speech("Congrats!", "Thank you for", "completing the", "quest! Here's the key!");
trmontgomery 4:cdc54191ff07 43 n -> has_key = false;
trmontgomery 4:cdc54191ff07 44 p1.has_key = true;
trmontgomery 4:cdc54191ff07 45 } else if(n -> has_key == false){
trmontgomery 4:cdc54191ff07 46 speech("Congrats!", "Quest complete.", "", "");
trmontgomery 4:cdc54191ff07 47 }
trmontgomery 4:cdc54191ff07 48 }
trmontgomery 4:cdc54191ff07 49
trmontgomery 4:cdc54191ff07 50 void player_hurt(){
trmontgomery 4:cdc54191ff07 51 //lower health bar
trmontgomery 4:cdc54191ff07 52 p1.health+=10;
trmontgomery 4:cdc54191ff07 53 for(int i = 0; i < 2; i++){
trmontgomery 4:cdc54191ff07 54 speaker.period(1.0/150.0); // 500hz period
trmontgomery 4:cdc54191ff07 55 speaker =0.25; //25% duty cycle - mid range volume
trmontgomery 4:cdc54191ff07 56 wait(.02);
trmontgomery 4:cdc54191ff07 57 speaker=0.0; // turn off audio
trmontgomery 4:cdc54191ff07 58 wait(0.1);
trmontgomery 4:cdc54191ff07 59 }
trmontgomery 4:cdc54191ff07 60 }
trmontgomery 4:cdc54191ff07 61
trmontgomery 2:0876296d9473 62
rconnorlawson 0:35660d7952f7 63 /**
rconnorlawson 0:35660d7952f7 64 * Given the game inputs, determine what kind of update needs to happen.
rconnorlawson 0:35660d7952f7 65 * Possbile return values are defined below.
rconnorlawson 0:35660d7952f7 66 */
rconnorlawson 0:35660d7952f7 67 #define NO_ACTION 0
trmontgomery 4:cdc54191ff07 68 #define BUTTON1 1 //omni button
trmontgomery 4:cdc54191ff07 69 #define BUTTON2 2 //toggle/action button
trmontgomery 4:cdc54191ff07 70 #define BUTTON3 3 //scroll/select?
trmontgomery 2:0876296d9473 71 #define MENU_BUTTON 4
trmontgomery 2:0876296d9473 72 #define GO_LEFT 5
trmontgomery 2:0876296d9473 73 #define GO_RIGHT 6
trmontgomery 2:0876296d9473 74 #define GO_UP 7
trmontgomery 2:0876296d9473 75 #define GO_DOWN 8
rconnorlawson 0:35660d7952f7 76 int get_action(GameInputs inputs)
rconnorlawson 0:35660d7952f7 77 {
trmontgomery 4:cdc54191ff07 78 double y_axis;
trmontgomery 4:cdc54191ff07 79 double x_axis;
trmontgomery 4:cdc54191ff07 80 if(switch_axis){
trmontgomery 4:cdc54191ff07 81 y_axis = inputs.ax;
trmontgomery 4:cdc54191ff07 82 x_axis = inputs.ay;
trmontgomery 4:cdc54191ff07 83 }else{
trmontgomery 4:cdc54191ff07 84 y_axis = inputs.ay;
trmontgomery 4:cdc54191ff07 85 x_axis = inputs.ax;
trmontgomery 4:cdc54191ff07 86 }
trmontgomery 2:0876296d9473 87 if(!inputs.b1){
trmontgomery 2:0876296d9473 88 return BUTTON1;
trmontgomery 2:0876296d9473 89 }
trmontgomery 2:0876296d9473 90 if(!inputs.b2){
trmontgomery 2:0876296d9473 91 return BUTTON2;
trmontgomery 2:0876296d9473 92 }
trmontgomery 2:0876296d9473 93 if (!inputs.b3){
trmontgomery 2:0876296d9473 94 return MENU_BUTTON;
trmontgomery 2:0876296d9473 95 }
trmontgomery 4:cdc54191ff07 96 if ((y_axis > 0.05 && y_axis < 0.9) && abs(x_axis) < .9){
trmontgomery 2:0876296d9473 97 return GO_LEFT;
trmontgomery 4:cdc54191ff07 98 } else if ((y_axis < -0.05 && y_axis > -0.9) && abs(x_axis) < .9){
trmontgomery 2:0876296d9473 99 return GO_RIGHT;
trmontgomery 4:cdc54191ff07 100 }else if (x_axis > 0.05 && x_axis < 0.9){
trmontgomery 2:0876296d9473 101 return GO_DOWN;
trmontgomery 4:cdc54191ff07 102 }else if (x_axis < -0.05 && x_axis > -0.9){
trmontgomery 2:0876296d9473 103 return GO_UP;
trmontgomery 2:0876296d9473 104 }
rconnorlawson 0:35660d7952f7 105 return NO_ACTION;
rconnorlawson 0:35660d7952f7 106 }
rconnorlawson 0:35660d7952f7 107
trmontgomery 4:cdc54191ff07 108 void update_npcs(){
trmontgomery 4:cdc54191ff07 109 //Map* m = get_active_map();
trmontgomery 4:cdc54191ff07 110 /*int h = map_height();
trmontgomery 4:cdc54191ff07 111 int w = map_width();
trmontgomery 4:cdc54191ff07 112
trmontgomery 4:cdc54191ff07 113 for(int i = 0; i < w; i++){
trmontgomery 4:cdc54191ff07 114 for(int j = 0; j < h; j++){
trmontgomery 4:cdc54191ff07 115 if (MapItem* object = get_here(i, j)){
trmontgomery 4:cdc54191ff07 116 if (object -> type == EXTRA){
trmontgomery 4:cdc54191ff07 117 Extra* d = (Extra*)object -> data;
trmontgomery 4:cdc54191ff07 118 d -> e = true;
trmontgomery 4:cdc54191ff07 119 add_extra(i+1, j);
trmontgomery 4:cdc54191ff07 120 }
trmontgomery 4:cdc54191ff07 121 }
trmontgomery 4:cdc54191ff07 122 }
trmontgomery 4:cdc54191ff07 123 }
trmontgomery 4:cdc54191ff07 124 for(int i = 0; i < w; i++){
trmontgomery 4:cdc54191ff07 125 for(int j = 0; j < h; j++){
trmontgomery 4:cdc54191ff07 126 if (MapItem* object = get_here(i, j)){
trmontgomery 4:cdc54191ff07 127 if (object -> type == EXTRA){
trmontgomery 4:cdc54191ff07 128 Extra* d = (Extra*)object -> data;
trmontgomery 4:cdc54191ff07 129 if(d -> e == true) map_erase(i, j);
trmontgomery 4:cdc54191ff07 130 }
trmontgomery 4:cdc54191ff07 131 }
trmontgomery 4:cdc54191ff07 132 }
trmontgomery 4:cdc54191ff07 133 }*/
trmontgomery 4:cdc54191ff07 134 }
trmontgomery 4:cdc54191ff07 135
rconnorlawson 0:35660d7952f7 136 /**
rconnorlawson 0:35660d7952f7 137 * Update the game state based on the user action. For example, if the user
rconnorlawson 0:35660d7952f7 138 * requests GO_UP, then this function should determine if that is possible by
rconnorlawson 0:35660d7952f7 139 * consulting the map, and update the Player position accordingly.
rconnorlawson 0:35660d7952f7 140 *
rconnorlawson 0:35660d7952f7 141 * Return values are defined below. FULL_DRAW indicates that for this frame,
rconnorlawson 0:35660d7952f7 142 * draw_game should not optimize drawing and should draw every tile, even if
rconnorlawson 0:35660d7952f7 143 * the player has not moved.
rconnorlawson 0:35660d7952f7 144 */
rconnorlawson 0:35660d7952f7 145 #define NO_RESULT 0
rconnorlawson 0:35660d7952f7 146 #define GAME_OVER 1
rconnorlawson 0:35660d7952f7 147 #define FULL_DRAW 2
trmontgomery 4:cdc54191ff07 148 #define NO_DRAW 3
rconnorlawson 0:35660d7952f7 149 int update_game(int action)
rconnorlawson 0:35660d7952f7 150 {
rconnorlawson 0:35660d7952f7 151 // Save player previous location before updating
trmontgomery 4:cdc54191ff07 152 p1.px = p1.x;
trmontgomery 4:cdc54191ff07 153 p1.py = p1.y;
rconnorlawson 0:35660d7952f7 154
rconnorlawson 0:35660d7952f7 155 // Do different things based on the each action.
rconnorlawson 0:35660d7952f7 156 // You can define functions like "go_up()" that get called for each case.
trmontgomery 2:0876296d9473 157 switch(action) //have a function called for each type of object. Pass a pointer to the object to the interact function
rconnorlawson 0:35660d7952f7 158 {
trmontgomery 2:0876296d9473 159 case GO_UP:
trmontgomery 4:cdc54191ff07 160 if (MapItem* object = get_north(p1.x, p1.y)){
trmontgomery 4:cdc54191ff07 161 //if (object -> type == NPC){
trmontgomery 4:cdc54191ff07 162 //interact_npc(object);
trmontgomery 4:cdc54191ff07 163 // return NO_RESULT;
trmontgomery 4:cdc54191ff07 164 if (object -> type == LBUSH){
trmontgomery 4:cdc54191ff07 165 p1.y = p1.y - 1;
trmontgomery 4:cdc54191ff07 166 return NO_DRAW;
trmontgomery 4:cdc54191ff07 167 } else if(object -> type == SPIKE){
trmontgomery 4:cdc54191ff07 168 player_hurt();
trmontgomery 4:cdc54191ff07 169 } else if(object -> type == GEM){
trmontgomery 4:cdc54191ff07 170 //add to gem score
trmontgomery 4:cdc54191ff07 171 p1.gems++;
trmontgomery 4:cdc54191ff07 172 if (p1.gems >= 3 && p1.quest_complete < 1){
trmontgomery 4:cdc54191ff07 173 p1.quest_complete += 0.5;
trmontgomery 2:0876296d9473 174 }
trmontgomery 4:cdc54191ff07 175 map_erase(p1.x, p1.y-1);
trmontgomery 4:cdc54191ff07 176 } else if (!omni_en && object -> walkable == false){
trmontgomery 2:0876296d9473 177 return NO_RESULT;
trmontgomery 2:0876296d9473 178 }
trmontgomery 2:0876296d9473 179 }
trmontgomery 4:cdc54191ff07 180 p1.y = p1.y - 1;
trmontgomery 2:0876296d9473 181 return FULL_DRAW;
trmontgomery 2:0876296d9473 182 case GO_LEFT:
trmontgomery 4:cdc54191ff07 183 if (MapItem* object = get_west(p1.x, p1.y)){
trmontgomery 2:0876296d9473 184 if (object -> type == ROCK){
trmontgomery 4:cdc54191ff07 185 add_rock(p1.x + 2, p1.y);
trmontgomery 4:cdc54191ff07 186 map_erase(p1.x+1, p1.y);
trmontgomery 4:cdc54191ff07 187 p1.x = p1.x + 1;
trmontgomery 4:cdc54191ff07 188 p1.rock_pushed = true;
trmontgomery 2:0876296d9473 189 return FULL_DRAW;
trmontgomery 2:0876296d9473 190
trmontgomery 4:cdc54191ff07 191 }else if (object -> type == LBUSH){
trmontgomery 4:cdc54191ff07 192 p1.x = p1.x + 1;
trmontgomery 4:cdc54191ff07 193 return NO_DRAW;
trmontgomery 4:cdc54191ff07 194 }else if(object -> type == SPIKE){
trmontgomery 4:cdc54191ff07 195 player_hurt();
trmontgomery 4:cdc54191ff07 196 } else if(object -> type == GEM){
trmontgomery 2:0876296d9473 197 //lower health bar
trmontgomery 4:cdc54191ff07 198 p1.gems++;
trmontgomery 4:cdc54191ff07 199 if (p1.gems >= 3 && p1.quest_complete < 1){
trmontgomery 4:cdc54191ff07 200 p1.quest_complete += 0.5;
trmontgomery 4:cdc54191ff07 201 }
trmontgomery 4:cdc54191ff07 202 map_erase(p1.x+1, p1.y);
trmontgomery 4:cdc54191ff07 203 }else if(object -> type == DOOR){
trmontgomery 4:cdc54191ff07 204 if (in_house){
trmontgomery 4:cdc54191ff07 205 set_active_map(0);
trmontgomery 4:cdc54191ff07 206 p1.x = 3;
trmontgomery 4:cdc54191ff07 207 p1.y = 7;
trmontgomery 4:cdc54191ff07 208 in_house = 0;
trmontgomery 4:cdc54191ff07 209 }else{
trmontgomery 4:cdc54191ff07 210 set_active_map(1);
trmontgomery 4:cdc54191ff07 211 p1.x = 6;
trmontgomery 4:cdc54191ff07 212 p1.y = 7;
trmontgomery 4:cdc54191ff07 213 in_house = 1;
trmontgomery 4:cdc54191ff07 214 }
trmontgomery 4:cdc54191ff07 215 } if (!omni_en && object -> walkable == false){
trmontgomery 2:0876296d9473 216 return NO_RESULT;
trmontgomery 2:0876296d9473 217 }
trmontgomery 2:0876296d9473 218 }
trmontgomery 4:cdc54191ff07 219 p1.x = p1.x + 1;
trmontgomery 2:0876296d9473 220 return FULL_DRAW;
trmontgomery 2:0876296d9473 221 case GO_DOWN:
trmontgomery 4:cdc54191ff07 222 if (MapItem* object = get_south(p1.x, p1.y)){
trmontgomery 2:0876296d9473 223 if (object -> type == DOOR){
trmontgomery 4:cdc54191ff07 224 if (p1.has_key == true){
trmontgomery 4:cdc54191ff07 225 map_erase(p1.x, p1.y + 1);
trmontgomery 4:cdc54191ff07 226 p1.y = p1.y + 1;
trmontgomery 2:0876296d9473 227 door_open = true;
trmontgomery 2:0876296d9473 228 return FULL_DRAW;
trmontgomery 2:0876296d9473 229 }
trmontgomery 2:0876296d9473 230 return NO_RESULT;
trmontgomery 4:cdc54191ff07 231 }if (object -> type == LBUSH){
trmontgomery 4:cdc54191ff07 232 p1.y = p1.y + 1;
trmontgomery 4:cdc54191ff07 233 return NO_DRAW;
trmontgomery 4:cdc54191ff07 234 }else if(object -> type == SPIKE){
trmontgomery 4:cdc54191ff07 235 player_hurt();
trmontgomery 4:cdc54191ff07 236 } else if(object -> type == GEM){
trmontgomery 4:cdc54191ff07 237 //add to gem score
trmontgomery 4:cdc54191ff07 238 p1.gems++;
trmontgomery 4:cdc54191ff07 239 if (p1.gems >= 3 && p1.quest_complete < 1){
trmontgomery 4:cdc54191ff07 240 p1.quest_complete += 0.5;
trmontgomery 4:cdc54191ff07 241 }
trmontgomery 4:cdc54191ff07 242 map_erase(p1.x, p1.y+1);
trmontgomery 2:0876296d9473 243 }
trmontgomery 4:cdc54191ff07 244 if (!omni_en && object -> walkable == false){
trmontgomery 2:0876296d9473 245 return NO_RESULT;
trmontgomery 2:0876296d9473 246 }
trmontgomery 2:0876296d9473 247 }
trmontgomery 4:cdc54191ff07 248 p1.y = p1.y + 1;
trmontgomery 2:0876296d9473 249 return FULL_DRAW;
trmontgomery 2:0876296d9473 250
trmontgomery 2:0876296d9473 251 case GO_RIGHT:
trmontgomery 4:cdc54191ff07 252 if (MapItem* object = get_east(p1.x, p1.y)){
trmontgomery 2:0876296d9473 253 if (object -> type == GOAL){
trmontgomery 2:0876296d9473 254 if(door_open == true){
trmontgomery 2:0876296d9473 255 game_over = true;
trmontgomery 4:cdc54191ff07 256 uLCD.locate(1,1);
trmontgomery 4:cdc54191ff07 257 uLCD.printf("Goal reached");
trmontgomery 2:0876296d9473 258 return NO_RESULT;
trmontgomery 2:0876296d9473 259 }
trmontgomery 2:0876296d9473 260
trmontgomery 4:cdc54191ff07 261 }//else if(object -> type == GDOOR){
trmontgomery 4:cdc54191ff07 262
trmontgomery 4:cdc54191ff07 263 //} //add a bit of a wait here to simulate going into the house
trmontgomery 4:cdc54191ff07 264 else if(object -> type == SPIKE){
trmontgomery 4:cdc54191ff07 265 player_hurt();
trmontgomery 4:cdc54191ff07 266
trmontgomery 4:cdc54191ff07 267 } if (object -> type == LBUSH){
trmontgomery 4:cdc54191ff07 268 p1.x = p1.x - 1;
trmontgomery 4:cdc54191ff07 269 return NO_DRAW;
trmontgomery 4:cdc54191ff07 270 }else if(object -> type == GEM){
trmontgomery 4:cdc54191ff07 271 //add to gem score
trmontgomery 4:cdc54191ff07 272 p1.gems++;
trmontgomery 4:cdc54191ff07 273 if (p1.gems >= 3 && p1.quest_complete < 1){
trmontgomery 4:cdc54191ff07 274 p1.quest_complete += 0.5;
trmontgomery 4:cdc54191ff07 275 }
trmontgomery 4:cdc54191ff07 276 map_erase(p1.x-1, p1.y);
trmontgomery 4:cdc54191ff07 277 }
trmontgomery 4:cdc54191ff07 278 if (!omni_en && object -> walkable == false){
trmontgomery 2:0876296d9473 279 return NO_RESULT;
trmontgomery 2:0876296d9473 280 }
trmontgomery 2:0876296d9473 281 }
trmontgomery 4:cdc54191ff07 282 p1.x = p1.x - 1;
trmontgomery 2:0876296d9473 283 return FULL_DRAW;
trmontgomery 2:0876296d9473 284
trmontgomery 2:0876296d9473 285 case BUTTON1:
trmontgomery 4:cdc54191ff07 286 omni_en = !omni_en;
trmontgomery 2:0876296d9473 287 break;
trmontgomery 2:0876296d9473 288 case BUTTON2:
trmontgomery 4:cdc54191ff07 289 MapItem* object1 = get_north(p1.x, p1.y);
trmontgomery 4:cdc54191ff07 290 MapItem* object2 = get_south(p1.x, p1.y);
trmontgomery 4:cdc54191ff07 291 MapItem* object3 = get_east(p1.x, p1.y);
trmontgomery 4:cdc54191ff07 292 MapItem* object4 = get_west(p1.x, p1.y);
trmontgomery 4:cdc54191ff07 293 if(object1 ->type == NPC){
trmontgomery 4:cdc54191ff07 294 interact_npc(object1);
trmontgomery 4:cdc54191ff07 295 } else if (object2->type ==NPC){
trmontgomery 4:cdc54191ff07 296 interact_npc(object2);
trmontgomery 4:cdc54191ff07 297 }else if (object3->type ==NPC){
trmontgomery 4:cdc54191ff07 298 interact_npc(object3);
trmontgomery 4:cdc54191ff07 299 } else if (object4->type ==NPC){
trmontgomery 4:cdc54191ff07 300 interact_npc(object4);
trmontgomery 4:cdc54191ff07 301 }
trmontgomery 4:cdc54191ff07 302 else if (object1 ->type == GDOOR|| object2->type == GDOOR || object3 ->type == GDOOR|| object4 ->type == GDOOR){
trmontgomery 4:cdc54191ff07 303 if (p1.has_key){
trmontgomery 4:cdc54191ff07 304 door_open = true;
trmontgomery 4:cdc54191ff07 305 map_erase(p1.x-1, p1.y);
trmontgomery 4:cdc54191ff07 306 } else{
trmontgomery 4:cdc54191ff07 307 speech("Door Locked.", "Must get key.", "Door Locked.", "Must get key.");
trmontgomery 4:cdc54191ff07 308 }
trmontgomery 4:cdc54191ff07 309 }
trmontgomery 2:0876296d9473 310 break;
trmontgomery 2:0876296d9473 311 case MENU_BUTTON:
trmontgomery 4:cdc54191ff07 312 switch_axis = ingamemenu(&p1);
trmontgomery 2:0876296d9473 313
trmontgomery 2:0876296d9473 314 break;
trmontgomery 4:cdc54191ff07 315 default: break;
rconnorlawson 0:35660d7952f7 316 }
rconnorlawson 0:35660d7952f7 317 return NO_RESULT;
rconnorlawson 0:35660d7952f7 318 }
rconnorlawson 0:35660d7952f7 319
trmontgomery 4:cdc54191ff07 320
trmontgomery 4:cdc54191ff07 321
rconnorlawson 0:35660d7952f7 322 /**
rconnorlawson 0:35660d7952f7 323 * Entry point for frame drawing. This should be called once per iteration of
rconnorlawson 0:35660d7952f7 324 * the game loop. This draws all tiles on the screen, followed by the status
rconnorlawson 0:35660d7952f7 325 * bars. Unless init is nonzero, this function will optimize drawing by only
rconnorlawson 0:35660d7952f7 326 * drawing tiles that have changed from the previous frame.
rconnorlawson 0:35660d7952f7 327 */
rconnorlawson 0:35660d7952f7 328 void draw_game(int init)
rconnorlawson 0:35660d7952f7 329 {
trmontgomery 2:0876296d9473 330 // int param1;
trmontgomery 2:0876296d9473 331 //int param2;
rconnorlawson 0:35660d7952f7 332 // Draw game border first
rconnorlawson 0:35660d7952f7 333 if(init) draw_border();
rconnorlawson 0:35660d7952f7 334
rconnorlawson 0:35660d7952f7 335 // Iterate over all visible map tiles
rconnorlawson 0:35660d7952f7 336 for (int i = -5; i <= 5; i++) // Iterate over columns of tiles
rconnorlawson 0:35660d7952f7 337 {
rconnorlawson 0:35660d7952f7 338 for (int j = -4; j <= 4; j++) // Iterate over one column of tiles
rconnorlawson 0:35660d7952f7 339 {
rconnorlawson 0:35660d7952f7 340 // Here, we have a given (i,j)
rconnorlawson 0:35660d7952f7 341
rconnorlawson 0:35660d7952f7 342 // Compute the current map (x,y) of this tile
trmontgomery 4:cdc54191ff07 343 int x = i + p1.x;
trmontgomery 4:cdc54191ff07 344 int y = j + p1.y;
rconnorlawson 0:35660d7952f7 345
rconnorlawson 0:35660d7952f7 346 // Compute the previous map (px, py) of this tile
trmontgomery 4:cdc54191ff07 347 int px = i + p1.px;
trmontgomery 4:cdc54191ff07 348 int py = j + p1.py;
rconnorlawson 0:35660d7952f7 349
rconnorlawson 0:35660d7952f7 350 // Compute u,v coordinates for drawing
rconnorlawson 0:35660d7952f7 351 int u = (i+5)*11 + 3;
rconnorlawson 0:35660d7952f7 352 int v = (j+4)*11 + 15;
rconnorlawson 0:35660d7952f7 353
rconnorlawson 0:35660d7952f7 354 // Figure out what to draw
rconnorlawson 0:35660d7952f7 355 DrawFunc draw = NULL;
trmontgomery 4:cdc54191ff07 356 if (init && i == 0 && j == 0 && init != NO_DRAW) // Only draw the player on init (this is when the loop hits the center of the screen)
rconnorlawson 0:35660d7952f7 357 {
trmontgomery 4:cdc54191ff07 358 draw_player(u, v, p1.has_key);
trmontgomery 2:0876296d9473 359
rconnorlawson 0:35660d7952f7 360 continue;
rconnorlawson 0:35660d7952f7 361 }
rconnorlawson 0:35660d7952f7 362 else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) // Current (i,j) in the map
rconnorlawson 0:35660d7952f7 363 {
rconnorlawson 0:35660d7952f7 364 MapItem* curr_item = get_here(x, y);
rconnorlawson 0:35660d7952f7 365 MapItem* prev_item = get_here(px, py);
rconnorlawson 0:35660d7952f7 366 if (init || curr_item != prev_item) // Only draw if they're different
rconnorlawson 0:35660d7952f7 367 {
rconnorlawson 0:35660d7952f7 368 if (curr_item) // There's something here! Draw it
rconnorlawson 0:35660d7952f7 369 {
trmontgomery 2:0876296d9473 370 //if (curr_item -> type == ROCK){
trmontgomery 2:0876296d9473 371 //set parameters
trmontgomery 2:0876296d9473 372 // Rock* r = (Rock*) curr_item -> data;
trmontgomery 2:0876296d9473 373 //param1 = r -> x;
trmontgomery 2:0876296d9473 374 //param2 = r -> y;
trmontgomery 2:0876296d9473 375 //}
rconnorlawson 0:35660d7952f7 376 draw = curr_item->draw;
rconnorlawson 0:35660d7952f7 377 }
rconnorlawson 0:35660d7952f7 378 else // There used to be something, but now there isn't
rconnorlawson 0:35660d7952f7 379 {
trmontgomery 4:cdc54191ff07 380 if (in_house){
trmontgomery 3:289762133fd6 381 draw = draw_house_floor;
trmontgomery 4:cdc54191ff07 382 }else{
trmontgomery 4:cdc54191ff07 383 draw = draw_nothing;
trmontgomery 3:289762133fd6 384 }
rconnorlawson 0:35660d7952f7 385 }
rconnorlawson 0:35660d7952f7 386 }
rconnorlawson 0:35660d7952f7 387 }
rconnorlawson 0:35660d7952f7 388 else if (init) // If doing a full draw, but we're out of bounds, draw the walls.
rconnorlawson 0:35660d7952f7 389 {
rconnorlawson 0:35660d7952f7 390 draw = draw_wall;
rconnorlawson 0:35660d7952f7 391 }
rconnorlawson 0:35660d7952f7 392
rconnorlawson 0:35660d7952f7 393 // Actually draw the tile
trmontgomery 2:0876296d9473 394 //MapItem* curr_item = get_here(x, y);
trmontgomery 2:0876296d9473 395 //MapItem* prev_item = get_here(px, py);
trmontgomery 2:0876296d9473 396 if (draw){
trmontgomery 2:0876296d9473 397 // if(curr_item){
trmontgomery 2:0876296d9473 398 // if(curr_item -> type == ROCK){
trmontgomery 2:0876296d9473 399 // draw(param1, param2);
trmontgomery 2:0876296d9473 400 //}
trmontgomery 2:0876296d9473 401 //}
trmontgomery 2:0876296d9473 402 draw(u,v);
trmontgomery 2:0876296d9473 403 }
rconnorlawson 0:35660d7952f7 404 }
rconnorlawson 0:35660d7952f7 405 }
rconnorlawson 0:35660d7952f7 406
rconnorlawson 0:35660d7952f7 407 // Draw status bars
trmontgomery 4:cdc54191ff07 408 draw_upper_status(p1.health);
trmontgomery 4:cdc54191ff07 409 draw_lower_status(p1.x, p1.y);
rconnorlawson 0:35660d7952f7 410 }
rconnorlawson 0:35660d7952f7 411
rconnorlawson 0:35660d7952f7 412
rconnorlawson 0:35660d7952f7 413 /**
rconnorlawson 0:35660d7952f7 414 * Initialize the main world map. Add walls around the edges, interior chambers,
rconnorlawson 0:35660d7952f7 415 * and plants in the background so you can see motion.
rconnorlawson 0:35660d7952f7 416 */
rconnorlawson 0:35660d7952f7 417 void init_main_map()
rconnorlawson 0:35660d7952f7 418 {
trmontgomery 4:cdc54191ff07 419 Map* map = set_active_map(0);
rconnorlawson 0:35660d7952f7 420 // "Random" plants
trmontgomery 3:289762133fd6 421
trmontgomery 3:289762133fd6 422 maps_init(50, 50, 5);
rconnorlawson 0:35660d7952f7 423 for(int i = map_width() + 3; i < map_area(); i += 39)
rconnorlawson 0:35660d7952f7 424 {
rconnorlawson 0:35660d7952f7 425 add_plant(i % map_width(), i / map_width());
rconnorlawson 0:35660d7952f7 426 }
trmontgomery 2:0876296d9473 427
trmontgomery 2:0876296d9473 428 srand(time(NULL));
trmontgomery 2:0876296d9473 429 for(int i = 0; i < 30; i++)
trmontgomery 2:0876296d9473 430 {
trmontgomery 2:0876296d9473 431 int x = rand()%((map_width()+1)-1) + 1;
trmontgomery 2:0876296d9473 432 int y = rand()%((map_height()+1)-1) + 1;
trmontgomery 2:0876296d9473 433 add_spike(x, y);
trmontgomery 2:0876296d9473 434 }
trmontgomery 2:0876296d9473 435
trmontgomery 4:cdc54191ff07 436 for(int i = 0; i < 30; i++)
trmontgomery 4:cdc54191ff07 437 {
trmontgomery 4:cdc54191ff07 438 int x = rand()%((map_width()+1)-1) + 1;
trmontgomery 4:cdc54191ff07 439 int y = rand()%((map_height()+1)-1) + 1;
trmontgomery 4:cdc54191ff07 440 add_gem(x, y, 1);
trmontgomery 4:cdc54191ff07 441 }
trmontgomery 4:cdc54191ff07 442
trmontgomery 4:cdc54191ff07 443 for(int i = 0; i < 15; i++)
trmontgomery 4:cdc54191ff07 444 {
trmontgomery 4:cdc54191ff07 445 int x = rand()%((map_width()+1)-1) + 1;
trmontgomery 4:cdc54191ff07 446 int y = rand()%((map_height()+1)-1) + 1;
trmontgomery 4:cdc54191ff07 447 add_gem(x, y, 2);
trmontgomery 4:cdc54191ff07 448 }
trmontgomery 4:cdc54191ff07 449
trmontgomery 4:cdc54191ff07 450 for(int i = 0; i < 9; i++)
trmontgomery 4:cdc54191ff07 451 {
trmontgomery 4:cdc54191ff07 452 int x = rand()%((map_width()+1)-1) + 1;
trmontgomery 4:cdc54191ff07 453 int y = rand()%((map_height()+1)-1) + 1;
trmontgomery 4:cdc54191ff07 454 add_gem(x, y, 3);
trmontgomery 4:cdc54191ff07 455 }
trmontgomery 4:cdc54191ff07 456
rconnorlawson 0:35660d7952f7 457 pc.printf("plants\r\n");
rconnorlawson 0:35660d7952f7 458
trmontgomery 4:cdc54191ff07 459 p1.x = 41;
trmontgomery 4:cdc54191ff07 460 p1.y = 10;
rconnorlawson 0:35660d7952f7 461 pc.printf("Adding walls!\r\n");
rconnorlawson 0:35660d7952f7 462 add_wall(0, 0, HORIZONTAL, map_width());
rconnorlawson 0:35660d7952f7 463 add_wall(0, map_height()-1, HORIZONTAL, map_width());
rconnorlawson 0:35660d7952f7 464 add_wall(0, 0, VERTICAL, map_height());
rconnorlawson 0:35660d7952f7 465 add_wall(map_width()-1, 0, VERTICAL, map_height());
trmontgomery 4:cdc54191ff07 466 add_rock(13, 2);
trmontgomery 4:cdc54191ff07 467 add_redhouse(9, 1, 1);
trmontgomery 4:cdc54191ff07 468 add_bush_rect(1, 19, 11, 38);
trmontgomery 4:cdc54191ff07 469 add_bush_rect(20, 17, 23, 21);
rconnorlawson 0:35660d7952f7 470 pc.printf("Walls done!\r\n");
rconnorlawson 0:35660d7952f7 471
rconnorlawson 0:35660d7952f7 472 print_map();
rconnorlawson 0:35660d7952f7 473 }
rconnorlawson 0:35660d7952f7 474
trmontgomery 3:289762133fd6 475 void init_house_map()
trmontgomery 3:289762133fd6 476 {
trmontgomery 3:289762133fd6 477
trmontgomery 3:289762133fd6 478 Map* map = set_active_map(1); //one is a house map
trmontgomery 4:cdc54191ff07 479 //in_house = 1;
trmontgomery 4:cdc54191ff07 480 maps_init(10, 10, 5);
trmontgomery 3:289762133fd6 481 add_wall(0, 0, HORIZONTAL, map_width());
trmontgomery 3:289762133fd6 482 add_wall(0, map_height()-1, HORIZONTAL, map_width());
trmontgomery 3:289762133fd6 483 add_wall(0, 0, VERTICAL, map_height());
trmontgomery 3:289762133fd6 484 add_wall(map_width()-1, 0, VERTICAL, map_height());
trmontgomery 4:cdc54191ff07 485
trmontgomery 4:cdc54191ff07 486 add_wall(1, 4, HORIZONTAL, 3);
trmontgomery 4:cdc54191ff07 487 add_wall(4, 4, VERTICAL, 4);
trmontgomery 4:cdc54191ff07 488 add_gdoor(4,8);
trmontgomery 4:cdc54191ff07 489
trmontgomery 4:cdc54191ff07 490 add_npc(6, 2); //place NPC in the house
trmontgomery 4:cdc54191ff07 491 add_door(8, 8);
trmontgomery 4:cdc54191ff07 492 add_goal(1, 8);
trmontgomery 3:289762133fd6 493 }
trmontgomery 3:289762133fd6 494
rconnorlawson 0:35660d7952f7 495 /**
rconnorlawson 0:35660d7952f7 496 * Program entry point! This is where it all begins.
rconnorlawson 0:35660d7952f7 497 * This function orchestrates all the parts of the game. Most of your
rconnorlawson 0:35660d7952f7 498 * implementation should be elsewhere - this holds the game loop, and should
rconnorlawson 0:35660d7952f7 499 * read like a road map for the rest of the code.
rconnorlawson 0:35660d7952f7 500 */
rconnorlawson 0:35660d7952f7 501 int main()
rconnorlawson 0:35660d7952f7 502 {
trmontgomery 4:cdc54191ff07 503 Menu start_m = Menu(3);
trmontgomery 4:cdc54191ff07 504 int start = start_m.display();
rconnorlawson 0:35660d7952f7 505 // First things first: initialize hardware
rconnorlawson 0:35660d7952f7 506 ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!");
rconnorlawson 0:35660d7952f7 507
rconnorlawson 0:35660d7952f7 508 // Initialize the maps
trmontgomery 3:289762133fd6 509
trmontgomery 4:cdc54191ff07 510 init_house_map();
rconnorlawson 0:35660d7952f7 511 init_main_map();
rconnorlawson 0:35660d7952f7 512
trmontgomery 3:289762133fd6 513
rconnorlawson 0:35660d7952f7 514 // Initialize game state
trmontgomery 3:289762133fd6 515 //set_active_map(0); //this doesn't need to be here twice
trmontgomery 4:cdc54191ff07 516 p1.health = 0;
trmontgomery 4:cdc54191ff07 517 p1.lives = 3;
trmontgomery 4:cdc54191ff07 518 p1.x = p1.y = 5;
trmontgomery 4:cdc54191ff07 519 p1.quest_complete = false;
rconnorlawson 0:35660d7952f7 520
rconnorlawson 0:35660d7952f7 521 // Initial drawing
rconnorlawson 0:35660d7952f7 522 draw_game(true);
rconnorlawson 0:35660d7952f7 523
rconnorlawson 0:35660d7952f7 524 // Main game loop
trmontgomery 4:cdc54191ff07 525 while(p1.lives)
rconnorlawson 0:35660d7952f7 526 {
rconnorlawson 0:35660d7952f7 527 // Timer to measure game update speed
rconnorlawson 0:35660d7952f7 528 Timer t; t.start();
rconnorlawson 0:35660d7952f7 529
trmontgomery 2:0876296d9473 530 // Actually do the game update:
trmontgomery 2:0876296d9473 531 // 1. Read inputs
trmontgomery 2:0876296d9473 532 GameInputs in = read_inputs();
trmontgomery 2:0876296d9473 533 // 2. Determine action (get_action)
trmontgomery 2:0876296d9473 534 int a = get_action(in);
rconnorlawson 0:35660d7952f7 535 // 3. Update game (update_game)
trmontgomery 2:0876296d9473 536 int u = update_game(a);
trmontgomery 4:cdc54191ff07 537 update_npcs();
rconnorlawson 0:35660d7952f7 538 // 3b. Check for game over
trmontgomery 4:cdc54191ff07 539 draw_game(u);
trmontgomery 4:cdc54191ff07 540 if (p1.health >= 120){
trmontgomery 4:cdc54191ff07 541 int result = gameover(--p1.lives);
trmontgomery 4:cdc54191ff07 542 if (result){
trmontgomery 4:cdc54191ff07 543 init_main_map();
trmontgomery 4:cdc54191ff07 544 p1.health = 0;
trmontgomery 4:cdc54191ff07 545 //Player.lives--;
trmontgomery 4:cdc54191ff07 546 p1.x = p1.y = 5;
trmontgomery 4:cdc54191ff07 547 p1.quest_complete = false;
trmontgomery 4:cdc54191ff07 548 }}
trmontgomery 2:0876296d9473 549 if (game_over == true){
trmontgomery 4:cdc54191ff07 550 break;
trmontgomery 4:cdc54191ff07 551 }
trmontgomery 4:cdc54191ff07 552
trmontgomery 4:cdc54191ff07 553 //else if(house_enabled == true){
trmontgomery 3:289762133fd6 554
trmontgomery 3:289762133fd6 555 //Player.x = Player.y = 5; //idk if this should be here...
trmontgomery 3:289762133fd6 556 //}
rconnorlawson 0:35660d7952f7 557 // 4. Draw frame (draw_game)
trmontgomery 4:cdc54191ff07 558
trmontgomery 3:289762133fd6 559
rconnorlawson 0:35660d7952f7 560
rconnorlawson 0:35660d7952f7 561 // 5. Frame delay
rconnorlawson 0:35660d7952f7 562 t.stop();
rconnorlawson 0:35660d7952f7 563 int dt = t.read_ms();
rconnorlawson 0:35660d7952f7 564 if (dt < 100) wait_ms(100 - dt);
rconnorlawson 0:35660d7952f7 565 }
trmontgomery 4:cdc54191ff07 566 draw_game_over();
rconnorlawson 0:35660d7952f7 567 }