You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
262 lines
8.8 KiB
262 lines
8.8 KiB
#include "systems.hpp"
|
|
#include <fmt/core.h>
|
|
#include <string>
|
|
#include <cmath>
|
|
#include "rand.hpp"
|
|
#include "collider.hpp"
|
|
#include "events.hpp"
|
|
#include "ftxui/screen/color.hpp"
|
|
#include "ftxui/screen/terminal.hpp" // for SetColorSupport, Color, TrueColor
|
|
#include "dbc.hpp"
|
|
|
|
const bool DEBUG_MAP=false;
|
|
|
|
using std::string;
|
|
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) {
|
|
using std::min, std::max, std::clamp;
|
|
|
|
auto &lighting = game_map.lighting();
|
|
std::vector<Point> has_light;
|
|
|
|
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();
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player) {
|
|
// TODO: this will be on each enemy not a global thing
|
|
const auto &config = world.get_the<EnemyConfig>();
|
|
const auto &player_position = world.get<Position>(player.entity);
|
|
game_map.set_target(player_position.location);
|
|
game_map.make_paths();
|
|
|
|
world.query<Position, Motion>([&](const auto &ent, auto &position, auto &motion) {
|
|
if(ent != player.entity) {
|
|
Point out = position.location; // copy
|
|
if(game_map.distance(out) < config.HEARING_DISTANCE) {
|
|
game_map.neighbors(out, false);
|
|
motion = { int(out.x - position.location.x), int(out.y - position.location.y)};
|
|
}
|
|
}
|
|
});
|
|
game_map.clear_target(player_position.location);
|
|
}
|
|
|
|
void System::init_positions(DinkyECS::World &world) {
|
|
auto &collider = world.get_the<spatial_map>();
|
|
|
|
world.query<Position, Combat>([&](const auto &ent, auto &pos, auto &combat) {
|
|
if(!combat.dead) {
|
|
collider.insert(pos.location, ent);
|
|
}
|
|
});
|
|
|
|
world.query<Position, Loot>([&](const auto &ent, auto &pos, auto &loot) {
|
|
collider.insert(pos.location, ent);
|
|
});
|
|
}
|
|
|
|
inline void move_entity(spatial_map &collider, Map &game_map, Position &position, Motion &motion, DinkyECS::Entity ent) {
|
|
Point move_to = {
|
|
position.location.x + motion.dx,
|
|
position.location.y + motion.dy
|
|
};
|
|
motion = {0,0}; // clear it after getting it
|
|
|
|
// it's a wall, skip
|
|
if(!game_map.can_move(move_to)) return;
|
|
// there's collision skip
|
|
if(collider.occupied(move_to)) return;
|
|
|
|
// all good, do the move
|
|
collider.move(position.location, move_to, ent);
|
|
position.location = move_to;
|
|
}
|
|
|
|
void System::motion(DinkyECS::World &world, Map &game_map) {
|
|
auto &collider = world.get_the<spatial_map>();
|
|
|
|
world.query<Position, Motion>([&](const auto &ent, auto &position, auto &motion) {
|
|
// don't process entities that don't move
|
|
if(motion.dx != 0 || motion.dy != 0) {
|
|
move_entity(collider, game_map, position, motion, ent);
|
|
}
|
|
});
|
|
}
|
|
|
|
void System::death(DinkyECS::World &world) {
|
|
auto &collider = world.get_the<spatial_map>();
|
|
|
|
world.query<Position, Combat>([&](const auto &ent, auto &position, auto &combat) {
|
|
// bring out yer dead
|
|
if(combat.hp <= 0 && !combat.dead) {
|
|
combat.dead = true;
|
|
// take them out of collision map
|
|
collider.remove(position.location);
|
|
|
|
// remove their motion so they're dead
|
|
world.remove<Motion>(ent);
|
|
}
|
|
});
|
|
}
|
|
|
|
void System::collision(DinkyECS::World &world, Player &player) {
|
|
auto& collider = world.get_the<spatial_map>();
|
|
const auto& player_position = world.get<Position>(player.entity);
|
|
auto& player_combat = world.get<Combat>(player.entity);
|
|
|
|
// this is guaranteed to not return the given position
|
|
auto [found, nearby] = collider.neighbors(player_position.location);
|
|
|
|
if(found) {
|
|
|
|
for(auto entity : nearby) {
|
|
if(world.has<Combat>(entity)) {
|
|
auto& enemy_combat = world.get<Combat>(entity);
|
|
|
|
Events::Combat result {
|
|
player_combat.attack(enemy_combat),
|
|
enemy_combat.attack(player_combat)
|
|
};
|
|
|
|
world.send<Events::GUI>(Events::GUI::COMBAT, entity, result);
|
|
} else if(world.has<Loot>(entity)) {
|
|
auto loot = world.get<Loot>(entity);
|
|
auto &loot_pos = world.get<Position>(entity);
|
|
auto &inventory = world.get<Inventory>(player.entity);
|
|
auto &light = world.get<LightSource>(player.entity);
|
|
|
|
world.send<Events::GUI>(Events::GUI::LOOT, entity, loot);
|
|
inventory.gold += loot.amount;
|
|
light.strength = 100;
|
|
collider.remove(loot_pos.location);
|
|
} else {
|
|
println("UNKNOWN COLLISION TYPE {}", entity);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void System::draw_entities(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canvas, const Point &cam_orig, size_t view_x, size_t view_y) {
|
|
auto &lighting = game_map.lighting();
|
|
|
|
world.query<Position, Tile>([&](const auto &ent, auto &pos, auto &tile) {
|
|
if(pos.location.x >= cam_orig.x && pos.location.x <= cam_orig.x + view_x
|
|
&& pos.location.y >= cam_orig.y && pos.location.y <= cam_orig.y + view_y) {
|
|
Point loc = game_map.map_to_camera(pos.location, cam_orig);
|
|
int light_value = lighting[pos.location.y][pos.location.x];
|
|
|
|
// the 2 and 4 are from ftxui::Canvas since it does a kind of "subpixel" drawing
|
|
canvas.DrawText(loc.x*2, loc.y*4, tile.chr, [light_value](auto &pixel) {
|
|
pixel.foreground_color = Color::HSV(255, 200, light_value + 20);
|
|
pixel.background_color = Color::HSV(30, 20, light_value / 5);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
void System::draw_map(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canvas, size_t view_x, size_t view_y) {
|
|
const auto& config = world.get_the<MapConfig>();
|
|
const auto& player = world.get_the<Player>();
|
|
const auto& player_position = world.get<Position>(player.entity);
|
|
Point start = game_map.center_camera(player_position.location, view_x, view_y);
|
|
auto &walls = game_map.walls();
|
|
auto &paths = game_map.paths();
|
|
auto &lighting = game_map.lighting();
|
|
|
|
size_t end_x = std::min(view_x, game_map.width() - start.x);
|
|
size_t end_y = std::min(view_y, game_map.height() - start.y);
|
|
|
|
for(size_t x = 0; x < end_x; ++x) {
|
|
for(size_t y = 0; y < end_y; ++y) {
|
|
string tile = walls[start.y+y][start.x+x] == 1 ? config.WALL_TILE : config.FLOOR_TILE;
|
|
// "WALL_TILE": "\u2591",
|
|
// "WALL_TILE": "\ua5b8",
|
|
int light_value = lighting[start.y+y][start.x+x];
|
|
|
|
if(tile == config.WALL_TILE) {
|
|
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];
|
|
string num = format("{:x}", dnum);
|
|
num = num.size() > 2 ? "*" : num;
|
|
|
|
canvas.DrawText(x * 2, y * 4, num, [dnum, light_value](auto &pixel) {
|
|
pixel.foreground_color = Color::HSV(dnum * 20, 150, 200);
|
|
pixel.background_color = Color::HSV(30, 20, light_value / 5);
|
|
});
|
|
} else {
|
|
canvas.DrawText(x * 2, y * 4, tile, [light_value](auto &pixel) {
|
|
pixel.foreground_color = Color::HSV(30, 40, light_value);
|
|
pixel.background_color = Color::HSV(30, 20, light_value / 5);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
System::draw_entities(world, game_map, canvas, start, view_x, view_y);
|
|
}
|
|
|