Game For ECE 2035
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
map.cpp@9:cbb9cfb1f6c5, 2021-11-30 (annotated)
- Committer:
- nasiromar
- Date:
- Tue Nov 30 00:48:48 2021 +0000
- Revision:
- 9:cbb9cfb1f6c5
- Parent:
- 7:862062ffca62
- Child:
- 10:e18685911e84
Sound Working;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rconnorlawson | 0:35660d7952f7 | 1 | #include "map.h" |
rconnorlawson | 0:35660d7952f7 | 2 | |
rconnorlawson | 0:35660d7952f7 | 3 | #include "globals.h" |
rconnorlawson | 0:35660d7952f7 | 4 | #include "graphics.h" |
rconnorlawson | 0:35660d7952f7 | 5 | |
nasiromar | 6:c9695079521d | 6 | #define NumBuckets 5 |
nasiromar | 6:c9695079521d | 7 | #define NumMaps 2 |
nasiromar | 6:c9695079521d | 8 | #define HEIGHT 50 |
nasiromar | 6:c9695079521d | 9 | #define WIDTH 50 |
nasiromar | 6:c9695079521d | 10 | |
rconnorlawson | 0:35660d7952f7 | 11 | /** |
rconnorlawson | 0:35660d7952f7 | 12 | * The Map structure. This holds a HashTable for all the MapItems, along with |
rconnorlawson | 0:35660d7952f7 | 13 | * values for the width and height of the Map. |
lballard9 | 4:37d3935365f8 | 14 | * In this file you need to define how the map will be structured. IE how will |
lballard9 | 4:37d3935365f8 | 15 | * you put values into the map, pull them from the map. Remember a "Map" variable |
nasiromar | 6:c9695079521d | 16 | * is a hashtable plus two ints (see below) |
lballard9 | 4:37d3935365f8 | 17 | * You will have more than one map variable, one will be the main map with it's own hashtable. |
lballard9 | 4:37d3935365f8 | 18 | * Then you'll have a second map with another hashtable |
lballard9 | 4:37d3935365f8 | 19 | * You should store objects into the hashtable with different properties (spells |
lballard9 | 4:37d3935365f8 | 20 | * etc) |
rconnorlawson | 0:35660d7952f7 | 21 | */ |
rconnorlawson | 0:35660d7952f7 | 22 | struct Map { |
rconnorlawson | 0:35660d7952f7 | 23 | HashTable* items; |
rconnorlawson | 0:35660d7952f7 | 24 | int w, h; |
rconnorlawson | 0:35660d7952f7 | 25 | }; |
rconnorlawson | 0:35660d7952f7 | 26 | |
rconnorlawson | 0:35660d7952f7 | 27 | /** |
rconnorlawson | 0:35660d7952f7 | 28 | * Storage area for the maps. |
rconnorlawson | 0:35660d7952f7 | 29 | * This is a global variable, but can only be access from this file because it |
rconnorlawson | 0:35660d7952f7 | 30 | * is static. |
rconnorlawson | 0:35660d7952f7 | 31 | */ |
nasiromar | 6:c9695079521d | 32 | static Map map[NumMaps]; |
rconnorlawson | 0:35660d7952f7 | 33 | static int active_map; |
rconnorlawson | 0:35660d7952f7 | 34 | |
rconnorlawson | 0:35660d7952f7 | 35 | /** |
rconnorlawson | 0:35660d7952f7 | 36 | * The first step in HashTable access for the map is turning the two-dimensional |
rconnorlawson | 0:35660d7952f7 | 37 | * key information (x, y) into a one-dimensional unsigned integer. |
rconnorlawson | 0:35660d7952f7 | 38 | * This function should uniquely map (x,y) onto the space of unsigned integers. |
rconnorlawson | 0:35660d7952f7 | 39 | */ |
nasiromar | 6:c9695079521d | 40 | static unsigned XY_KEY(int X, int Y) |
nasiromar | 6:c9695079521d | 41 | { |
rconnorlawson | 0:35660d7952f7 | 42 | // TODO: Fix me! |
nasiromar | 6:c9695079521d | 43 | unsigned int key = WIDTH*X+Y; |
nasiromar | 6:c9695079521d | 44 | return key; |
rconnorlawson | 0:35660d7952f7 | 45 | } |
rconnorlawson | 0:35660d7952f7 | 46 | |
rconnorlawson | 0:35660d7952f7 | 47 | /** |
rconnorlawson | 0:35660d7952f7 | 48 | * This is the hash function actually passed into createHashTable. It takes an |
rconnorlawson | 0:35660d7952f7 | 49 | * unsigned key (the output of XY_KEY) and turns it into a hash value (some |
rconnorlawson | 0:35660d7952f7 | 50 | * small non-negative integer). |
rconnorlawson | 0:35660d7952f7 | 51 | */ |
rconnorlawson | 0:35660d7952f7 | 52 | unsigned map_hash(unsigned key) |
rconnorlawson | 0:35660d7952f7 | 53 | { |
rconnorlawson | 0:35660d7952f7 | 54 | // TODO: Fix me! |
nasiromar | 6:c9695079521d | 55 | int hash_value = key%NumBuckets; |
nasiromar | 6:c9695079521d | 56 | return hash_value; |
rconnorlawson | 0:35660d7952f7 | 57 | } |
rconnorlawson | 0:35660d7952f7 | 58 | |
rconnorlawson | 0:35660d7952f7 | 59 | void maps_init() |
rconnorlawson | 0:35660d7952f7 | 60 | { |
nasiromar | 6:c9695079521d | 61 | // TODO: Implement! |
rconnorlawson | 0:35660d7952f7 | 62 | // Initialize hash table |
rconnorlawson | 0:35660d7952f7 | 63 | // Set width & height |
nasiromar | 6:c9695079521d | 64 | |
nasiromar | 6:c9695079521d | 65 | map[0].items = createHashTable(map_hash,NumBuckets); |
nasiromar | 6:c9695079521d | 66 | map[0].w = WIDTH; |
nasiromar | 6:c9695079521d | 67 | map[0].h = HEIGHT; |
nasiromar | 6:c9695079521d | 68 | |
nasiromar | 6:c9695079521d | 69 | map[1].items = createHashTable(map_hash,NumBuckets); |
nasiromar | 6:c9695079521d | 70 | map[1].w = WIDTH-25; |
nasiromar | 6:c9695079521d | 71 | map[1].h = HEIGHT-25; |
nasiromar | 6:c9695079521d | 72 | |
nasiromar | 6:c9695079521d | 73 | |
rconnorlawson | 0:35660d7952f7 | 74 | } |
rconnorlawson | 0:35660d7952f7 | 75 | |
rconnorlawson | 0:35660d7952f7 | 76 | Map* get_active_map() |
rconnorlawson | 0:35660d7952f7 | 77 | { |
rconnorlawson | 0:35660d7952f7 | 78 | // There's only one map |
nasiromar | 6:c9695079521d | 79 | return &map[active_map]; |
rconnorlawson | 0:35660d7952f7 | 80 | } |
rconnorlawson | 0:35660d7952f7 | 81 | |
rconnorlawson | 0:35660d7952f7 | 82 | Map* set_active_map(int m) |
rconnorlawson | 0:35660d7952f7 | 83 | { |
rconnorlawson | 0:35660d7952f7 | 84 | active_map = m; |
nasiromar | 6:c9695079521d | 85 | return &map[active_map]; |
rconnorlawson | 0:35660d7952f7 | 86 | } |
rconnorlawson | 0:35660d7952f7 | 87 | |
rconnorlawson | 0:35660d7952f7 | 88 | void print_map() |
rconnorlawson | 0:35660d7952f7 | 89 | { |
rconnorlawson | 0:35660d7952f7 | 90 | // As you add more types, you'll need to add more items to this array. |
rconnorlawson | 0:35660d7952f7 | 91 | char lookup[] = {'W', 'P'}; |
nasiromar | 6:c9695079521d | 92 | for(int y = 0; y < map_height(); y++) { |
nasiromar | 6:c9695079521d | 93 | for (int x = 0; x < map_width(); x++) { |
rconnorlawson | 0:35660d7952f7 | 94 | MapItem* item = get_here(x,y); |
rconnorlawson | 0:35660d7952f7 | 95 | if (item) pc.printf("%c", lookup[item->type]); |
rconnorlawson | 0:35660d7952f7 | 96 | else pc.printf(" "); |
rconnorlawson | 0:35660d7952f7 | 97 | } |
rconnorlawson | 0:35660d7952f7 | 98 | pc.printf("\r\n"); |
rconnorlawson | 0:35660d7952f7 | 99 | } |
rconnorlawson | 0:35660d7952f7 | 100 | } |
rconnorlawson | 0:35660d7952f7 | 101 | |
rconnorlawson | 0:35660d7952f7 | 102 | int map_width() |
rconnorlawson | 0:35660d7952f7 | 103 | { |
nasiromar | 6:c9695079521d | 104 | return get_active_map()->w; |
rconnorlawson | 0:35660d7952f7 | 105 | } |
rconnorlawson | 0:35660d7952f7 | 106 | |
rconnorlawson | 0:35660d7952f7 | 107 | int map_height() |
rconnorlawson | 0:35660d7952f7 | 108 | { |
nasiromar | 6:c9695079521d | 109 | return get_active_map()->h; |
rconnorlawson | 0:35660d7952f7 | 110 | } |
rconnorlawson | 0:35660d7952f7 | 111 | |
rconnorlawson | 0:35660d7952f7 | 112 | int map_area() |
rconnorlawson | 0:35660d7952f7 | 113 | { |
nasiromar | 6:c9695079521d | 114 | int area = map_height() * map_width(); |
nasiromar | 6:c9695079521d | 115 | return area; |
rconnorlawson | 0:35660d7952f7 | 116 | } |
rconnorlawson | 0:35660d7952f7 | 117 | |
rconnorlawson | 0:35660d7952f7 | 118 | MapItem* get_north(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 119 | { |
nasiromar | 6:c9695079521d | 120 | unsigned int key = XY_KEY(x,y-1); |
nasiromar | 6:c9695079521d | 121 | MapItem* up = (MapItem*)getItem(get_active_map()->items,key); |
nasiromar | 6:c9695079521d | 122 | return up; |
nasiromar | 6:c9695079521d | 123 | |
rconnorlawson | 0:35660d7952f7 | 124 | } |
rconnorlawson | 0:35660d7952f7 | 125 | |
rconnorlawson | 0:35660d7952f7 | 126 | MapItem* get_south(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 127 | { |
nasiromar | 6:c9695079521d | 128 | unsigned int key = XY_KEY(x,y+1); |
nasiromar | 6:c9695079521d | 129 | MapItem* down = (MapItem*)getItem(get_active_map()->items,key); |
nasiromar | 6:c9695079521d | 130 | return down; |
nasiromar | 6:c9695079521d | 131 | |
rconnorlawson | 0:35660d7952f7 | 132 | } |
rconnorlawson | 0:35660d7952f7 | 133 | |
rconnorlawson | 0:35660d7952f7 | 134 | MapItem* get_east(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 135 | { |
nasiromar | 6:c9695079521d | 136 | unsigned int key = XY_KEY(x+1,y); |
nasiromar | 6:c9695079521d | 137 | MapItem* right = (MapItem*)getItem(get_active_map()->items,key); |
nasiromar | 6:c9695079521d | 138 | return right; |
nasiromar | 6:c9695079521d | 139 | |
rconnorlawson | 0:35660d7952f7 | 140 | } |
rconnorlawson | 0:35660d7952f7 | 141 | |
rconnorlawson | 0:35660d7952f7 | 142 | MapItem* get_west(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 143 | { |
nasiromar | 6:c9695079521d | 144 | unsigned int key = XY_KEY(x-1,y); |
nasiromar | 6:c9695079521d | 145 | MapItem* left = (MapItem*)getItem(get_active_map()->items,key); |
nasiromar | 6:c9695079521d | 146 | return left; |
nasiromar | 6:c9695079521d | 147 | |
rconnorlawson | 0:35660d7952f7 | 148 | } |
rconnorlawson | 0:35660d7952f7 | 149 | |
rconnorlawson | 0:35660d7952f7 | 150 | MapItem* get_here(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 151 | { |
nasiromar | 6:c9695079521d | 152 | unsigned int key = XY_KEY(x,y); |
nasiromar | 6:c9695079521d | 153 | MapItem* curr = (MapItem*)getItem(get_active_map()->items,key); |
nasiromar | 6:c9695079521d | 154 | return curr; |
rconnorlawson | 0:35660d7952f7 | 155 | } |
rconnorlawson | 0:35660d7952f7 | 156 | |
rconnorlawson | 0:35660d7952f7 | 157 | |
rconnorlawson | 0:35660d7952f7 | 158 | void map_erase(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 159 | { |
nasiromar | 6:c9695079521d | 160 | unsigned int key = XY_KEY(x,y); |
nasiromar | 6:c9695079521d | 161 | |
nasiromar | 6:c9695079521d | 162 | destroyHashTable(get_active_map()->items); |
rconnorlawson | 0:35660d7952f7 | 163 | } |
rconnorlawson | 0:35660d7952f7 | 164 | |
rconnorlawson | 0:35660d7952f7 | 165 | void add_wall(int x, int y, int dir, int len) |
rconnorlawson | 0:35660d7952f7 | 166 | { |
nasiromar | 6:c9695079521d | 167 | for(int i = 0; i < len; i++) { |
rconnorlawson | 0:35660d7952f7 | 168 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
rconnorlawson | 0:35660d7952f7 | 169 | w1->type = WALL; |
rconnorlawson | 0:35660d7952f7 | 170 | w1->draw = draw_wall; |
rconnorlawson | 0:35660d7952f7 | 171 | w1->walkable = false; |
rconnorlawson | 0:35660d7952f7 | 172 | w1->data = NULL; |
rconnorlawson | 0:35660d7952f7 | 173 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
rconnorlawson | 0:35660d7952f7 | 174 | void* val = insertItem(get_active_map()->items, key, w1); |
rconnorlawson | 0:35660d7952f7 | 175 | if (val) free(val); // If something is already there, free it |
rconnorlawson | 0:35660d7952f7 | 176 | } |
rconnorlawson | 0:35660d7952f7 | 177 | } |
rconnorlawson | 0:35660d7952f7 | 178 | |
nasiromar | 7:862062ffca62 | 179 | void add_castle(int x, int y, int dir, int len) |
nasiromar | 6:c9695079521d | 180 | { |
nasiromar | 7:862062ffca62 | 181 | for(int i = 0; i < len; i++) { |
nasiromar | 6:c9695079521d | 182 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 6:c9695079521d | 183 | w1->type = CASTL; |
nasiromar | 6:c9695079521d | 184 | w1->draw = draw_castle; |
nasiromar | 6:c9695079521d | 185 | w1->walkable = false; |
nasiromar | 6:c9695079521d | 186 | w1->data = NULL; |
nasiromar | 7:862062ffca62 | 187 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
nasiromar | 7:862062ffca62 | 188 | void* val = insertItem(get_active_map()->items, key, w1); |
nasiromar | 6:c9695079521d | 189 | if (val) free(val); // If something is already there, free it |
nasiromar | 7:862062ffca62 | 190 | } |
nasiromar | 6:c9695079521d | 191 | } |
nasiromar | 6:c9695079521d | 192 | |
nasiromar | 9:cbb9cfb1f6c5 | 193 | MapItem*npc = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 9:cbb9cfb1f6c5 | 194 | |
nasiromar | 6:c9695079521d | 195 | |
nasiromar | 6:c9695079521d | 196 | void add_npc(int x, int y) |
nasiromar | 6:c9695079521d | 197 | { |
nasiromar | 9:cbb9cfb1f6c5 | 198 | |
nasiromar | 6:c9695079521d | 199 | npc->type = NPC; |
nasiromar | 6:c9695079521d | 200 | npc->draw = draw_npc; |
nasiromar | 6:c9695079521d | 201 | npc->walkable = false; |
nasiromar | 6:c9695079521d | 202 | npc->data = NULL; |
nasiromar | 6:c9695079521d | 203 | void* val = insertItem(get_active_map()->items,XY_KEY(x,y),npc); |
nasiromar | 6:c9695079521d | 204 | if (val) free(val); |
nasiromar | 6:c9695079521d | 205 | } |
nasiromar | 6:c9695079521d | 206 | |
nasiromar | 9:cbb9cfb1f6c5 | 207 | void delete_npc(int x, int y) |
nasiromar | 9:cbb9cfb1f6c5 | 208 | { |
nasiromar | 9:cbb9cfb1f6c5 | 209 | npc->draw = draw_nothing; |
nasiromar | 9:cbb9cfb1f6c5 | 210 | deleteItem(get_active_map()->items,XY_KEY(x,y)); |
nasiromar | 9:cbb9cfb1f6c5 | 211 | } |
nasiromar | 9:cbb9cfb1f6c5 | 212 | |
nasiromar | 6:c9695079521d | 213 | void add_portal(int x, int y) |
nasiromar | 6:c9695079521d | 214 | { |
nasiromar | 6:c9695079521d | 215 | MapItem*w1 = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 6:c9695079521d | 216 | w1->type = PORTAL; |
nasiromar | 6:c9695079521d | 217 | w1->draw = draw_portal; |
nasiromar | 6:c9695079521d | 218 | w1->walkable = true; |
nasiromar | 6:c9695079521d | 219 | w1->data = NULL; |
nasiromar | 6:c9695079521d | 220 | void* val = insertItem(get_active_map()->items,XY_KEY(x,y),w1); |
nasiromar | 6:c9695079521d | 221 | if (val) free(val); |
nasiromar | 6:c9695079521d | 222 | } |
nasiromar | 6:c9695079521d | 223 | |
nasiromar | 7:862062ffca62 | 224 | void add_kindom(int x, int y) |
nasiromar | 7:862062ffca62 | 225 | { |
nasiromar | 7:862062ffca62 | 226 | MapItem*w1 = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 7:862062ffca62 | 227 | w1->type = KINDOM; |
nasiromar | 7:862062ffca62 | 228 | w1->draw = draw_kindom; |
nasiromar | 7:862062ffca62 | 229 | w1->walkable = false; |
nasiromar | 7:862062ffca62 | 230 | w1->data = NULL; |
nasiromar | 7:862062ffca62 | 231 | void* val = insertItem(get_active_map()->items,XY_KEY(x,y),w1); |
nasiromar | 7:862062ffca62 | 232 | if (val) free(val); |
nasiromar | 7:862062ffca62 | 233 | } |
nasiromar | 7:862062ffca62 | 234 | |
nasiromar | 7:862062ffca62 | 235 | |
nasiromar | 7:862062ffca62 | 236 | void add_portal2(int x, int y) |
nasiromar | 7:862062ffca62 | 237 | { |
nasiromar | 7:862062ffca62 | 238 | MapItem*w1 = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 7:862062ffca62 | 239 | w1->type = PORTAl; |
nasiromar | 7:862062ffca62 | 240 | w1->draw = draw_portal2; |
nasiromar | 7:862062ffca62 | 241 | w1->walkable = true; |
nasiromar | 7:862062ffca62 | 242 | w1->data = NULL; |
nasiromar | 7:862062ffca62 | 243 | void* val = insertItem(get_active_map()->items,XY_KEY(x,y),w1); |
nasiromar | 7:862062ffca62 | 244 | if (val) free(val); |
nasiromar | 7:862062ffca62 | 245 | } |
nasiromar | 7:862062ffca62 | 246 | |
nasiromar | 7:862062ffca62 | 247 | |
nasiromar | 6:c9695079521d | 248 | void add_chest(int x, int y) |
nasiromar | 6:c9695079521d | 249 | { |
nasiromar | 6:c9695079521d | 250 | MapItem*chest = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 6:c9695079521d | 251 | chest->type = CHEST; |
nasiromar | 6:c9695079521d | 252 | chest->draw = draw_chest; |
nasiromar | 6:c9695079521d | 253 | chest->walkable = false; |
nasiromar | 6:c9695079521d | 254 | chest->data = NULL; |
nasiromar | 6:c9695079521d | 255 | void* val = insertItem(get_active_map()->items,XY_KEY(x,y),chest); |
nasiromar | 6:c9695079521d | 256 | if (val) free(val); |
nasiromar | 6:c9695079521d | 257 | } |
nasiromar | 6:c9695079521d | 258 | |
nasiromar | 7:862062ffca62 | 259 | void add_door(int x, int y) |
nasiromar | 7:862062ffca62 | 260 | { |
nasiromar | 7:862062ffca62 | 261 | MapItem*w1 = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 7:862062ffca62 | 262 | w1->type = DOOR; |
nasiromar | 7:862062ffca62 | 263 | w1->draw = draw_door; |
nasiromar | 7:862062ffca62 | 264 | w1->walkable = false; |
nasiromar | 7:862062ffca62 | 265 | w1->data = NULL; |
nasiromar | 7:862062ffca62 | 266 | void* val = insertItem(get_active_map()->items,XY_KEY(x,y),w1); |
nasiromar | 7:862062ffca62 | 267 | if (val) free(val); |
nasiromar | 7:862062ffca62 | 268 | } |
nasiromar | 7:862062ffca62 | 269 | |
nasiromar | 7:862062ffca62 | 270 | void add_dragon(int x, int y) |
nasiromar | 7:862062ffca62 | 271 | { |
nasiromar | 7:862062ffca62 | 272 | MapItem*w1 = (MapItem*) malloc(sizeof(MapItem)); |
nasiromar | 7:862062ffca62 | 273 | w1->type = DRAGON; |
nasiromar | 7:862062ffca62 | 274 | w1->draw = draw_dragon; |
nasiromar | 7:862062ffca62 | 275 | w1->walkable = false; |
nasiromar | 7:862062ffca62 | 276 | w1->data = NULL; |
nasiromar | 7:862062ffca62 | 277 | void* val = insertItem(get_active_map()->items,XY_KEY(x,y),w1); |
nasiromar | 7:862062ffca62 | 278 | if (val) free(val); |
nasiromar | 7:862062ffca62 | 279 | } |
nasiromar | 6:c9695079521d | 280 | |
nasiromar | 6:c9695079521d | 281 | |
nasiromar | 6:c9695079521d | 282 | |
nasiromar | 6:c9695079521d | 283 | |
rconnorlawson | 0:35660d7952f7 | 284 | void add_plant(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 285 | { |
rconnorlawson | 0:35660d7952f7 | 286 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
rconnorlawson | 0:35660d7952f7 | 287 | w1->type = PLANT; |
rconnorlawson | 0:35660d7952f7 | 288 | w1->draw = draw_plant; |
rconnorlawson | 0:35660d7952f7 | 289 | w1->walkable = true; |
nasiromar | 9:cbb9cfb1f6c5 | 290 | w1->data = FRUIT; |
rconnorlawson | 0:35660d7952f7 | 291 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
rconnorlawson | 0:35660d7952f7 | 292 | if (val) free(val); // If something is already there, free it |
rconnorlawson | 0:35660d7952f7 | 293 | } |