#include #include #include "map.h" #include "player.h" #include "define.h" #include "util.h" extern struct Map map_1; extern struct Map map_2; struct Map *maps[] = { &map_1, &map_2, }; /*check if a tile is walkable*/ int map_walkable(struct Map const *map, int x, int y) { int tile = map->info_map[x + map->w * y]; if(x < 0 || x > map->w-1 || y < 0 || y > map->h-1) return 0; return (tile != TILE_SOLID && tile != TILE_TALKABLE); } /*get the tile under the player*/ int map_get_player_tile(struct Game const *game) { return game->map->info_map[game->player->pos.x + game->map->w * game->player->pos.y]; } /*generate the interior*/ void generate_interior_map(struct Game *game) { const int NB_INTERIORS = 3; extern struct Map in_1; extern struct Map in_2; extern struct Map in_3; struct Map *interiors[] = { &in_1, &in_2, &in_3 }; srand(game->player->pos.x * game->player->pos.y); game->map = interiors[rand_range(0,NB_INTERIORS)]; game->player->pos = locate_tile(game->map, TILE_DOOR_OUT); } /*may be useful later*/ void set_map(struct Game *game, int id) { game->map = maps[id]; } /*locate the first occurence of a tile in a specified map*/ struct Vec2 locate_tile(struct Map const *map, int tile) { for(int x = 0; x < map->w; x++) { for(int y = 0; y < map->h; y++) { int tile_id = map->info_map[x + y * map->w]; if(tile_id == tile) { return VEC2(x,y); } } } return VEC2(0,0); } struct Teleporter get_teleporter_xy(struct Map *map, struct Vec2 pos) { int i = 0; while(i < map->teleporter_count) { if(map->teleporters[i].x == pos.x && map->teleporters[i].y == pos.y) return map->teleporters[i]; i++; } return map->teleporters[0]; }