Game For ECE 2035
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
map.cpp@4:37d3935365f8, 2021-09-28 (annotated)
- Committer:
- lballard9
- Date:
- Tue Sep 28 13:52:11 2021 +0000
- Revision:
- 4:37d3935365f8
- Parent:
- 0:35660d7952f7
- Child:
- 6:c9695079521d
Fall 2021 notes added
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 | |
rconnorlawson | 0:35660d7952f7 | 6 | /** |
rconnorlawson | 0:35660d7952f7 | 7 | * The Map structure. This holds a HashTable for all the MapItems, along with |
rconnorlawson | 0:35660d7952f7 | 8 | * values for the width and height of the Map. |
lballard9 | 4:37d3935365f8 | 9 | * In this file you need to define how the map will be structured. IE how will |
lballard9 | 4:37d3935365f8 | 10 | * you put values into the map, pull them from the map. Remember a "Map" variable |
lballard9 | 4:37d3935365f8 | 11 | * is a hashtable plus two ints (see below) |
lballard9 | 4:37d3935365f8 | 12 | * You will have more than one map variable, one will be the main map with it's own hashtable. |
lballard9 | 4:37d3935365f8 | 13 | * Then you'll have a second map with another hashtable |
lballard9 | 4:37d3935365f8 | 14 | * You should store objects into the hashtable with different properties (spells |
lballard9 | 4:37d3935365f8 | 15 | * etc) |
rconnorlawson | 0:35660d7952f7 | 16 | */ |
rconnorlawson | 0:35660d7952f7 | 17 | struct Map { |
rconnorlawson | 0:35660d7952f7 | 18 | HashTable* items; |
rconnorlawson | 0:35660d7952f7 | 19 | int w, h; |
rconnorlawson | 0:35660d7952f7 | 20 | }; |
rconnorlawson | 0:35660d7952f7 | 21 | |
rconnorlawson | 0:35660d7952f7 | 22 | /** |
rconnorlawson | 0:35660d7952f7 | 23 | * Storage area for the maps. |
rconnorlawson | 0:35660d7952f7 | 24 | * This is a global variable, but can only be access from this file because it |
rconnorlawson | 0:35660d7952f7 | 25 | * is static. |
rconnorlawson | 0:35660d7952f7 | 26 | */ |
rconnorlawson | 0:35660d7952f7 | 27 | static Map map; |
rconnorlawson | 0:35660d7952f7 | 28 | static int active_map; |
rconnorlawson | 0:35660d7952f7 | 29 | |
rconnorlawson | 0:35660d7952f7 | 30 | /** |
rconnorlawson | 0:35660d7952f7 | 31 | * The first step in HashTable access for the map is turning the two-dimensional |
rconnorlawson | 0:35660d7952f7 | 32 | * key information (x, y) into a one-dimensional unsigned integer. |
rconnorlawson | 0:35660d7952f7 | 33 | * This function should uniquely map (x,y) onto the space of unsigned integers. |
rconnorlawson | 0:35660d7952f7 | 34 | */ |
rconnorlawson | 0:35660d7952f7 | 35 | static unsigned XY_KEY(int X, int Y) { |
rconnorlawson | 0:35660d7952f7 | 36 | // TODO: Fix me! |
rconnorlawson | 0:35660d7952f7 | 37 | } |
rconnorlawson | 0:35660d7952f7 | 38 | |
rconnorlawson | 0:35660d7952f7 | 39 | /** |
rconnorlawson | 0:35660d7952f7 | 40 | * This is the hash function actually passed into createHashTable. It takes an |
rconnorlawson | 0:35660d7952f7 | 41 | * unsigned key (the output of XY_KEY) and turns it into a hash value (some |
rconnorlawson | 0:35660d7952f7 | 42 | * small non-negative integer). |
rconnorlawson | 0:35660d7952f7 | 43 | */ |
rconnorlawson | 0:35660d7952f7 | 44 | unsigned map_hash(unsigned key) |
rconnorlawson | 0:35660d7952f7 | 45 | { |
rconnorlawson | 0:35660d7952f7 | 46 | // TODO: Fix me! |
rconnorlawson | 0:35660d7952f7 | 47 | } |
rconnorlawson | 0:35660d7952f7 | 48 | |
rconnorlawson | 0:35660d7952f7 | 49 | void maps_init() |
rconnorlawson | 0:35660d7952f7 | 50 | { |
rconnorlawson | 0:35660d7952f7 | 51 | // TODO: Implement! |
rconnorlawson | 0:35660d7952f7 | 52 | // Initialize hash table |
rconnorlawson | 0:35660d7952f7 | 53 | // Set width & height |
rconnorlawson | 0:35660d7952f7 | 54 | } |
rconnorlawson | 0:35660d7952f7 | 55 | |
rconnorlawson | 0:35660d7952f7 | 56 | Map* get_active_map() |
rconnorlawson | 0:35660d7952f7 | 57 | { |
rconnorlawson | 0:35660d7952f7 | 58 | // There's only one map |
rconnorlawson | 0:35660d7952f7 | 59 | return ↦ |
rconnorlawson | 0:35660d7952f7 | 60 | } |
rconnorlawson | 0:35660d7952f7 | 61 | |
rconnorlawson | 0:35660d7952f7 | 62 | Map* set_active_map(int m) |
rconnorlawson | 0:35660d7952f7 | 63 | { |
rconnorlawson | 0:35660d7952f7 | 64 | active_map = m; |
rconnorlawson | 0:35660d7952f7 | 65 | return ↦ |
rconnorlawson | 0:35660d7952f7 | 66 | } |
rconnorlawson | 0:35660d7952f7 | 67 | |
rconnorlawson | 0:35660d7952f7 | 68 | void print_map() |
rconnorlawson | 0:35660d7952f7 | 69 | { |
rconnorlawson | 0:35660d7952f7 | 70 | // As you add more types, you'll need to add more items to this array. |
rconnorlawson | 0:35660d7952f7 | 71 | char lookup[] = {'W', 'P'}; |
rconnorlawson | 0:35660d7952f7 | 72 | for(int y = 0; y < map_height(); y++) |
rconnorlawson | 0:35660d7952f7 | 73 | { |
rconnorlawson | 0:35660d7952f7 | 74 | for (int x = 0; x < map_width(); x++) |
rconnorlawson | 0:35660d7952f7 | 75 | { |
rconnorlawson | 0:35660d7952f7 | 76 | MapItem* item = get_here(x,y); |
rconnorlawson | 0:35660d7952f7 | 77 | if (item) pc.printf("%c", lookup[item->type]); |
rconnorlawson | 0:35660d7952f7 | 78 | else pc.printf(" "); |
rconnorlawson | 0:35660d7952f7 | 79 | } |
rconnorlawson | 0:35660d7952f7 | 80 | pc.printf("\r\n"); |
rconnorlawson | 0:35660d7952f7 | 81 | } |
rconnorlawson | 0:35660d7952f7 | 82 | } |
rconnorlawson | 0:35660d7952f7 | 83 | |
rconnorlawson | 0:35660d7952f7 | 84 | int map_width() |
rconnorlawson | 0:35660d7952f7 | 85 | { |
rconnorlawson | 0:35660d7952f7 | 86 | } |
rconnorlawson | 0:35660d7952f7 | 87 | |
rconnorlawson | 0:35660d7952f7 | 88 | int map_height() |
rconnorlawson | 0:35660d7952f7 | 89 | { |
rconnorlawson | 0:35660d7952f7 | 90 | } |
rconnorlawson | 0:35660d7952f7 | 91 | |
rconnorlawson | 0:35660d7952f7 | 92 | int map_area() |
rconnorlawson | 0:35660d7952f7 | 93 | { |
rconnorlawson | 0:35660d7952f7 | 94 | } |
rconnorlawson | 0:35660d7952f7 | 95 | |
rconnorlawson | 0:35660d7952f7 | 96 | MapItem* get_north(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 97 | { |
rconnorlawson | 0:35660d7952f7 | 98 | } |
rconnorlawson | 0:35660d7952f7 | 99 | |
rconnorlawson | 0:35660d7952f7 | 100 | MapItem* get_south(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 101 | { |
rconnorlawson | 0:35660d7952f7 | 102 | } |
rconnorlawson | 0:35660d7952f7 | 103 | |
rconnorlawson | 0:35660d7952f7 | 104 | MapItem* get_east(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 105 | { |
rconnorlawson | 0:35660d7952f7 | 106 | } |
rconnorlawson | 0:35660d7952f7 | 107 | |
rconnorlawson | 0:35660d7952f7 | 108 | MapItem* get_west(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 109 | { |
rconnorlawson | 0:35660d7952f7 | 110 | } |
rconnorlawson | 0:35660d7952f7 | 111 | |
rconnorlawson | 0:35660d7952f7 | 112 | MapItem* get_here(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 113 | { |
rconnorlawson | 0:35660d7952f7 | 114 | } |
rconnorlawson | 0:35660d7952f7 | 115 | |
rconnorlawson | 0:35660d7952f7 | 116 | |
rconnorlawson | 0:35660d7952f7 | 117 | void map_erase(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 118 | { |
rconnorlawson | 0:35660d7952f7 | 119 | } |
rconnorlawson | 0:35660d7952f7 | 120 | |
rconnorlawson | 0:35660d7952f7 | 121 | void add_wall(int x, int y, int dir, int len) |
rconnorlawson | 0:35660d7952f7 | 122 | { |
rconnorlawson | 0:35660d7952f7 | 123 | for(int i = 0; i < len; i++) |
rconnorlawson | 0:35660d7952f7 | 124 | { |
rconnorlawson | 0:35660d7952f7 | 125 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
rconnorlawson | 0:35660d7952f7 | 126 | w1->type = WALL; |
rconnorlawson | 0:35660d7952f7 | 127 | w1->draw = draw_wall; |
rconnorlawson | 0:35660d7952f7 | 128 | w1->walkable = false; |
rconnorlawson | 0:35660d7952f7 | 129 | w1->data = NULL; |
rconnorlawson | 0:35660d7952f7 | 130 | unsigned key = (dir == HORIZONTAL) ? XY_KEY(x+i, y) : XY_KEY(x, y+i); |
rconnorlawson | 0:35660d7952f7 | 131 | void* val = insertItem(get_active_map()->items, key, w1); |
rconnorlawson | 0:35660d7952f7 | 132 | if (val) free(val); // If something is already there, free it |
rconnorlawson | 0:35660d7952f7 | 133 | } |
rconnorlawson | 0:35660d7952f7 | 134 | } |
rconnorlawson | 0:35660d7952f7 | 135 | |
rconnorlawson | 0:35660d7952f7 | 136 | void add_plant(int x, int y) |
rconnorlawson | 0:35660d7952f7 | 137 | { |
rconnorlawson | 0:35660d7952f7 | 138 | MapItem* w1 = (MapItem*) malloc(sizeof(MapItem)); |
rconnorlawson | 0:35660d7952f7 | 139 | w1->type = PLANT; |
rconnorlawson | 0:35660d7952f7 | 140 | w1->draw = draw_plant; |
rconnorlawson | 0:35660d7952f7 | 141 | w1->walkable = true; |
rconnorlawson | 0:35660d7952f7 | 142 | w1->data = NULL; |
rconnorlawson | 0:35660d7952f7 | 143 | void* val = insertItem(get_active_map()->items, XY_KEY(x, y), w1); |
rconnorlawson | 0:35660d7952f7 | 144 | if (val) free(val); // If something is already there, free it |
rconnorlawson | 0:35660d7952f7 | 145 | } |