Nimbin[12]?SDL & Graphics / mapcreator / commits / 7d065bc

mapcreator git · master

SDL3 2.5D game and engine using assets, with map editor

sdl3 c++ game engine map-editor cmake · first commit 2025-10-31 · last commit 2026-09-07 (1 month ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/sdl-spieleentwicklung/map_creator

C++ 99.3%
git clone https://git.christianimmanuel.de/sdl-graphics/mapcreator.gitwget https://git.christianimmanuel.de/sdl-graphics/mapcreator/archive/mapcreator.tar.gz

Add Enemy detection radius

Christian Immanuel · 2026-02-18 16:26 · 7d065bcc23a11c2c790409ce9cfc4d5a81a42033

 src/character/enemy.cpp | 31 ++++++++++++++++++++++++++-----
 src/character/enemy.hpp |  6 ++++++
 2 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/src/character/enemy.cpp b/src/character/enemy.cpp
index 34b9918..4e054af 100644
--- a/src/character/enemy.cpp
+++ b/src/character/enemy.cpp
@@ -35,6 +35,8 @@ Enemy::Enemy(WeaponType weapon_type, WeaponMaterial weapon_material, SDL_FPoint
 
         id = id_next++;
 
+        detection_radius = 150 + rand() % 51;
+
         speed = 40.0f;
         run = 1.4f;
         direction = WalkDirection::DOWN;
@@ -425,9 +427,18 @@ void Enemy::Decide(Character& player) {
     //float  d_y  = player.collider_world.y - collider_world.y;
     //size_t dc_x = static_cast<size_t>(abs(d_x));
     //size_t dc_y = static_cast<size_t>(abs(d_y));
-    EnemyMovingType r = (move_focus != EnemyMovingType::NONE) ? move_focus: static_cast<EnemyMovingType>(1 + (rand() % static_cast<int>(EnemyMovingType::LAST)-1));
+    EnemyMovingType r;
 
-    r = EnemyMovingType::CHARACTER;
+    if (is_character_in_range) {
+        r = EnemyMovingType::CHARACTER;
+    } else if (move_focus != EnemyMovingType::NONE) {
+        r = move_focus;
+    } else {
+        // pick random type excluding CHARACTER
+        do {
+            r = static_cast<EnemyMovingType>(1 + rand() % (static_cast<int>(EnemyMovingType::LAST) - 1));
+        } while (r == EnemyMovingType::CHARACTER);
+    }
     set_MoveLogic(r, player);
 }
 
@@ -436,6 +447,15 @@ inline bool same_corner(const SDL_FPoint& a, const SDL_FPoint& b, float epsilon
 }
 
 
+
+inline bool Enemy::character_InRange(SDL_FPoint char_pos) const {
+    float dx = char_pos.x - collider_world.x;
+    float dy = char_pos.y - collider_world.y;
+    float distance_squared = dx * dx + dy * dy;
+    return distance_squared <= detection_radius * detection_radius;
+}
+
+
 bool
 Enemy::set_CollisinnWithEnemy(const SDL_FRect& rect) {
 
@@ -979,6 +999,10 @@ Enemy::Update(Character& player) {
 
     point_in_reach = false;
 
+    is_character_in_range = character_InRange(player.collider_world);
+    if (!is_character_in_range && stats.is_attacking)
+        attack_Stop();
+
     // IS DEAD
     if (stats.health <= 0) {
         if (status == EnemyStatus::DYING ||
@@ -1013,9 +1037,6 @@ Enemy::Update(Character& player) {
         }
     }
 
-
-    std::cout << "ID: " << id << std::endl;
-
     float  d_x  = moving_logic.position.x - collider_world.x;
     float  d_y  = moving_logic.position.y - collider_world.y;
     /*
diff --git a/src/character/enemy.hpp b/src/character/enemy.hpp
index cdd3a60..e21364a 100644
--- a/src/character/enemy.hpp
+++ b/src/character/enemy.hpp
@@ -95,6 +95,8 @@ class Enemy {
     static size_t tile_size;
     float  arm_reach_radius;
 
+    float detection_radius;
+
     public:
     static SDL_FPoint colider_default;
     static std::vector<Enemy*> enemies;
@@ -122,6 +124,8 @@ class Enemy {
     float dist_min = 24;
     float dist_max = 32;
 
+    bool is_character_in_range = false;
+
     std::vector<SDL_FPoint> next_moves;
     SDL_Point last_player_pos{};
     SDL_Point last_target_pos{};
@@ -213,6 +217,8 @@ class Enemy {
     bool
     is_Colliding(const SDL_FRect& rect);
 
+    inline bool character_InRange(SDL_FPoint char_pos) const;
+
     void
     compute_MapCollisionInterval(size_t cx, size_t cy, size_t max_x, size_t max_y);