Add transperency when units are behind

This commit is contained in:
2024-01-15 13:16:24 +01:00
parent 0f33ed9fc0
commit ac7525c42a
5 changed files with 52 additions and 4 deletions

View File

@@ -465,6 +465,27 @@ void bzTileMapDrawCollisions(BzTileMap *map) {
}
}
void bzTileMapClearCollisions(BzTileMap *map) {
BZ_ASSERT(map->collisionMap);
for (i32 y = 0; y < map->width; y++) {
for (i32 x = 0; x < map->height; x++) {
i32 idxOffset = y * map->width + x;
u8 *cell = map->collisionMap + idxOffset;
*cell = 0;
}
}
}
void bzTileMapClearCollisionLayer(BzTileMap *map, i32 layer) {
BZ_ASSERT(map->collisionMap);
BZ_ASSERT(layer >= 0 && layer < 8);
for (i32 y = 0; y < map->width; y++) {
for (i32 x = 0; x < map->height; x++) {
i32 idxOffset = y * map->width + x;
u8 *cell = map->collisionMap + idxOffset;
*cell &= ~(1 << layer);
}
}
}
bool bzTileMapHasAnyCollision(BzTileMap *map, i32 x, i32 y) {
if (!map->collisionMap) return false;
i32 idx = y * map->width + x;

View File

@@ -132,6 +132,8 @@ bool bzTileMapCanRayCastLine(BzTileMap *map, Vector2 from, Vector2 to);
void bzTileMapDraw(BzTileMap *map);
void bzTileMapDrawCollisions(BzTileMap *map);
void bzTileMapClearCollisions(BzTileMap *map);
void bzTileMapClearCollisionLayer(BzTileMap *map, i32 layer);
bool bzTileMapHasAnyCollision(BzTileMap *map, i32 x, i32 y);
bool bzTileMapHasCollision(BzTileMap *map, i32 layer, i32 x, i32 y);
void bzTileMapSetCollisions(BzTileMap *map, bool collision, i32 layer, i32 startX, i32 startY, i32 sizeX, i32 sizeY);

View File

@@ -4,6 +4,7 @@
enum {
COLL_LAYER_TERRAIN = 0,
COLL_LAYER_BUILDINGS = 1,
COLL_LAYER_TRANSPARENCY = 7,
};
#endif //PIXELDEFENSE_CONSTANTS_H

View File

@@ -18,6 +18,7 @@ typedef struct DrawData {
Rectangle dst;
Vector2 origin;
f32 rotation;
bool canHaveAlpha;
} DrawData;
typedef struct Options {

View File

@@ -350,7 +350,8 @@ static void renderGame(Game *game, float dt) {
BeginMode2D(game->camera);
// Map
bzTileMapDraw(&game->map);
BzTileMap *map = &game->map;
bzTileMapDraw(map);
// Ground UI
drawPlayerInputUIGround();
// Entities
@@ -380,12 +381,22 @@ static void renderGame(Game *game, float dt) {
src.height -= 0.02f;
if (t[i].flipX) src.width *= -1.0f;
if (t[i].flipY) src.height *= -1.0f;
drawData[drawIdx++] = (DrawData) {
DrawData draw = (DrawData) {
.src = src,
.dst = dst,
.origin = origin,
.rotation = r[i]
.rotation = r[i],
.canHaveAlpha = true,
};
if (ecs_has_id(ECS, it.entities[i], ecs_id(Unit)) ||
ecs_has_id(ECS, it.entities[i], ecs_id(Arm))) {
Vector2 pos = {dst.x, dst.y};
Vec2i cellPos = bzTileMapPosToTile(map, pos);
bzTileMapSetCollisions(map, true, COLL_LAYER_TRANSPARENCY,
cellPos.x, cellPos.y, 1, 1);
draw.canHaveAlpha = false;
}
drawData[drawIdx++] = draw;
}
}
BZ_ASSERT(drawIdx == numDraws);
@@ -394,8 +405,20 @@ static void renderGame(Game *game, float dt) {
Texture2D tex = game->tileset.tiles;
for (i32 i = 0; i < numDraws; i++) {
DrawData draw = drawData[i];
DrawTexturePro(tex, draw.src, draw.dst, draw.origin, draw.rotation, WHITE);
Vector2 pos = {
draw.dst.x,
draw.dst.y - draw.dst.height * 0.5f,
};
Color c = WHITE;
if (draw.canHaveAlpha) {
Vec2i mapPos = bzTileMapPosToTile(map, pos);
if (bzTileMapHasCollision(map, COLL_LAYER_TRANSPARENCY, mapPos.x, mapPos.y)) {
c.a = 180;
}
}
DrawTexturePro(tex, draw.src, draw.dst, draw.origin, draw.rotation, c);
}
bzTileMapClearCollisionLayer(&game->map, COLL_LAYER_TRANSPARENCY);
Vector2 target = GetMousePosition();
target = GetScreenToWorld2D(target, game->camera);
static f32 elapsed = 0;