From 90c37fe4c93e6f8bdef6bbeb9dae69af601d3a62 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 22 May 2025 14:25:42 -0400 Subject: [PATCH] Fixing up how rotation works with combat and then making the lighting better. --- assets/items.json | 2 +- camera.hpp | 2 +- constants.hpp | 2 ++ gui/fsm.cpp | 10 +++++----- gui/main_ui.hpp | 2 +- raycaster.cpp | 28 +++++++++++++++++++++++++--- 6 files changed, 35 insertions(+), 11 deletions(-) diff --git a/assets/items.json b/assets/items.json index 109fab9..85c74ff 100644 --- a/assets/items.json +++ b/assets/items.json @@ -38,7 +38,7 @@ "foreground": [24, 205, 210], "background": [24, 205, 210] }, - {"_type": "LightSource", "strength": 50, "radius": 2.8}, + {"_type": "LightSource", "strength": 80, "radius": 2.8}, {"_type": "Sprite", "name": "torch_pillar", "width": 256, "height": 256, "scale": 1.0}, {"_type": "Sound", "attack": "pickup", "death": "blank"} ] diff --git a/camera.hpp b/camera.hpp index 12ef8e3..f52a5c8 100644 --- a/camera.hpp +++ b/camera.hpp @@ -17,7 +17,7 @@ struct CameraLOL { rayview(rv) {} Point plan_move(int dir, bool strafe); - void plan_rotate(int dir, float amount=0.5f); + void plan_rotate(int dir, float amount); bool play_rotate(); bool play_move(); diff --git a/constants.hpp b/constants.hpp index 11f8146..5a2f7fc 100644 --- a/constants.hpp +++ b/constants.hpp @@ -63,6 +63,8 @@ constexpr int COMBAT_UI_HEIGHT = SCREEN_HEIGHT - RAY_VIEW_HEIGHT; constexpr int INITIAL_MAP_W = 17; constexpr int INITIAL_MAP_H = 15; +constexpr float DEFAULT_ROTATE=0.25f; + // for the panels/renderer constexpr wchar_t BG_TILE = L'█'; constexpr wchar_t UI_BASE_CHAR = L'█'; diff --git a/gui/fsm.cpp b/gui/fsm.cpp index 5a4dd67..b404974 100644 --- a/gui/fsm.cpp +++ b/gui/fsm.cpp @@ -152,11 +152,11 @@ namespace gui { try_move(1, true); break; case ROTATE_LEFT: - $main_ui.plan_rotate(-1, 0.5f); + $main_ui.plan_rotate(-1, 0.25f); state(State::ROTATING); break; case ROTATE_RIGHT: - $main_ui.plan_rotate(1, 0.5f); + $main_ui.plan_rotate(1, 0.25f); state(State::ROTATING); break; case MAP_OPEN: @@ -212,11 +212,11 @@ namespace gui { state(State::ATTACKING); break; case ROTATE_LEFT: - $main_ui.plan_rotate(-1); + $main_ui.plan_rotate(-1, DEFAULT_ROTATE); state(State::COMBAT_ROTATE); break; case ROTATE_RIGHT: - $main_ui.plan_rotate(1); + $main_ui.plan_rotate(1, DEFAULT_ROTATE); state(State::COMBAT_ROTATE); break; case STOP_COMBAT: @@ -356,7 +356,7 @@ namespace gui { if($map_open) { $map_ui.render($window, $main_ui.$compass_dir); } else { - $mini_map.render($window, $main_ui.$compass_dir); + // $mini_map.render($window, $main_ui.$compass_dir); } } } diff --git a/gui/main_ui.hpp b/gui/main_ui.hpp index e696cab..b78b79a 100644 --- a/gui/main_ui.hpp +++ b/gui/main_ui.hpp @@ -29,7 +29,7 @@ namespace gui { void debug(); void render_debug(); - void plan_rotate(int dir, float amount=0.5f); + void plan_rotate(int dir, float amount); bool play_rotate(); std::optional play_move(); Point plan_move(int dir, bool strafe); diff --git a/raycaster.cpp b/raycaster.cpp index 3bec6eb..e0a8b1c 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -25,6 +25,27 @@ union ColorConv { uint32_t as_int; }; +// from: https://permadi.com/1996/05/ray-casting-tutorial-19/ +// Intensity = (kI/(d+do))*(N*L) +// rcr says: kI = intensity coefficient, d = distance, d0 = fudge term to prevent division by zero, N is surface, L is direction to light from surface +// +// That formula is just "Inverse-square law" (except they don't square, which is physically dubious), and "Lambertian reflectance" ("Diffuse reflection") which sounds fancy but is super standard. All the quoted terms have wikipedia articles +// +// Distance means distance to surface from light. +// +// Intensity = Object Intensity/Distance * Multiplier +// +inline uint32_t new_new_lighting(uint32_t pixel, float dist, int level) { + ColorConv conv{.as_int=pixel}; + float intensity = (float(level) * PERCENT) / (dist+1) * 2.5; + + conv.as_color.r *= intensity; + conv.as_color.g *= intensity; + conv.as_color.b *= intensity; + + return conv.as_int; +} + inline uint32_t old_lighting(uint32_t pixel, float dist, int level) { (void)level; ColorConv conv{.as_int=pixel}; @@ -41,6 +62,7 @@ inline uint32_t old_lighting(uint32_t pixel, float dist, int level) { * bits around. */ inline uint32_t new_lighting(uint32_t pixel, float dist, int level) { + // Intensity = Object Intensity/Distance * Multiplier (void)dist; // ignore for now until I can do more research float factor = float(level) * PERCENT; ColorConv conv{.as_int=pixel}; @@ -310,7 +332,7 @@ void Raycaster::cast_rays() { tex_pos += step; RGBA pixel = texture[texture_height * tex_y + tex_x]; int light_level = lights[map_y][map_x]; - $pixels[pixcoord(x, y)] = new_lighting(pixel, perp_wall_dist, light_level); + $pixels[pixcoord(x, y)] = new_new_lighting(pixel, perp_wall_dist, light_level); } // SET THE ZBUFFER FOR THE SPRITE CASTING @@ -378,11 +400,11 @@ void Raycaster::draw_ceiling_floor() { // FLOOR color = $floor_texture[texture_width * ty + tx]; - $pixels[pixcoord(x, y)] = new_lighting(color, row_distance, light_level); + $pixels[pixcoord(x, y)] = new_new_lighting(color, row_distance, light_level); // CEILING color = $ceiling_texture[texture_width * ty + tx]; - $pixels[pixcoord(x, $height - y - 1)] = new_lighting(color, row_distance, light_level); + $pixels[pixcoord(x, $height - y - 1)] = new_new_lighting(color, row_distance, light_level); } } }