![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Adventure game written for ECE2035 at the Georgia Institute of Technology
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
Diff: map.cpp
- Revision:
- 2:0876296d9473
- Parent:
- 0:35660d7952f7
- Child:
- 3:289762133fd6
--- a/map.cpp Wed Apr 04 21:11:07 2018 +0000 +++ b/map.cpp Tue Apr 17 17:17:20 2018 +0000 @@ -7,17 +7,17 @@ * The Map structure. This holds a HashTable for all the MapItems, along with * values for the width and height of the Map. */ -struct Map { +typedef struct{ HashTable* items; int w, h; -}; +}Map; /** * Storage area for the maps. * This is a global variable, but can only be access from this file because it * is static. */ -static Map map; +static Map maps[2]; static int active_map; /** @@ -27,6 +27,8 @@ */ static unsigned XY_KEY(int X, int Y) { // TODO: Fix me! + //could just calculate the single array position of the location in the map using the offset formula from P1 + return (map.w)*Y + X; } /** @@ -37,6 +39,7 @@ unsigned map_hash(unsigned key) { // TODO: Fix me! + return key%5; } void maps_init() @@ -44,18 +47,21 @@ // TODO: Implement! // Initialize hash table // Set width & height + map.items = createHashTable(map_hash, 5); + map.w = 50; + map.h = 50; } Map* get_active_map() { - // There's only one map - return ↦ + + return &maps[active_map]; } Map* set_active_map(int m) { active_map = m; - return ↦ + return &maps[active_map]; } void print_map() @@ -76,39 +82,74 @@ int map_width() { + get_active_map() + return map.w; } int map_height() { + return map.h; } int map_area() { + return map.h*map.w; } MapItem* get_north(int x, int y) { + Map* map = get_active_map(); + //get key of item north of the location + int index = XY_KEY(x, y-1); + return (MapItem*)getItem(map->items, index); } MapItem* get_south(int x, int y) { + Map* map = get_active_map(); + int index = XY_KEY(x, y+1); + return (MapItem*)getItem(map->items, index); } MapItem* get_east(int x, int y) { + Map* map = get_active_map(); + int index = XY_KEY(x-1, y); //wtf why does this work boi + return (MapItem*)getItem(map->items, index); } MapItem* get_west(int x, int y) { + Map* map = get_active_map(); + int index = XY_KEY(x+1, y); //shouldn't this be minus? + return (MapItem*)getItem(map->items, index); } MapItem* get_here(int x, int y) { + Map* map = get_active_map(); + int index = XY_KEY(x, y); + return (MapItem*)getItem(map->items, index); } void map_erase(int x, int y) { + Map* map = get_active_map(); + int index = XY_KEY(x, y); + deleteItem(map->items, index); +} + +void omni(){ + Map* map = get_active_map(); + int h = map -> h; + int w = map -> w; + for (int i = 0; i < h; i++){ + for(int j = 0; j < w; j++){ + MapItem* object = (MapItem*)getItem(map->items, XY_KEY(i,j)); + if(object) object -> walkable = true; + } + } } void add_wall(int x, int y, int dir, int len) @@ -135,4 +176,71 @@ 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_rock(int x, int y) +{ + MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); + w1->type = ROCK; + w1->draw = draw_rock; + w1->walkable = false; + Rock* r = (Rock*) malloc(sizeof(Rock)); + //r -> is_pushed = false; + //r -> wall_touch = false; + //r -> x = x; + //r -> y = y; + w1->data = r; + 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_npc(int x, int y) +{ + MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); + w1->type = NPC; + w1->draw = draw_npc; + w1->walkable = false; + NonPlayer* npc = (NonPlayer*) malloc(sizeof(NonPlayer)); + npc->quest_requested = false; + npc->quest_complete = false; + npc->has_key = true; + w1->data = npc; + 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_door(int x, int y) +{ + MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); + w1->type = DOOR; + w1->draw = draw_door; + w1->walkable = false; + int o = false; + w1->data = &o; + 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_goal(int x, int y) +{ + MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); + w1->type = GOAL; + w1->draw = draw_goal; + w1->walkable = false; + int o = false; + w1->data = &o; + 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_spike(int x, int y) +{ + MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); + w1->type = SPIKE; + w1->draw = draw_spike; + w1->walkable = false; + int o = false; + w1->data = &o; + 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