Game For ECE 2035

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
nasiromar
Date:
Fri Nov 19 22:03:25 2021 +0000
Revision:
6:c9695079521d
Parent:
5:2f34484325bc
Child:
7:862062ffca62
Basics

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 6:c9695079521d 142 if(east||west||north||south->type == NPC){
nasiromar 6:c9695079521d 143 npc_speech1();
nasiromar 6:c9695079521d 144 }
nasiromar 6:c9695079521d 145
nasiromar 6:c9695079521d 146 }
nasiromar 6:c9695079521d 147 break;
nasiromar 6:c9695079521d 148 case MENU_BUTTON: {
nasiromar 6:c9695079521d 149 init_spells();
nasiromar 6:c9695079521d 150 Player.spell = spell();
nasiromar 6:c9695079521d 151 }
nasiromar 6:c9695079521d 152 break;
nasiromar 6:c9695079521d 153 case GOD_MODE: {
nasiromar 6:c9695079521d 154
nasiromar 6:c9695079521d 155 Player.god = !Player.god;
nasiromar 6:c9695079521d 156 }
nasiromar 6:c9695079521d 157 break;
nasiromar 6:c9695079521d 158
nasiromar 6:c9695079521d 159 default:
nasiromar 6:c9695079521d 160
nasiromar 6:c9695079521d 161 break;
rconnorlawson 0:35660d7952f7 162 }
rconnorlawson 0:35660d7952f7 163 return NO_RESULT;
rconnorlawson 0:35660d7952f7 164 }
rconnorlawson 0:35660d7952f7 165
rconnorlawson 0:35660d7952f7 166 /**
rconnorlawson 0:35660d7952f7 167 * Entry point for frame drawing. This should be called once per iteration of
nasiromar 6:c9695079521d 168 * the game loop. This draws all tiles on the screen, followed by the status
nasiromar 6:c9695079521d 169 * bars. Unless init is nonzero, this function will optimize drawing by only
rconnorlawson 0:35660d7952f7 170 * drawing tiles that have changed from the previous frame.
rconnorlawson 0:35660d7952f7 171 */
rconnorlawson 0:35660d7952f7 172 void draw_game(int init)
rconnorlawson 0:35660d7952f7 173 {
rconnorlawson 0:35660d7952f7 174 // Draw game border first
rconnorlawson 0:35660d7952f7 175 if(init) draw_border();
nasiromar 6:c9695079521d 176
rconnorlawson 0:35660d7952f7 177 // Iterate over all visible map tiles
nasiromar 6:c9695079521d 178 for (int i = -5; i <= 5; i++) { // Iterate over columns of tiles
nasiromar 6:c9695079521d 179 for (int j = -4; j <= 4; j++) { // Iterate over one column of tiles
rconnorlawson 0:35660d7952f7 180 // Here, we have a given (i,j)
nasiromar 6:c9695079521d 181
rconnorlawson 0:35660d7952f7 182 // Compute the current map (x,y) of this tile
rconnorlawson 0:35660d7952f7 183 int x = i + Player.x;
rconnorlawson 0:35660d7952f7 184 int y = j + Player.y;
nasiromar 6:c9695079521d 185
rconnorlawson 0:35660d7952f7 186 // Compute the previous map (px, py) of this tile
rconnorlawson 0:35660d7952f7 187 int px = i + Player.px;
rconnorlawson 0:35660d7952f7 188 int py = j + Player.py;
nasiromar 6:c9695079521d 189
rconnorlawson 0:35660d7952f7 190 // Compute u,v coordinates for drawing
rconnorlawson 0:35660d7952f7 191 int u = (i+5)*11 + 3;
rconnorlawson 0:35660d7952f7 192 int v = (j+4)*11 + 15;
nasiromar 6:c9695079521d 193
rconnorlawson 0:35660d7952f7 194 // Figure out what to draw
rconnorlawson 0:35660d7952f7 195 DrawFunc draw = NULL;
nasiromar 6:c9695079521d 196 if (init && i == 0 && j == 0) { // Only draw the player on init
rconnorlawson 0:35660d7952f7 197 draw_player(u, v, Player.has_key);
rconnorlawson 0:35660d7952f7 198 continue;
nasiromar 6:c9695079521d 199 } else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) { // Current (i,j) in the map
rconnorlawson 0:35660d7952f7 200 MapItem* curr_item = get_here(x, y);
rconnorlawson 0:35660d7952f7 201 MapItem* prev_item = get_here(px, py);
nasiromar 6:c9695079521d 202 if (init || curr_item != prev_item) { // Only draw if they're different
nasiromar 6:c9695079521d 203 if (curr_item) { // There's something here! Draw it
rconnorlawson 0:35660d7952f7 204 draw = curr_item->draw;
nasiromar 6:c9695079521d 205 } else { // There used to be something, but now there isn't
rconnorlawson 0:35660d7952f7 206 draw = draw_nothing;
rconnorlawson 0:35660d7952f7 207 }
rconnorlawson 0:35660d7952f7 208 }
nasiromar 6:c9695079521d 209 } else if (init) { // If doing a full draw, but we're out of bounds, draw the walls.
rconnorlawson 0:35660d7952f7 210 draw = draw_wall;
rconnorlawson 0:35660d7952f7 211 }
rconnorlawson 0:35660d7952f7 212
rconnorlawson 0:35660d7952f7 213 // Actually draw the tile
rconnorlawson 0:35660d7952f7 214 if (draw) draw(u, v);
rconnorlawson 0:35660d7952f7 215 }
rconnorlawson 0:35660d7952f7 216 }
rconnorlawson 0:35660d7952f7 217
nasiromar 6:c9695079521d 218 // Draw status bars
nasiromar 6:c9695079521d 219 draw_upper_status(Player.x,Player.y);
nasiromar 6:c9695079521d 220 draw_lower_status(HP,MP);
rconnorlawson 0:35660d7952f7 221 }
rconnorlawson 0:35660d7952f7 222
rconnorlawson 0:35660d7952f7 223
rconnorlawson 0:35660d7952f7 224 /**
rconnorlawson 0:35660d7952f7 225 * Initialize the main world map. Add walls around the edges, interior chambers,
rashidi1saeed 2:1ca97843a334 226 * and plants in the background so you can see motion. Note: using the similar
rashidi1saeed 3:e4690ad5a4d2 227 * procedure you can init the secondary map(s).
rconnorlawson 0:35660d7952f7 228 */
rconnorlawson 0:35660d7952f7 229 void init_main_map()
rconnorlawson 0:35660d7952f7 230 {
rconnorlawson 0:35660d7952f7 231 // "Random" plants
rconnorlawson 0:35660d7952f7 232 Map* map = set_active_map(0);
nasiromar 6:c9695079521d 233 for(int i = map_width() + 3; i < map_area(); i += 39) {
rconnorlawson 0:35660d7952f7 234 add_plant(i % map_width(), i / map_width());
rconnorlawson 0:35660d7952f7 235 }
rconnorlawson 0:35660d7952f7 236 pc.printf("plants\r\n");
nasiromar 6:c9695079521d 237
rconnorlawson 0:35660d7952f7 238 pc.printf("Adding walls!\r\n");
rconnorlawson 0:35660d7952f7 239 add_wall(0, 0, HORIZONTAL, map_width());
rconnorlawson 0:35660d7952f7 240 add_wall(0, map_height()-1, HORIZONTAL, map_width());
rconnorlawson 0:35660d7952f7 241 add_wall(0, 0, VERTICAL, map_height());
rconnorlawson 0:35660d7952f7 242 add_wall(map_width()-1, 0, VERTICAL, map_height());
rconnorlawson 0:35660d7952f7 243 pc.printf("Walls done!\r\n");
rconnorlawson 0:35660d7952f7 244
nasiromar 6:c9695079521d 245 add_npc(9,9);
nasiromar 6:c9695079521d 246
nasiromar 6:c9695079521d 247 add_chest(10,10);
nasiromar 6:c9695079521d 248
nasiromar 6:c9695079521d 249 add_castle(15,15);
nasiromar 6:c9695079521d 250
nasiromar 6:c9695079521d 251 add_portal(25,30);
nasiromar 6:c9695079521d 252
rconnorlawson 0:35660d7952f7 253 print_map();
rconnorlawson 0:35660d7952f7 254 }
rconnorlawson 0:35660d7952f7 255
nasiromar 6:c9695079521d 256 //void init_dung
nasiromar 6:c9695079521d 257
rconnorlawson 0:35660d7952f7 258 /**
rconnorlawson 0:35660d7952f7 259 * Program entry point! This is where it all begins.
rconnorlawson 0:35660d7952f7 260 * This function orchestrates all the parts of the game. Most of your
rconnorlawson 0:35660d7952f7 261 * implementation should be elsewhere - this holds the game loop, and should
rconnorlawson 0:35660d7952f7 262 * read like a road map for the rest of the code.
rconnorlawson 0:35660d7952f7 263 */
rconnorlawson 0:35660d7952f7 264 int main()
rconnorlawson 0:35660d7952f7 265 {
rconnorlawson 0:35660d7952f7 266 // First things first: initialize hardware
rconnorlawson 0:35660d7952f7 267 ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!");
nasiromar 6:c9695079521d 268
rconnorlawson 0:35660d7952f7 269 // Initialize the maps
rconnorlawson 0:35660d7952f7 270 maps_init();
rconnorlawson 0:35660d7952f7 271 init_main_map();
nasiromar 6:c9695079521d 272
rconnorlawson 0:35660d7952f7 273 // Initialize game state
rconnorlawson 0:35660d7952f7 274 set_active_map(0);
rconnorlawson 0:35660d7952f7 275 Player.x = Player.y = 5;
rconnorlawson 0:35660d7952f7 276
rconnorlawson 0:35660d7952f7 277 // Initial drawing
rconnorlawson 0:35660d7952f7 278 draw_game(true);
rconnorlawson 0:35660d7952f7 279 // Main game loop
nasiromar 6:c9695079521d 280 while(1) {
rconnorlawson 0:35660d7952f7 281 // Timer to measure game update speed
nasiromar 6:c9695079521d 282 Timer t;
nasiromar 6:c9695079521d 283 t.start();
nasiromar 6:c9695079521d 284
lballard9 5:2f34484325bc 285 // Actually do the game update:
nasiromar 6:c9695079521d 286 // 1. Read inputs
nasiromar 6:c9695079521d 287 GameInputs input = read_inputs();
nasiromar 6:c9695079521d 288 // 2. Determine action (get_action)
nasiromar 6:c9695079521d 289 int action = get_action(input);
rconnorlawson 0:35660d7952f7 290 // 3. Update game (update_game)
nasiromar 6:c9695079521d 291 update_game(action);
rconnorlawson 0:35660d7952f7 292 // 3b. Check for game over
rconnorlawson 0:35660d7952f7 293 // 4. Draw frame (draw_game)
lballard9 5:2f34484325bc 294 draw_game(true);
nasiromar 6:c9695079521d 295
rconnorlawson 0:35660d7952f7 296 // 5. Frame delay
rconnorlawson 0:35660d7952f7 297 t.stop();
rconnorlawson 0:35660d7952f7 298 int dt = t.read_ms();
rconnorlawson 0:35660d7952f7 299 if (dt < 100) wait_ms(100 - dt);
rconnorlawson 0:35660d7952f7 300 }
nasiromar 6:c9695079521d 301 //
nasiromar 6:c9695079521d 302
rconnorlawson 0:35660d7952f7 303 }