Skip to content

Commit

Permalink
User configurable region min / max brightness
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Feb 15, 2024
1 parent 847bd8e commit 1eb7d30
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions creator/src/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ impl Sidebar {
grid_edit.set_status_text("The size of the region grid in pixels.");
text_layout.add_pair("Grid Size".to_string(), Box::new(grid_edit));

let mut minbr = TheSlider::new(TheId::named("Region Min Brightness"));
minbr.set_value(TheValue::Float(0.3));
minbr.set_continuous(true);
minbr.set_status_text("The minimum brightness of the region for the daylight cycle.");
text_layout.add_pair("Min. Brightness".to_string(), Box::new(minbr));

let mut maxbr = TheSlider::new(TheId::named("Region Max Brightness"));
maxbr.set_value(TheValue::Float(1.0));
maxbr.set_continuous(true);
maxbr.set_status_text("The maximum brightness of the region for the daylight cycle.");
text_layout.add_pair("Max. Brightness".to_string(), Box::new(maxbr));

settings_canvas.set_layout(text_layout);
region_tab.add_canvas("Settings".to_string(), settings_canvas);

Expand Down Expand Up @@ -864,6 +876,20 @@ impl Sidebar {
}
}
}
} else if id.name == "Region Min Brightness" {
if let Some(v) = value.to_f32() {
if let Some(region) = project.get_region_mut(&server_ctx.curr_region) {
region.min_brightness = v;
server.update_region(region);
}
}
} else if id.name == "Region Max Brightness" {
if let Some(v) = value.to_f32() {
if let Some(region) = project.get_region_mut(&server_ctx.curr_region) {
region.max_brightness = v;
server.update_region(region);
}
}
}
// Change the size of the tilemap grid
else if id.name == "Tilemap Grid Edit" {
Expand Down
2 changes: 2 additions & 0 deletions shared/src/client/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub fn add_compiler_client_functions(compiler: &mut TheCompiler) {
settings.center_on_character = None;
}

settings.brightness = update.brightness;

if zoom != 1.0 {
let scaled_width = (buffer.dim().width as f32 / zoom) as i32;
let scaled_height = (buffer.dim().height as f32 / zoom) as i32;
Expand Down
17 changes: 17 additions & 0 deletions shared/src/region.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
use crate::prelude::*;
use theframework::prelude::*;

fn default_min_brightness() -> f32 {
0.3
}

fn default_max_brightness() -> f32 {
1.0
}

#[derive(Serialize, Deserialize, PartialEq, Clone, Copy, Debug)]
pub enum RegionType {
Region2D,
Expand Down Expand Up @@ -30,6 +38,12 @@ pub struct Region {
pub grid_size: i32,
pub scroll_offset: Vec2i,
pub zoom: f32,

#[serde(default = "default_min_brightness")]
pub min_brightness: f32,

#[serde(default = "default_max_brightness")]
pub max_brightness: f32,
}

impl Default for Region {
Expand All @@ -56,6 +70,9 @@ impl Region {
grid_size: 24,
scroll_offset: Vec2i::zero(),
zoom: 1.0,

min_brightness: 0.3,
max_brightness: 1.0,
}
}

Expand Down
2 changes: 2 additions & 0 deletions shared/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ impl Server {
instance.insert_area(area.clone(), &mut self.compiler);
}

instance.set_time(self.world.time);
self.instances.insert(uuid, instance);
}

Expand Down Expand Up @@ -315,6 +316,7 @@ impl Server {
if let Ok(r) = &mut REGIONS.write() {
r.insert(region.id, region.clone());
}
self.set_time(self.world.time);
}

/// Draws the given region instance into the given buffer. This drawing routine is only used by the editor.
Expand Down
48 changes: 46 additions & 2 deletions shared/src/server/region_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct RegionInstance {
last_tick: i64,

draw_settings: RegionDrawSettings,
time: TheTime,
}

impl Default for RegionInstance {
Expand Down Expand Up @@ -76,6 +77,7 @@ impl RegionInstance {
last_tick: 0,

draw_settings: RegionDrawSettings::new(),
time: TheTime::default(),
}
}

Expand Down Expand Up @@ -104,13 +106,15 @@ impl RegionInstance {
pub fn tick(&mut self, time: TheTime) {
self.debug_modules.clear();
self.sandbox.clear_debug_messages();
self.draw_settings.time = time;
self.time = time;
self.draw_settings.brightness = self.get_brightness();

if let Some(update) = UPDATES.write().unwrap().get_mut(&self.id) {
for character in update.characters.values_mut() {
character.moving = None;
character.move_delta = 0.0;
}
update.brightness = self.draw_settings.brightness;
}

self.sandbox.level.clear_blocking();
Expand Down Expand Up @@ -687,6 +691,46 @@ impl RegionInstance {

/// If the user changes the time w/o the server running, we have to update the draw settings manually.
pub fn set_time(&mut self, time: TheTime) {
self.draw_settings.time = time;
self.time = time;
self.draw_settings.brightness = self.get_brightness();
}

/// Calculates the brightness of the region.
pub fn get_brightness(&self) -> f32 {
let minutes = self.time.total_minutes();

let sunrise = 300; // 5:00 am
let sunset = 1200; // 8:00 pm
let transition_duration = 60; // 1 hour

let daylight_start = sunrise + transition_duration;
let daylight_end = sunset + transition_duration;

let br = if minutes < sunrise || minutes > daylight_end {
0.0
} else if minutes >= sunrise && minutes <= daylight_start {
// transition from darkness to daylight
let transition_start = sunrise;
let time_since_transition_start = minutes - transition_start;

time_since_transition_start as f32 / transition_duration as f32
} else if minutes >= sunset && minutes <= daylight_end {
// transition from daylight to darkness
let transition_start = sunset;
let time_since_transition_start = minutes - transition_start;

1.0 - time_since_transition_start as f32 / transition_duration as f32
} else {
1.0
};

if let Some(region) = REGIONS.read().unwrap().get(&self.id) {
br.clamp(
min(region.min_brightness, region.max_brightness),
max(region.min_brightness, region.max_brightness),
)
} else {
br
}
}
}
37 changes: 3 additions & 34 deletions shared/src/tiledrawer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct RegionDrawSettings {
pub delta_in_tick: f32,
pub offset: Vec2i,
pub delta: f32,
pub time: TheTime,
pub brightness: f32,

pub center_on_character: Option<Uuid>,
}
Expand All @@ -22,7 +22,7 @@ impl RegionDrawSettings {
delta_in_tick: 0.0,
offset: Vec2i::zero(),
delta: 0.0,
time: TheTime::default(),
brightness: 1.0,
center_on_character: None,
}
}
Expand Down Expand Up @@ -216,8 +216,7 @@ impl TileDrawer {
}
}

let minutes = settings.time.minutes as i32 + settings.time.hours as i32 * 60;
let brightness = Self::get_brightness(minutes).clamp(0.3, 1.0);
let brightness = settings.brightness;

color[0] = (color[0] as f32 * brightness) as u8;
color[1] = (color[1] as f32 * brightness) as u8;
Expand Down Expand Up @@ -399,34 +398,4 @@ impl TileDrawer {
}
time
}

/// Get the brightness of the current time.
fn get_brightness(minutes: i32) -> f32 {
let sunrise = 300; // 5:00 am
let sunset = 1200; // 8:00 pm
let transition_duration = 60; // 1 hour

let daylight_start = sunrise + transition_duration;
let daylight_end = sunset + transition_duration;

if minutes < sunrise || minutes > daylight_end {
return 0.0; // it's dark outside
}

if minutes >= sunrise && minutes <= daylight_start {
// transition from darkness to daylight
let transition_start = sunrise;
let time_since_transition_start = minutes - transition_start;

time_since_transition_start as f32 / transition_duration as f32
} else if minutes >= sunset && minutes <= daylight_end {
// transition from daylight to darkness
let transition_start = sunset;
let time_since_transition_start = minutes - transition_start;

1.0 - time_since_transition_start as f32 / transition_duration as f32
} else {
1.0
}
}
}
2 changes: 2 additions & 0 deletions shared/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct RegionUpdate {
pub items: FxHashMap<Uuid, ItemUpdate>,

pub server_tick: i64,
pub brightness: f32,
}

impl Default for RegionUpdate {
Expand All @@ -27,6 +28,7 @@ impl RegionUpdate {
characters: FxHashMap::default(),
items: FxHashMap::default(),
server_tick: 0,
brightness: 1.0,
}
}

Expand Down

0 comments on commit 1eb7d30

Please sign in to comment.