Finished V1

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Revision:
4:2297a714936f
Parent:
0:35660d7952f7
Child:
5:2fb023cdc666
--- a/map.cpp	Mon Oct 28 05:26:35 2019 +0000
+++ b/map.cpp	Tue Nov 19 16:53:47 2019 +0000
@@ -20,6 +20,13 @@
 static Map map;
 static int active_map;
 
+//MYCODE 
+static Map map2;
+
+//array of maps
+static Map maps[2];
+
+
 /**
  * The first step in HashTable access for the map is turning the two-dimensional
  * key information (x, y) into a one-dimensional unsigned integer.
@@ -27,6 +34,9 @@
  */
 static unsigned XY_KEY(int X, int Y) {
     // TODO: Fix me!
+    
+    //MYCODE
+    return ((X*map.h) + Y); //multiplies X by height of map to get the row, then adds y for the column
 }
 
 /**
@@ -37,6 +47,9 @@
 unsigned map_hash(unsigned key)
 {
     // TODO: Fix me!
+    
+    //MYCODE
+    return key%NUMBUCKETS; //NB = 75
 }
 
 void maps_init()
@@ -44,18 +57,40 @@
     // TODO: Implement!    
     // Initialize hash table
     // Set width & height
+    
+    //MYCODE
+    map.items = createHashTable(map_hash, NUMBUCKETS);//NB = 75
+    map.w = MAPWIDTH;  //75
+    map.h = MAPHEIGHT; //75
+    maps[0] = map;
+    
+    map2.items = createHashTable(map_hash, NUMBUCKETS);
+    map2.w = MAP2WIDTH;     //20
+    map2.h = MAP2HEIGHT;    //10
+    maps[1] = map2;
 }
 
 Map* get_active_map()
 {
+    //OGCODE
     // There's only one map
-    return ↦
+    //return ↦
+    
+    return &(maps[active_map]);
+    
+   
+    
 }
 
 Map* set_active_map(int m)
 {
+    //OGCODE
+    //active_map = m;
+    //return ↦
+    
     active_map = m;
-    return ↦
+    return &(maps[m]);
+    
 }
 
 void print_map()
@@ -74,41 +109,72 @@
     }
 }
 
+
+//MYCODE
 int map_width()
 {
+    //need to get active map
+    Map *map = get_active_map();
+    return map->w;
 }
 
+//MYCODE
 int map_height()
 {
+    Map *map = get_active_map();
+    return map->h;
 }
 
+//MYCODE
 int map_area()
 {
+ 
+    Map *map = get_active_map();
+    return (map->h * map->w);
 }
 
+//MYCODE
 MapItem* get_north(int x, int y)
 {
+    Map *thisMap = get_active_map();
+    int thisKey = XY_KEY(x, y-1);
+    return (MapItem*) getItem(thisMap->items,thisKey);
 }
 
 MapItem* get_south(int x, int y)
 {
+    Map *thisMap = get_active_map();
+    int thisKey = XY_KEY(x, y+1);
+    return (MapItem*) getItem(thisMap->items,thisKey);
 }
 
 MapItem* get_east(int x, int y)
 {
+    Map *thisMap = get_active_map();
+    int thisKey = XY_KEY(x+1, y);
+    return (MapItem*) getItem(thisMap->items,thisKey);
 }
 
 MapItem* get_west(int x, int y)
 {
+    Map *thisMap = get_active_map();
+    int thisKey = XY_KEY(x-1, y);
+    return (MapItem*) getItem(thisMap->items,thisKey);
 }
 
 MapItem* get_here(int x, int y)
 {
+    Map *thisMap = get_active_map();
+    int thisKey = XY_KEY(x, y);
+    return (MapItem*) getItem(thisMap->items,thisKey);
 }
 
 
 void map_erase(int x, int y)
 {
+    Map *thisMap = get_active_map();
+    int thisKey = XY_KEY(x, y);
+    deleteItem(thisMap->items,thisKey);
 }
 
 void add_wall(int x, int y, int dir, int len)
@@ -135,4 +201,26 @@
     w1->data = NULL;
     void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
     if (val) free(val); // If something is already there, free it
+}
+
+//MYCODE
+void add_NPC(int x, int y)
+{
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = NPC;
+    w1->draw = draw_NPC;
+    w1->walkable = false;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it
+}
+
+void add_ladder(int x, int y){
+    MapItem* w1 = (MapItem*) malloc(sizeof(MapItem));
+    w1->type = ladder;
+    w1->draw = draw_ladder;
+    w1->walkable = true;
+    w1->data = NULL;
+    void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1);
+    if (val) free(val); // If something is already there, free it   
 }
\ No newline at end of file