diff options
Diffstat (limited to 'src/map.cpp')
| -rw-r--r-- | src/map.cpp | 241 |
1 files changed, 241 insertions, 0 deletions
| diff --git a/src/map.cpp b/src/map.cpp new file mode 100644 index 0000000..10cd313 --- /dev/null +++ b/src/map.cpp | |||
| @@ -0,0 +1,241 @@ | |||
| 1 | #include "map.h" | ||
| 2 | |||
| 3 | struct platform_t { | ||
| 4 | int x; | ||
| 5 | int y; | ||
| 6 | int w; | ||
| 7 | int h; | ||
| 8 | bool enter_from_left; | ||
| 9 | bool enter_from_right; | ||
| 10 | bool enter_from_top; | ||
| 11 | bool enter_from_bottom; | ||
| 12 | int r; | ||
| 13 | int g; | ||
| 14 | int b; | ||
| 15 | int a; | ||
| 16 | }; | ||
| 17 | |||
| 18 | Map::Map() | ||
| 19 | { | ||
| 20 | add_collision(-6, 0, GAME_WIDTH, left, 1); | ||
| 21 | add_collision(GAME_WIDTH+6, 0, GAME_WIDTH, right, 1); | ||
| 22 | |||
| 23 | FILE* f = fopen("../maps/bigmap.txt", "r"); | ||
| 24 | char* mapbuf = (char*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(char)); | ||
| 25 | |||
| 26 | for (int i=0; i<MAP_HEIGHT; i++) | ||
| 27 | { | ||
| 28 | fread(mapbuf + i*MAP_WIDTH, sizeof(char), MAP_WIDTH, f); | ||
| 29 | fgetc(f); | ||
| 30 | } | ||
| 31 | |||
| 32 | fclose(f); | ||
| 33 | |||
| 34 | //Texture* tiles = loadTextureFromBMP("../res/tiles.bmp"); | ||
| 35 | bg = createTexture(GAME_WIDTH, GAME_HEIGHT); | ||
| 36 | fillTexture(bg, NULL, 0, 0, 0); | ||
| 37 | |||
| 38 | for (int i=0; i<MAP_WIDTH*MAP_HEIGHT; i++) | ||
| 39 | { | ||
| 40 | int x = i % MAP_WIDTH; | ||
| 41 | int y = i / MAP_WIDTH; | ||
| 42 | Rectangle dst(x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); | ||
| 43 | //Rectangle src; | ||
| 44 | |||
| 45 | switch (mapbuf[i]) | ||
| 46 | { | ||
| 47 | case ' ': break; | ||
| 48 | case 'X': fillTexture(bg, &dst, 255, 85, 85); break; | ||
| 49 | case 'P': fillTexture(bg, &dst, 85, 255, 255); break; | ||
| 50 | } | ||
| 51 | |||
| 52 | //blitTexture(tiles, bg, &src, &dst); | ||
| 53 | |||
| 54 | if (mapbuf[i] == 'X') | ||
| 55 | { | ||
| 56 | if ((x != 0) && (mapbuf[i-1] != 'X')) | ||
| 57 | { | ||
| 58 | add_collision(x*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, right, 0); | ||
| 59 | } | ||
| 60 | |||
| 61 | if ((x != 15) && (mapbuf[i+1] != 'X')) | ||
| 62 | { | ||
| 63 | add_collision((x+1)*TILE_WIDTH, y*TILE_HEIGHT, (y+1)*TILE_HEIGHT, left, 0); | ||
| 64 | } | ||
| 65 | |||
| 66 | if ((y != 0) && (mapbuf[i-MAP_WIDTH] != 'X')) | ||
| 67 | { | ||
| 68 | add_collision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, down, 0); | ||
| 69 | } | ||
| 70 | |||
| 71 | if ((y != 15) && (mapbuf[i+MAP_WIDTH] != 'X')) | ||
| 72 | { | ||
| 73 | add_collision((y+1)*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, up, 0); | ||
| 74 | } | ||
| 75 | } else if (mapbuf[i] == 'P') | ||
| 76 | { | ||
| 77 | add_collision(y*TILE_HEIGHT, x*TILE_WIDTH, (x+1)*TILE_WIDTH, down, 0); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | //destroyTexture(tiles); | ||
| 82 | } | ||
| 83 | |||
| 84 | Map::~Map() | ||
| 85 | { | ||
| 86 | destroyTexture(bg); | ||
| 87 | } | ||
| 88 | |||
| 89 | void Map::add_collision(int axis, int lower, int upper, direction_t dir, int type) | ||
| 90 | { | ||
| 91 | //printf("added collision\n"); | ||
| 92 | list<collision_t>::iterator it; | ||
| 93 | |||
| 94 | switch (dir) | ||
| 95 | { | ||
| 96 | case up: | ||
| 97 | it = up_collisions.begin(); | ||
| 98 | for (; it!=up_collisions.end(); it++) | ||
| 99 | { | ||
| 100 | if (it->axis < axis) break; | ||
| 101 | } | ||
| 102 | |||
| 103 | up_collisions.insert(it, {axis, lower, upper, type}); | ||
| 104 | |||
| 105 | break; | ||
| 106 | case down: | ||
| 107 | it = down_collisions.begin(); | ||
| 108 | for (; it!=down_collisions.end(); it++) | ||
| 109 | { | ||
| 110 | if (it->axis > axis) break; | ||
| 111 | } | ||
| 112 | |||
| 113 | down_collisions.insert(it, {axis, lower, upper, type}); | ||
| 114 | |||
| 115 | break; | ||
| 116 | case left: | ||
| 117 | it = left_collisions.begin(); | ||
| 118 | for (; it!=left_collisions.end(); it++) | ||
| 119 | { | ||
| 120 | if (it->axis < axis) break; | ||
| 121 | } | ||
| 122 | |||
| 123 | left_collisions.insert(it, {axis, lower, upper, type}); | ||
| 124 | |||
| 125 | break; | ||
| 126 | case right: | ||
| 127 | it = right_collisions.begin(); | ||
| 128 | for (; it!=right_collisions.end(); it++) | ||
| 129 | { | ||
| 130 | if (it->axis > axis) break; | ||
| 131 | } | ||
| 132 | |||
| 133 | right_collisions.insert(it, {axis, lower, upper, type}); | ||
| 134 | |||
| 135 | break; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | void Map::check_collisions(mob_t* mob, int x_next, int y_next) | ||
| 140 | { | ||
| 141 | if (x_next < mob->x) | ||
| 142 | { | ||
| 143 | for (list<collision_t>::iterator it=left_collisions.begin(); it!=left_collisions.end(); it++) | ||
| 144 | { | ||
| 145 | if (it->axis > mob->x) continue; | ||
| 146 | if (it->axis < x_next) break; | ||
| 147 | |||
| 148 | if ((mob->y+mob->h > it->lower) && (mob->y < it->upper)) | ||
| 149 | { | ||
| 150 | // We have a collision! | ||
| 151 | if (it->type == 0) | ||
| 152 | { | ||
| 153 | x_next = it->axis; | ||
| 154 | mob->x_vel = 0; | ||
| 155 | } else if (it->type == 1) | ||
| 156 | { | ||
| 157 | x_next = GAME_WIDTH-mob->w/2; | ||
| 158 | } | ||
| 159 | |||
| 160 | break; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } else if (x_next > mob->x) | ||
| 164 | { | ||
| 165 | for (list<collision_t>::iterator it=right_collisions.begin(); it!=right_collisions.end(); it++) | ||
| 166 | { | ||
| 167 | if (it->axis < mob->x+mob->w) continue; | ||
| 168 | if (it->axis > x_next+mob->w) break; | ||
| 169 | |||
| 170 | if ((mob->y+mob->h > it->lower) && (mob->y < it->upper)) | ||
| 171 | { | ||
| 172 | // We have a collision! | ||
| 173 | if (it->type == 0) | ||
| 174 | { | ||
| 175 | x_next = it->axis - mob->w; | ||
| 176 | mob->x_vel = 0; | ||
| 177 | } else if (it->type == 1) | ||
| 178 | { | ||
| 179 | x_next = -mob->w/2; | ||
| 180 | } | ||
| 181 | |||
| 182 | break; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | mob->x = x_next; | ||
| 188 | |||
| 189 | if (y_next < mob->y) | ||
| 190 | { | ||
| 191 | for (list<collision_t>::iterator it=up_collisions.begin(); it!=up_collisions.end(); it++) | ||
| 192 | { | ||
| 193 | if (it->axis > mob->y) continue; | ||
| 194 | if (it->axis < y_next) break; | ||
| 195 | |||
| 196 | if ((mob->x+mob->w > it->lower) && (mob->x < it->upper)) | ||
| 197 | { | ||
| 198 | // We have a collision! | ||
| 199 | if (it->type == 0) | ||
| 200 | { | ||
| 201 | y_next = it->axis; | ||
| 202 | mob->y_vel = 0; | ||
| 203 | } else if (it->type == 1) | ||
| 204 | { | ||
| 205 | y_next = GAME_HEIGHT-mob->h/2-1; | ||
| 206 | } | ||
| 207 | |||
| 208 | break; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | } else if (y_next > mob->y) | ||
| 212 | { | ||
| 213 | for (list<collision_t>::iterator it=down_collisions.begin(); it!=down_collisions.end(); it++) | ||
| 214 | { | ||
| 215 | if (it->axis < mob->y+mob->h) continue; | ||
| 216 | if (it->axis > y_next+mob->h) break; | ||
| 217 | |||
| 218 | if ((mob->x+mob->w > it->lower) && (mob->x < it->upper)) | ||
| 219 | { | ||
| 220 | // We have a collision! | ||
| 221 | if (it->type == 0) | ||
| 222 | { | ||
| 223 | y_next = it->axis - mob->h; | ||
| 224 | mob->y_vel = 0; | ||
| 225 | } else if (it->type == 1) | ||
| 226 | { | ||
| 227 | y_next = 1 - mob->h/2; | ||
| 228 | } | ||
| 229 | |||
| 230 | break; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | mob->y = y_next; | ||
| 236 | } | ||
| 237 | |||
| 238 | void Map::render(Texture* buffer) | ||
| 239 | { | ||
| 240 | blitTexture(bg, buffer, NULL, NULL); | ||
| 241 | } | ||
