tile.c (4236B)
1 #include <stdlib.h> 2 3 #include "../include/entity.h" 4 #include "../include/tile.h" 5 #include "../include/const.h" 6 #include "../include/util.h" 7 8 /* ########################################################################## */ 9 /* # Tile # */ 10 /* ########################################################################## */ 11 12 tile_t *tile_create(pos_t pos, tile_e type) { 13 tile_t *tile = malloc(sizeof(tile_t)); 14 tile->pos = pos; 15 tile->type = type; 16 return tile; 17 } 18 19 void tile_update(tile_t *tile, game_t *game) { 20 UNUSED(tile); 21 UNUSED(game); 22 } 23 24 void tile_draw(tile_t *tile, game_t *game) { 25 switch (tile->type) { 26 case TILE_AIR: { 27 break; 28 } 29 case TILE_STONE: { 30 DrawTextureRec(game->assets.tiles, texture_rect(0, 0, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 31 break; 32 } 33 case TILE_STONE_JOINT: { 34 DrawTextureRec(game->assets.tiles, texture_rect(1, 0, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 35 break; 36 } 37 case TILE_STONE_WALL: { 38 DrawTextureRec(game->assets.tiles, texture_rect(2, 0, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 39 break; 40 } 41 case TILE_GRASS: { 42 DrawTextureRec(game->assets.tiles, texture_rect(3, 0, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 43 break; 44 } 45 case TILE_WOOD_PLATFORM: { 46 DrawTextureRec(game->assets.tiles, texture_rect(4, 0, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 47 break; 48 } 49 case TILE_SNOW: { 50 DrawTextureRec(game->assets.tiles, texture_rect(5, 0, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 51 break; 52 } 53 case TILE_SAND: { 54 DrawTextureRec(game->assets.tiles, texture_rect(6, 0, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 55 break; 56 } 57 case TILE_BOX: { 58 DrawTextureRec(game->assets.tiles, texture_rect(7, 0, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 59 break; 60 } 61 case TILE_BG_CHANDELIER: { 62 DrawTextureRec(game->assets.background, texture_rect_v(3, 5, 4, 3, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 63 break; 64 } 65 case TILE_BG_WINDOW_1: { 66 DrawTextureRec(game->assets.background, texture_rect_v(8, 0, 4, 6, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 67 break; 68 } 69 case TILE_BG_WINDOW_2: { 70 DrawTextureRec(game->assets.background, texture_rect_v(8, 7, 3, 4, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 71 break; 72 } 73 case TILE_BG_BANNER: { 74 DrawTextureRec(game->assets.background, texture_rect_v(3, 0, 3, 4, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 75 break; 76 } 77 case TILE_BG_BRICK_1: { 78 DrawTextureRec(game->assets.background, texture_rect_v(0, 0, 2, 3, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 79 break; 80 } 81 case TILE_BG_BRICK_2: { 82 DrawTextureRec(game->assets.background, texture_rect_v(0, 4, 2, 1, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 83 break; 84 } 85 case TILE_BG_BRICK_3: { 86 DrawTextureRec(game->assets.background, texture_rect_v(0, 6, 2, 2, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 87 break; 88 } 89 case TILE_BG_INFO: { 90 DrawTextureRec(game->assets.background, texture_rect_v(12, 0, 10, 6, TILE_WIDTH, TILE_HEIGHT), tile->pos, WHITE); 91 break; 92 } 93 } 94 } 95 96 tile_t *tile_get(game_t *game, int x, int y) { 97 for (int i = 0; i < game->tiles_len; i++) { 98 tile_t *tile = game->tiles[i]; 99 if (x >= tile->pos.x && x <= tile->pos.x + TILE_WIDTH) { 100 if (y >= tile->pos.y && y <= tile->pos.y + TILE_HEIGHT) { 101 return tile; 102 } 103 } 104 } 105 return NULL; 106 } 107 108 bool tile_ground(tile_t *tile) { 109 switch (tile->type) { 110 case TILE_STONE: 111 case TILE_GRASS: 112 case TILE_WOOD_PLATFORM: 113 case TILE_STONE_JOINT: 114 case TILE_SNOW: 115 case TILE_SAND: 116 case TILE_BOX: 117 return true; 118 default: 119 return false; 120 } 121 } 122 123 bool tile_solid(tile_t *tile) { 124 switch (tile->type) { 125 case TILE_STONE: 126 case TILE_GRASS: 127 case TILE_STONE_JOINT: 128 case TILE_SNOW: 129 case TILE_SAND: 130 return true; 131 default: 132 return false; 133 } 134 } 135 136 bool tile_wall(tile_t *tile) { 137 switch (tile->type) { 138 case TILE_STONE_WALL: 139 return true; 140 default: 141 return false; 142 } 143 } 144 145 void tile_free(tile_t *tile) { 146 free(tile); 147 }