Lighting now works, now to get multiple lights.

This commit is contained in:
Zed A. Shaw 2024-11-26 05:03:49 -05:00
parent 4ceacecfda
commit 6174df5ec7
4 changed files with 36 additions and 13 deletions

View file

@ -16,17 +16,39 @@ using namespace fmt;
using namespace components;
using ftxui::Color;
const int LIGHT_MIN = 20;
const int LIGHT_MAX = 160;
void System::lighting(DinkyECS::World &world, Map &game_map, Player &player) {
const auto& player_light = world.get<LightSource>(player.entity);
auto &paths = game_map.paths();
auto &lighting = game_map.lighting();
const int LIGHT_MIN = 10;
const int LIGHT_MAX = 110;
std::vector<Point> has_light;
for(size_t x = 0; x < game_map.width(); ++x) {
for(size_t y = 0; y < game_map.height(); ++y) {
int dnum = paths[y][x];
lighting[y][x] = std::clamp(255 - (dnum * player_light.strength), LIGHT_MIN, LIGHT_MAX);
int light = std::clamp(255 - (dnum * player_light.strength), LIGHT_MIN, LIGHT_MAX);
lighting[y][x] = light;
if(light > LIGHT_MIN) {
has_light.push_back({x,y});
}
}
}
const int UNPATH = game_map.limit();
for(auto point : has_light) {
for(int i = -1; i <= 1; i++) {
for(int j = -1; j <= 1; j++) {
if(!game_map.inmap(point.x+i, point.y+j)) continue;
if(paths[point.y+j][point.x+i] == UNPATH) {
lighting[point.y+j][point.x+i] = LIGHT_MAX;
}
}
}
}
}
@ -183,9 +205,14 @@ void System::draw_map(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canv
int light_value = lighting[start.y+y][start.x+x];
if(tile == config.WALL_TILE) {
canvas.DrawText(x * 2, y * 4, config.WALL_TILE, [](auto &pixel) {
pixel.foreground_color = Color::HSV(230, 20, 10);
pixel.background_color = Color::HSV(230, 20, 30);
canvas.DrawText(x * 2, y * 4, config.WALL_TILE, [light_value](auto &pixel) {
if(light_value > LIGHT_MIN) {
pixel.foreground_color = Color::HSV(230, 20, 10);
pixel.background_color = Color::HSV(230, 20, light_value / 2);
} else {
pixel.foreground_color = Color::HSV(230, 20, 20);
pixel.background_color = Color::HSV(230, 20, 30);
}
});
} else if(DEBUG_MAP) {
int dnum = paths[start.y+y][start.x+x];