Light works with multiple sources and strengths, walls are faked out but I think I may keep that to make it easier to play.

This commit is contained in:
Zed A. Shaw 2024-11-27 04:02:57 -05:00
parent 6174df5ec7
commit 0e8a2e520a
5 changed files with 55 additions and 14 deletions

View file

@ -20,23 +20,49 @@ 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();
using std::min, std::max, std::clamp;
auto &lighting = game_map.lighting();
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];
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});
}
for(auto &row : lighting) {
for(size_t i = 0; i < row.size(); i++) {
row[i] = LIGHT_MIN;
}
}
world.query<Position, LightSource>([&](const auto &ent, auto &position, auto &lightsource) {
game_map.set_target(position.location);
});
game_map.make_paths();
auto &paths = game_map.paths();
world.query<Position, LightSource>([&](const auto &ent, auto &position, auto &lightsource) {
game_map.clear_target(position.location);
int strength = 255 - lightsource.strength;
size_t dist = size_t((float(lightsource.strength) / 255.0) * 3) + 1;
size_t min_x = max(position.location.x, dist) - dist;
size_t max_x = min(position.location.x + dist, game_map.width() - 1);
size_t min_y = max(position.location.y, dist) - dist;
size_t max_y = min(position.location.y + dist, game_map.height() - 1);
for(size_t x = min_x; x <= max_x; ++x) {
for(size_t y = min_y; y <= max_y; ++y) {
int dnum = paths[y][x];
int light = std::clamp(255 - (strength * dnum), LIGHT_MIN, LIGHT_MAX);
if(lighting[y][x] < light) {
lighting[y][x] = light;
if(light > LIGHT_MIN) {
has_light.push_back({x, y});
}
}
}
}
});
const int UNPATH = game_map.limit();