Game For ECE 2035

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Revision:
6:c9695079521d
Parent:
5:2f34484325bc
Child:
7:862062ffca62
--- a/main.cpp	Tue Oct 12 15:26:32 2021 +0000
+++ b/main.cpp	Fri Nov 19 22:03:25 2021 +0000
@@ -4,6 +4,8 @@
 #include "map.h"
 #include "graphics.h"
 #include "speech.h"
+#include "spells.h"
+#include "actions.h"
 
 // Functions in this file
 int get_action (GameInputs inputs);
@@ -20,6 +22,8 @@
     int x,y;    // Current locations
     int px, py; // Previous locations
     int has_key;
+    int spell;
+    int god;
     // You can add other properties for the player here
 } Player;
 
@@ -34,20 +38,56 @@
 #define GO_RIGHT 4
 #define GO_UP 5
 #define GO_DOWN 6
+#define GOD_MODE 7
+
+#define TILT .25
 int get_action(GameInputs inputs)
 {
-    return NO_ACTION;
+
+    if(inputs.ay > TILT) {      //Go in north direction if it is not blocked
+        return GO_UP;
+    }
+    if(inputs.ay < TILT *-1) {     //Go in south direction if it is not blocked
+        return GO_DOWN;
+    }
+    if(inputs.ax > TILT) {      //Go in north direction if it is not blocked
+        return GO_RIGHT;
+    }
+    if(inputs.ax < TILT *-1) {     //Go in north direction if it is not blocked
+        return GO_LEFT;
+    }
+    if(!inputs.b1) {
+        wait(.5);
+        return ACTION_BUTTON;
+    }
+    if(!inputs.b2) {
+        wait(.5);
+        return MENU_BUTTON;
+    }
+    if (!inputs.b3) {
+        wait(.5);
+        return GOD_MODE;
+    } else {
+        return NO_ACTION;
+    }
 }
 
+
 /**
  * Update the game state based on the user action. For example, if the user
  * requests GO_UP, then this function should determine if that is possible by
  * consulting the map, and update the Player position accordingly.
- * 
+ *
  * Return values are defined below. FULL_DRAW indicates that for this frame,
  * draw_game should not optimize drawing and should draw every tile, even if
  * the player has not moved.
  */
+// Gmae Life and Magic Points
+#define HP 100
+#define MP 100
+
+
+
 #define NO_RESULT 0
 #define GAME_OVER 1
 #define FULL_DRAW 2
@@ -56,77 +96,117 @@
     // Save player previous location before updating
     Player.px = Player.x;
     Player.py = Player.y;
-    
+
+
+
     // Do different things based on the each action.
     // You can define functions like "go_up()" that get called for each case.
-    switch(action)
-    {
-        case GO_UP:     break;
-        case GO_LEFT:   break;            
-        case GO_DOWN:   break;
-        case GO_RIGHT:  break;
-        case ACTION_BUTTON: break;
-        case MENU_BUTTON: break;
-        default:        break;
+
+    // Need to check if Claue has changed as well if whats in front of me is able to be walked into
+    switch(action) {
+        case GO_UP: {
+            MapItem* north = get_north(Player.x,Player.y);
+            if (north->walkable && north == NULL || north->type == (PORTAL || PLANT) && Player.y != 0 || Player.god ) {
+                Player.y--;
+            }
+            break;
+        }
+        case GO_RIGHT: {
+            MapItem* east = get_east(Player.x,Player.y);
+            if (east->walkable && east == NULL || east->type == (PORTAL || PLANT) && Player.x != 0 || Player.god ) {
+                Player.x++;
+            }
+            break;
+        }
+        case GO_DOWN:  {
+            MapItem* south = get_south(Player.x,Player.y);
+            if (south->walkable && south == NULL || south->type ==(PORTAL || PLANT) && Player.y != 0 || Player.god ) {
+                Player.y++;
+            }
+            break;
+        }
+        case GO_LEFT: {
+            MapItem* west = get_west(Player.x,Player.y);
+            if (west->walkable && west == NULL || west->type == (PORTAL || PLANT) && Player.x != 0 || Player.god  ) {
+                Player.x--;
+            }
+            break;
+        }
+        case ACTION_BUTTON: {
+            MapItem* west = get_west(Player.x,Player.y);
+            MapItem* south = get_south(Player.x,Player.y);
+            MapItem* north = get_north(Player.x,Player.y);
+            MapItem* east = get_east(Player.x,Player.y);
+            MapItem* curr =  get_here(Player.x,Player.y);
+            
+            if(east||west||north||south->type == NPC){
+                npc_speech1();
+                }
+            
+        }
+        break;
+        case MENU_BUTTON: {
+            init_spells();
+            Player.spell = spell();
+        }
+        break;
+        case GOD_MODE: {
+
+            Player.god = !Player.god;
+        }
+        break;
+
+        default:
+
+            break;
     }
     return NO_RESULT;
 }
 
 /**
  * Entry point for frame drawing. This should be called once per iteration of
- * the game loop. This draws all tiles on the screen, followed by the status 
- * bars. Unless init is nonzero, this function will optimize drawing by only 
+ * the game loop. This draws all tiles on the screen, followed by the status
+ * bars. Unless init is nonzero, this function will optimize drawing by only
  * drawing tiles that have changed from the previous frame.
  */
 void draw_game(int init)
 {
     // Draw game border first
     if(init) draw_border();
-    
+
     // Iterate over all visible map tiles
-    for (int i = -5; i <= 5; i++) // Iterate over columns of tiles
-    {
-        for (int j = -4; j <= 4; j++) // Iterate over one column of tiles
-        {
+    for (int i = -5; i <= 5; i++) { // Iterate over columns of tiles
+        for (int j = -4; j <= 4; j++) { // Iterate over one column of tiles
             // Here, we have a given (i,j)
-            
+
             // Compute the current map (x,y) of this tile
             int x = i + Player.x;
             int y = j + Player.y;
-            
+
             // Compute the previous map (px, py) of this tile
             int px = i + Player.px;
             int py = j + Player.py;
-                        
+
             // Compute u,v coordinates for drawing
             int u = (i+5)*11 + 3;
             int v = (j+4)*11 + 15;
-            
+
             // Figure out what to draw
             DrawFunc draw = NULL;
-            if (init && i == 0 && j == 0) // Only draw the player on init
-            {
+            if (init && i == 0 && j == 0) { // Only draw the player on init
                 draw_player(u, v, Player.has_key);
                 continue;
-            }
-            else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) // Current (i,j) in the map
-            {
+            } else if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) { // Current (i,j) in the map
                 MapItem* curr_item = get_here(x, y);
                 MapItem* prev_item = get_here(px, py);
-                if (init || curr_item != prev_item) // Only draw if they're different
-                {
-                    if (curr_item) // There's something here! Draw it
-                    {
+                if (init || curr_item != prev_item) { // Only draw if they're different
+                    if (curr_item) { // There's something here! Draw it
                         draw = curr_item->draw;
-                    }
-                    else // There used to be something, but now there isn't
-                    {
+                    } else { // There used to be something, but now there isn't
                         draw = draw_nothing;
                     }
                 }
-            }
-            else if (init) // If doing a full draw, but we're out of bounds, draw the walls.
-            {
+            } else if (init) { // If doing a full draw, but we're out of bounds, draw the walls.
                 draw = draw_wall;
             }
 
@@ -135,9 +215,9 @@
         }
     }
 
-    // Draw status bars    
-    draw_upper_status();
-    draw_lower_status();
+    // Draw status bars
+    draw_upper_status(Player.x,Player.y);
+    draw_lower_status(HP,MP);
 }
 
 
@@ -150,12 +230,11 @@
 {
     // "Random" plants
     Map* map = set_active_map(0);
-    for(int i = map_width() + 3; i < map_area(); i += 39)
-    {
+    for(int i = map_width() + 3; i < map_area(); i += 39) {
         add_plant(i % map_width(), i / map_width());
     }
     pc.printf("plants\r\n");
-        
+
     pc.printf("Adding walls!\r\n");
     add_wall(0,              0,              HORIZONTAL, map_width());
     add_wall(0,              map_height()-1, HORIZONTAL, map_width());
@@ -163,9 +242,19 @@
     add_wall(map_width()-1,  0,              VERTICAL,   map_height());
     pc.printf("Walls done!\r\n");
 
+    add_npc(9,9);
+
+    add_chest(10,10);
+
+    add_castle(15,15);
+
+    add_portal(25,30);
+
     print_map();
 }
 
+//void init_dung
+
 /**
  * Program entry point! This is where it all begins.
  * This function orchestrates all the parts of the game. Most of your
@@ -176,12 +265,11 @@
 {
     // First things first: initialize hardware
     ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!");
-    uLCD.filled_rectangle(64, 64, 74, 74, RED); //DELETE OR COMMENT THIS LINE
-    /*<------ DELETE THIS LINE
+
     // Initialize the maps
     maps_init();
     init_main_map();
-    
+
     // Initialize game state
     set_active_map(0);
     Player.x = Player.y = 5;
@@ -189,23 +277,27 @@
     // Initial drawing
     draw_game(true);
     // Main game loop
-    while(1)
-    {
+    while(1) {
         // Timer to measure game update speed
-        Timer t; t.start();
-        
+        Timer t;
+        t.start();
+
         // Actually do the game update:
-        // 1. Read inputs        
-        // 2. Determine action (get_action)        
+        // 1. Read inputs
+        GameInputs input = read_inputs();
+        // 2. Determine action (get_action)
+        int action = get_action(input);
         // 3. Update game (update_game)
+        update_game(action);
         // 3b. Check for game over
         // 4. Draw frame (draw_game)
         draw_game(true);
-        
+
         // 5. Frame delay
         t.stop();
         int dt = t.read_ms();
         if (dt < 100) wait_ms(100 - dt);
     }
-  DELETE THIS LINE  ----->*/ 
+    //
+
 }