Game For ECE 2035

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
nasiromar
Date:
Sat Nov 20 03:37:50 2021 +0000
Revision:
7:862062ffca62
Parent:
6:c9695079521d
Child:
8:fcc333a8f9e1
Base Model without game over

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