optional darken by distance and viewshed multiplier

This commit is contained in:
Llywelwyn 2023-08-23 00:00:44 +01:00
parent f8c74ea6f9
commit 2c7671b348
7 changed files with 40 additions and 16 deletions

View file

@ -1,4 +1,8 @@
pub const NON_VISIBLE_MULTIPLIER: f32 = 0.7;
pub const NON_VISIBLE_MULTIPLIER: f32 = 0.3;
pub const MAX_DARKENING: f32 = 0.3;
pub const START_DARKEN_AT_N_TILES: f32 = 9.0;
pub const MAX_DARKEN_AT_N_TILES: f32 = 12.0;
pub const BLOODSTAIN_COLOUR: (u8, u8, u8) = (153, 0, 0);
// DEFAULT THEME
@ -14,7 +18,7 @@ pub const GRAVEL_COLOUR: (u8, u8, u8) = (26, 26, 53);
pub const ROAD_COLOUR: (u8, u8, u8) = (8, 38, 40);
pub const GRASS_COLOUR: (u8, u8, u8) = (9, 65, 6);
pub const FOLIAGE_COLOUR: (u8, u8, u8) = (5, 60, 5);
pub const HEAVY_FOLIAGE_COLOUR: (u8, u8, u8) = (5, 55, 5);
pub const HEAVY_FOLIAGE_COLOUR: (u8, u8, u8) = (5, 60, 5);
pub const SAND_COLOUR: (u8, u8, u8) = (70, 70, 21);
pub const SHALLOW_WATER_COLOUR: (u8, u8, u8) = (24, 47, 99);
pub const DEEP_WATER_COLOUR: (u8, u8, u8) = (18, 33, 63);

View file

@ -10,6 +10,7 @@ pub use interval_spawning_system::try_spawn_interval;
pub mod dungeon;
pub use dungeon::{level_transition, MasterDungeonMap};
pub mod themes;
pub use colours::NON_VISIBLE_MULTIPLIER;
// FIXME: If the map size gets too small, entities stop being rendered starting from the right.
// i.e. on a map size of 40*40, only entities to the left of the player are rendered.

View file

@ -1,8 +1,10 @@
use super::{colours::*, glyphs::*, Map, TileType};
use rltk::RGB;
use super::{colours::*, glyphs::*, Map, Point, TileType};
use rltk::prelude::*;
use std::ops::{Add, Mul};
pub fn get_tile_renderables_for_id(idx: usize, map: &Map) -> (rltk::FontCharType, RGB, RGB) {
const DARKEN_TILES_BY_DISTANCE: bool = true;
pub fn get_tile_renderables_for_id(idx: usize, map: &Map, other_pos: Option<Point>) -> (rltk::FontCharType, RGB, RGB) {
let (glyph, mut fg, mut bg) = match map.id {
2 => get_forest_theme_renderables(idx, map),
_ => get_default_theme_renderables(idx, map),
@ -19,6 +21,11 @@ pub fn get_tile_renderables_for_id(idx: usize, map: &Map) -> (rltk::FontCharType
(fg, bg) = apply_colour_offset(fg, bg, map, idx);
bg = apply_bloodstain_if_necessary(bg, map, idx);
(fg, bg) = darken_if_not_visible(fg, bg, map, idx);
if other_pos.is_some() && DARKEN_TILES_BY_DISTANCE {
let distance =
darken_by_distance(Point::new(idx as i32 % map.width, idx as i32 / map.width), other_pos.unwrap());
(fg, bg) = (fg.mul(distance), bg.mul(distance));
}
return (glyph, fg, bg);
}
@ -241,3 +248,11 @@ pub fn multiply_by_float(rgb: rltk::RGB, offsets: (f32, f32, f32)) -> RGB {
return rltk::RGB::from_f32(r, g, b);
}
fn darken_by_distance(pos: Point, other_pos: Point) -> f32 {
let distance = DistanceAlg::Pythagoras.distance2d(pos, other_pos) as f32; // Get distance in tiles.
let interp_factor = (distance - START_DARKEN_AT_N_TILES)
/ (MAX_DARKEN_AT_N_TILES * crate::spawner::VIEWSHED_MOD - START_DARKEN_AT_N_TILES);
let interp_factor = interp_factor.max(0.0).min(1.0); // Clamp [0-1]
return 1.0 - interp_factor * (1.0 - MAX_DARKENING);
}