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