From a19bc47904bd3b16fc7aeddb126d355741b16013 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 7 Feb 2025 11:05:15 -0500 Subject: [PATCH] FINALLY fix that stupid bug. The cause was two-fold: I was giving every 'enemy' a sprite, but that automatically included the player in the list of enemies, which meant that I was rendering the player's sprite while moving. Then in the sprite casting loop I was rendering things at 0. --- camera.cpp | 4 +++- camera.hpp | 2 +- gui.cpp | 7 +++---- raycaster.cpp | 5 +++++ 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/camera.cpp b/camera.cpp index d107b93..b1d9cea 100644 --- a/camera.cpp +++ b/camera.cpp @@ -2,7 +2,7 @@ #include #include -void CameraLOL::plan_move(Raycaster &rayview, int dir, bool strafe) { +Point CameraLOL::plan_move(Raycaster &rayview, int dir, bool strafe) { t = 0.0; if(strafe) { targetX = rayview.$posX + int(-rayview.$dirY * 1.5 * dir); @@ -11,6 +11,8 @@ void CameraLOL::plan_move(Raycaster &rayview, int dir, bool strafe) { targetX = rayview.$posX + int(rayview.$dirX * 1.5 * dir); targetY = rayview.$posY + int(rayview.$dirY * 1.5 * dir); } + + return {size_t(targetX), size_t(targetY)}; } void CameraLOL::plan_rotate(Raycaster &rayview, int dir) { diff --git a/camera.hpp b/camera.hpp index 3e92def..85b374d 100644 --- a/camera.hpp +++ b/camera.hpp @@ -12,7 +12,7 @@ struct CameraLOL { double targetPlaneX = 0.0; double targetPlaneY = 0.0; - void plan_move(Raycaster &rayview, int dir, bool strafe); + Point plan_move(Raycaster &rayview, int dir, bool strafe); void plan_rotate(Raycaster &rayview, int dir); bool play_rotate(Raycaster &rayview); diff --git a/gui.cpp b/gui.cpp index 19aabf4..c578a45 100644 --- a/gui.cpp +++ b/gui.cpp @@ -1,3 +1,4 @@ +#define FSM_DEBUG 1 #include "gui.hpp" #include #include @@ -96,9 +97,7 @@ namespace gui { void FSM::try_move(int dir, bool strafe) { // prevent moving into occupied space - $camera.plan_move($rayview, dir, strafe); - - Point move_to{size_t($camera.targetX), size_t($camera.targetY)}; + Point move_to = $camera.plan_move($rayview, dir, strafe); if($level.map->can_move(move_to) && !$level.collision->occupied(move_to)) { state(State::MOVING); @@ -221,8 +220,8 @@ namespace gui { } void FSM::run_systems() { - System::motion($level); System::enemy_pathing($level); + System::motion($level); System::collision($level); System::death($level); } diff --git a/raycaster.cpp b/raycaster.cpp index 06a77cf..a0eb5c7 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -67,6 +67,7 @@ void Raycaster::sprite_casting(sf::RenderTarget &target) { // sort sprites from far to close auto sprite_order = $level.collision->distance_sorted({(size_t)$posX, (size_t)$posY}); + // after sorting the sprites, do the projection for(auto& rec : sprite_order) { if(!$sprites.contains(rec.second)) continue; @@ -332,9 +333,13 @@ void Raycaster::set_level(GameLevel level) { $level = level; auto& tiles = $level.map->tiles(); auto world = $level.world; + auto& player = world->get_the(); $map = $textures.convert_char_to_texture(tiles.$tile_ids); world->query([&](const auto ent, auto &pos) { + // player doesn't need a sprite + if(player.entity == ent) return; + if(world->has(ent)) { fmt::println("enemy: {}, pos={},{}", ent, pos.location.x, pos.location.y); auto sprite_txt = $textures.sprite_textures.at("evil_eye");