Adventure game written for ECE2035 at the Georgia Institute of Technology

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
trmontgomery
Date:
Sat Oct 26 15:44:26 2019 +0000
Revision:
5:93a4c396c1af
Parent:
4:cdc54191ff07
test

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 5:93a4c396c1af 8 #include "pausemenu.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 5:93a4c396c1af 312 PauseMenu pause_m(&p1);
trmontgomery 5:93a4c396c1af 313 pause_m.display();
trmontgomery 5:93a4c396c1af 314 switch_axis = pause_m.get_config();
trmontgomery 5:93a4c396c1af 315 //switch_axis = ingamemenu(&p1);
trmontgomery 2:0876296d9473 316
trmontgomery 2:0876296d9473 317 break;
trmontgomery 4:cdc54191ff07 318 default: break;
rconnorlawson 0:35660d7952f7 319 }
rconnorlawson 0:35660d7952f7 320 return NO_RESULT;
rconnorlawson 0:35660d7952f7 321 }
rconnorlawson 0:35660d7952f7 322
trmontgomery 4:cdc54191ff07 323
trmontgomery 4:cdc54191ff07 324
rconnorlawson 0:35660d7952f7 325 /**
rconnorlawson 0:35660d7952f7 326 * Entry point for frame drawing. This should be called once per iteration of
rconnorlawson 0:35660d7952f7 327 * the game loop. This draws all tiles on the screen, followed by the status
rconnorlawson 0:35660d7952f7 328 * bars. Unless init is nonzero, this function will optimize drawing by only
rconnorlawson 0:35660d7952f7 329 * drawing tiles that have changed from the previous frame.
rconnorlawson 0:35660d7952f7 330 */
rconnorlawson 0:35660d7952f7 331 void draw_game(int init)
rconnorlawson 0:35660d7952f7 332 {
trmontgomery 2:0876296d9473 333 // int param1;
trmontgomery 2:0876296d9473 334 //int param2;
rconnorlawson 0:35660d7952f7 335 // Draw game border first
rconnorlawson 0:35660d7952f7 336 if(init) draw_border();
rconnorlawson 0:35660d7952f7 337
rconnorlawson 0:35660d7952f7 338 // Iterate over all visible map tiles
rconnorlawson 0:35660d7952f7 339 for (int i = -5; i <= 5; i++) // Iterate over columns of tiles
rconnorlawson 0:35660d7952f7 340 {
rconnorlawson 0:35660d7952f7 341 for (int j = -4; j <= 4; j++) // Iterate over one column of tiles
rconnorlawson 0:35660d7952f7 342 {
rconnorlawson 0:35660d7952f7 343 // Here, we have a given (i,j)
rconnorlawson 0:35660d7952f7 344
rconnorlawson 0:35660d7952f7 345 // Compute the current map (x,y) of this tile
trmontgomery 4:cdc54191ff07 346 int x = i + p1.x;
trmontgomery 4:cdc54191ff07 347 int y = j + p1.y;
rconnorlawson 0:35660d7952f7 348
rconnorlawson 0:35660d7952f7 349 // Compute the previous map (px, py) of this tile
trmontgomery 4:cdc54191ff07 350 int px = i + p1.px;
trmontgomery 4:cdc54191ff07 351 int py = j + p1.py;
rconnorlawson 0:35660d7952f7 352
rconnorlawson 0:35660d7952f7 353 // Compute u,v coordinates for drawing
rconnorlawson 0:35660d7952f7 354 int u = (i+5)*11 + 3;
rconnorlawson 0:35660d7952f7 355 int v = (j+4)*11 + 15;
rconnorlawson 0:35660d7952f7 356
rconnorlawson 0:35660d7952f7 357 // Figure out what to draw
rconnorlawson 0:35660d7952f7 358 DrawFunc draw = NULL;
trmontgomery 4:cdc54191ff07 359 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 360 {
trmontgomery 4:cdc54191ff07 361 draw_player(u, v, p1.has_key);
trmontgomery 2:0876296d9473 362
rconnorlawson 0:35660d7952f7 363 continue;
rconnorlawson 0:35660d7952f7 364 }
rconnorlawson 0:35660d7952f7 365 else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) // Current (i,j) in the map
rconnorlawson 0:35660d7952f7 366 {
rconnorlawson 0:35660d7952f7 367 MapItem* curr_item = get_here(x, y);
rconnorlawson 0:35660d7952f7 368 MapItem* prev_item = get_here(px, py);
rconnorlawson 0:35660d7952f7 369 if (init || curr_item != prev_item) // Only draw if they're different
rconnorlawson 0:35660d7952f7 370 {
rconnorlawson 0:35660d7952f7 371 if (curr_item) // There's something here! Draw it
rconnorlawson 0:35660d7952f7 372 {
trmontgomery 2:0876296d9473 373 //if (curr_item -> type == ROCK){
trmontgomery 2:0876296d9473 374 //set parameters
trmontgomery 2:0876296d9473 375 // Rock* r = (Rock*) curr_item -> data;
trmontgomery 2:0876296d9473 376 //param1 = r -> x;
trmontgomery 2:0876296d9473 377 //param2 = r -> y;
trmontgomery 2:0876296d9473 378 //}
rconnorlawson 0:35660d7952f7 379 draw = curr_item->draw;
rconnorlawson 0:35660d7952f7 380 }
rconnorlawson 0:35660d7952f7 381 else // There used to be something, but now there isn't
rconnorlawson 0:35660d7952f7 382 {
trmontgomery 4:cdc54191ff07 383 if (in_house){
trmontgomery 3:289762133fd6 384 draw = draw_house_floor;
trmontgomery 4:cdc54191ff07 385 }else{
trmontgomery 4:cdc54191ff07 386 draw = draw_nothing;
trmontgomery 3:289762133fd6 387 }
rconnorlawson 0:35660d7952f7 388 }
rconnorlawson 0:35660d7952f7 389 }
rconnorlawson 0:35660d7952f7 390 }
rconnorlawson 0:35660d7952f7 391 else if (init) // If doing a full draw, but we're out of bounds, draw the walls.
rconnorlawson 0:35660d7952f7 392 {
rconnorlawson 0:35660d7952f7 393 draw = draw_wall;
rconnorlawson 0:35660d7952f7 394 }
rconnorlawson 0:35660d7952f7 395
rconnorlawson 0:35660d7952f7 396 // Actually draw the tile
trmontgomery 2:0876296d9473 397 //MapItem* curr_item = get_here(x, y);
trmontgomery 2:0876296d9473 398 //MapItem* prev_item = get_here(px, py);
trmontgomery 2:0876296d9473 399 if (draw){
trmontgomery 2:0876296d9473 400 // if(curr_item){
trmontgomery 2:0876296d9473 401 // if(curr_item -> type == ROCK){
trmontgomery 2:0876296d9473 402 // draw(param1, param2);
trmontgomery 2:0876296d9473 403 //}
trmontgomery 2:0876296d9473 404 //}
trmontgomery 2:0876296d9473 405 draw(u,v);
trmontgomery 2:0876296d9473 406 }
rconnorlawson 0:35660d7952f7 407 }
rconnorlawson 0:35660d7952f7 408 }
rconnorlawson 0:35660d7952f7 409
rconnorlawson 0:35660d7952f7 410 // Draw status bars
trmontgomery 4:cdc54191ff07 411 draw_upper_status(p1.health);
trmontgomery 4:cdc54191ff07 412 draw_lower_status(p1.x, p1.y);
rconnorlawson 0:35660d7952f7 413 }
rconnorlawson 0:35660d7952f7 414
rconnorlawson 0:35660d7952f7 415
rconnorlawson 0:35660d7952f7 416 /**
rconnorlawson 0:35660d7952f7 417 * Initialize the main world map. Add walls around the edges, interior chambers,
rconnorlawson 0:35660d7952f7 418 * and plants in the background so you can see motion.
rconnorlawson 0:35660d7952f7 419 */
rconnorlawson 0:35660d7952f7 420 void init_main_map()
rconnorlawson 0:35660d7952f7 421 {
trmontgomery 4:cdc54191ff07 422 Map* map = set_active_map(0);
rconnorlawson 0:35660d7952f7 423 // "Random" plants
trmontgomery 3:289762133fd6 424
trmontgomery 3:289762133fd6 425 maps_init(50, 50, 5);
rconnorlawson 0:35660d7952f7 426 for(int i = map_width() + 3; i < map_area(); i += 39)
rconnorlawson 0:35660d7952f7 427 {
rconnorlawson 0:35660d7952f7 428 add_plant(i % map_width(), i / map_width());
rconnorlawson 0:35660d7952f7 429 }
trmontgomery 2:0876296d9473 430
trmontgomery 2:0876296d9473 431 srand(time(NULL));
trmontgomery 2:0876296d9473 432 for(int i = 0; i < 30; i++)
trmontgomery 2:0876296d9473 433 {
trmontgomery 2:0876296d9473 434 int x = rand()%((map_width()+1)-1) + 1;
trmontgomery 2:0876296d9473 435 int y = rand()%((map_height()+1)-1) + 1;
trmontgomery 2:0876296d9473 436 add_spike(x, y);
trmontgomery 2:0876296d9473 437 }
trmontgomery 2:0876296d9473 438
trmontgomery 4:cdc54191ff07 439 for(int i = 0; i < 30; i++)
trmontgomery 4:cdc54191ff07 440 {
trmontgomery 4:cdc54191ff07 441 int x = rand()%((map_width()+1)-1) + 1;
trmontgomery 4:cdc54191ff07 442 int y = rand()%((map_height()+1)-1) + 1;
trmontgomery 4:cdc54191ff07 443 add_gem(x, y, 1);
trmontgomery 4:cdc54191ff07 444 }
trmontgomery 4:cdc54191ff07 445
trmontgomery 4:cdc54191ff07 446 for(int i = 0; i < 15; i++)
trmontgomery 4:cdc54191ff07 447 {
trmontgomery 4:cdc54191ff07 448 int x = rand()%((map_width()+1)-1) + 1;
trmontgomery 4:cdc54191ff07 449 int y = rand()%((map_height()+1)-1) + 1;
trmontgomery 4:cdc54191ff07 450 add_gem(x, y, 2);
trmontgomery 4:cdc54191ff07 451 }
trmontgomery 4:cdc54191ff07 452
trmontgomery 4:cdc54191ff07 453 for(int i = 0; i < 9; i++)
trmontgomery 4:cdc54191ff07 454 {
trmontgomery 4:cdc54191ff07 455 int x = rand()%((map_width()+1)-1) + 1;
trmontgomery 4:cdc54191ff07 456 int y = rand()%((map_height()+1)-1) + 1;
trmontgomery 4:cdc54191ff07 457 add_gem(x, y, 3);
trmontgomery 4:cdc54191ff07 458 }
trmontgomery 4:cdc54191ff07 459
rconnorlawson 0:35660d7952f7 460 pc.printf("plants\r\n");
rconnorlawson 0:35660d7952f7 461
trmontgomery 4:cdc54191ff07 462 p1.x = 41;
trmontgomery 4:cdc54191ff07 463 p1.y = 10;
rconnorlawson 0:35660d7952f7 464 pc.printf("Adding walls!\r\n");
rconnorlawson 0:35660d7952f7 465 add_wall(0, 0, HORIZONTAL, map_width());
rconnorlawson 0:35660d7952f7 466 add_wall(0, map_height()-1, HORIZONTAL, map_width());
rconnorlawson 0:35660d7952f7 467 add_wall(0, 0, VERTICAL, map_height());
rconnorlawson 0:35660d7952f7 468 add_wall(map_width()-1, 0, VERTICAL, map_height());
trmontgomery 4:cdc54191ff07 469 add_rock(13, 2);
trmontgomery 4:cdc54191ff07 470 add_redhouse(9, 1, 1);
trmontgomery 4:cdc54191ff07 471 add_bush_rect(1, 19, 11, 38);
trmontgomery 4:cdc54191ff07 472 add_bush_rect(20, 17, 23, 21);
rconnorlawson 0:35660d7952f7 473 pc.printf("Walls done!\r\n");
rconnorlawson 0:35660d7952f7 474
rconnorlawson 0:35660d7952f7 475 print_map();
rconnorlawson 0:35660d7952f7 476 }
rconnorlawson 0:35660d7952f7 477
trmontgomery 3:289762133fd6 478 void init_house_map()
trmontgomery 3:289762133fd6 479 {
trmontgomery 3:289762133fd6 480
trmontgomery 3:289762133fd6 481 Map* map = set_active_map(1); //one is a house map
trmontgomery 4:cdc54191ff07 482 //in_house = 1;
trmontgomery 4:cdc54191ff07 483 maps_init(10, 10, 5);
trmontgomery 3:289762133fd6 484 add_wall(0, 0, HORIZONTAL, map_width());
trmontgomery 3:289762133fd6 485 add_wall(0, map_height()-1, HORIZONTAL, map_width());
trmontgomery 3:289762133fd6 486 add_wall(0, 0, VERTICAL, map_height());
trmontgomery 3:289762133fd6 487 add_wall(map_width()-1, 0, VERTICAL, map_height());
trmontgomery 4:cdc54191ff07 488
trmontgomery 4:cdc54191ff07 489 add_wall(1, 4, HORIZONTAL, 3);
trmontgomery 4:cdc54191ff07 490 add_wall(4, 4, VERTICAL, 4);
trmontgomery 4:cdc54191ff07 491 add_gdoor(4,8);
trmontgomery 4:cdc54191ff07 492
trmontgomery 4:cdc54191ff07 493 add_npc(6, 2); //place NPC in the house
trmontgomery 4:cdc54191ff07 494 add_door(8, 8);
trmontgomery 4:cdc54191ff07 495 add_goal(1, 8);
trmontgomery 3:289762133fd6 496 }
trmontgomery 3:289762133fd6 497
rconnorlawson 0:35660d7952f7 498 /**
rconnorlawson 0:35660d7952f7 499 * Program entry point! This is where it all begins.
rconnorlawson 0:35660d7952f7 500 * This function orchestrates all the parts of the game. Most of your
rconnorlawson 0:35660d7952f7 501 * implementation should be elsewhere - this holds the game loop, and should
rconnorlawson 0:35660d7952f7 502 * read like a road map for the rest of the code.
rconnorlawson 0:35660d7952f7 503 */
rconnorlawson 0:35660d7952f7 504 int main()
rconnorlawson 0:35660d7952f7 505 {
trmontgomery 5:93a4c396c1af 506
trmontgomery 5:93a4c396c1af 507 StartMenu start_m = StartMenu();
trmontgomery 5:93a4c396c1af 508 int st = start_m.display();
rconnorlawson 0:35660d7952f7 509 // First things first: initialize hardware
rconnorlawson 0:35660d7952f7 510 ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!");
rconnorlawson 0:35660d7952f7 511
rconnorlawson 0:35660d7952f7 512 // Initialize the maps
trmontgomery 3:289762133fd6 513
trmontgomery 4:cdc54191ff07 514 init_house_map();
rconnorlawson 0:35660d7952f7 515 init_main_map();
rconnorlawson 0:35660d7952f7 516
trmontgomery 3:289762133fd6 517
rconnorlawson 0:35660d7952f7 518 // Initialize game state
trmontgomery 3:289762133fd6 519 //set_active_map(0); //this doesn't need to be here twice
trmontgomery 4:cdc54191ff07 520 p1.health = 0;
trmontgomery 4:cdc54191ff07 521 p1.lives = 3;
trmontgomery 4:cdc54191ff07 522 p1.x = p1.y = 5;
trmontgomery 4:cdc54191ff07 523 p1.quest_complete = false;
rconnorlawson 0:35660d7952f7 524
rconnorlawson 0:35660d7952f7 525 // Initial drawing
rconnorlawson 0:35660d7952f7 526 draw_game(true);
rconnorlawson 0:35660d7952f7 527
rconnorlawson 0:35660d7952f7 528 // Main game loop
trmontgomery 4:cdc54191ff07 529 while(p1.lives)
rconnorlawson 0:35660d7952f7 530 {
rconnorlawson 0:35660d7952f7 531 // Timer to measure game update speed
rconnorlawson 0:35660d7952f7 532 Timer t; t.start();
rconnorlawson 0:35660d7952f7 533
trmontgomery 2:0876296d9473 534 // Actually do the game update:
trmontgomery 2:0876296d9473 535 // 1. Read inputs
trmontgomery 2:0876296d9473 536 GameInputs in = read_inputs();
trmontgomery 2:0876296d9473 537 // 2. Determine action (get_action)
trmontgomery 2:0876296d9473 538 int a = get_action(in);
rconnorlawson 0:35660d7952f7 539 // 3. Update game (update_game)
trmontgomery 2:0876296d9473 540 int u = update_game(a);
trmontgomery 4:cdc54191ff07 541 update_npcs();
rconnorlawson 0:35660d7952f7 542 // 3b. Check for game over
trmontgomery 4:cdc54191ff07 543 draw_game(u);
trmontgomery 4:cdc54191ff07 544 if (p1.health >= 120){
trmontgomery 4:cdc54191ff07 545 int result = gameover(--p1.lives);
trmontgomery 4:cdc54191ff07 546 if (result){
trmontgomery 4:cdc54191ff07 547 init_main_map();
trmontgomery 4:cdc54191ff07 548 p1.health = 0;
trmontgomery 4:cdc54191ff07 549 //Player.lives--;
trmontgomery 4:cdc54191ff07 550 p1.x = p1.y = 5;
trmontgomery 4:cdc54191ff07 551 p1.quest_complete = false;
trmontgomery 4:cdc54191ff07 552 }}
trmontgomery 2:0876296d9473 553 if (game_over == true){
trmontgomery 4:cdc54191ff07 554 break;
trmontgomery 4:cdc54191ff07 555 }
trmontgomery 4:cdc54191ff07 556
trmontgomery 4:cdc54191ff07 557 //else if(house_enabled == true){
trmontgomery 3:289762133fd6 558
trmontgomery 3:289762133fd6 559 //Player.x = Player.y = 5; //idk if this should be here...
trmontgomery 3:289762133fd6 560 //}
rconnorlawson 0:35660d7952f7 561 // 4. Draw frame (draw_game)
trmontgomery 4:cdc54191ff07 562
trmontgomery 3:289762133fd6 563
rconnorlawson 0:35660d7952f7 564
rconnorlawson 0:35660d7952f7 565 // 5. Frame delay
rconnorlawson 0:35660d7952f7 566 t.stop();
rconnorlawson 0:35660d7952f7 567 int dt = t.read_ms();
rconnorlawson 0:35660d7952f7 568 if (dt < 100) wait_ms(100 - dt);
rconnorlawson 0:35660d7952f7 569 }
trmontgomery 4:cdc54191ff07 570 draw_game_over();
rconnorlawson 0:35660d7952f7 571 }