Nimbin[12]?SDL & Graphics / mapcreator / commits / d9204a1

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

Update prep enemy for object attack

Christian Immanuel · 2025-12-29 19:27 · d9204a1a30e57704cbec7c09b0a1b3c7c4e7dcf4

 saves/save.collissions         | Bin 256016 -> 256016 bytes
 saves/save.map                 | Bin 984232 -> 984624 bytes
 saves/save.tileset_collissions | Bin 214812 -> 214812 bytes
 saves/save.tileset_objects     | Bin 27624 -> 27624 bytes
 src/character/enemy.cpp        |  69 +++++++++++++++++++++--------------------
 src/character/enemy.hpp        |  18 +++++++++--
 src/game/state_playing.cpp     |   2 +-
 src/map/map.hpp                |   4 +++
 8 files changed, 57 insertions(+), 36 deletions(-)

diff --git a/saves/save.collissions b/saves/save.collissions
index 612e4c6..6d68bc7 100644
Binary files a/saves/save.collissions and b/saves/save.collissions differ
diff --git a/saves/save.map b/saves/save.map
index 39beda8..c24cfe0 100644
Binary files a/saves/save.map and b/saves/save.map differ
diff --git a/saves/save.tileset_collissions b/saves/save.tileset_collissions
index f0f44a6..4f3ecd4 100644
Binary files a/saves/save.tileset_collissions and b/saves/save.tileset_collissions differ
diff --git a/saves/save.tileset_objects b/saves/save.tileset_objects
index 40198ef..b3cb200 100644
Binary files a/saves/save.tileset_objects and b/saves/save.tileset_objects differ
diff --git a/src/character/enemy.cpp b/src/character/enemy.cpp
index dceac29..5f11fc3 100644
--- a/src/character/enemy.cpp
+++ b/src/character/enemy.cpp
@@ -87,6 +87,7 @@ Enemy::Init(SDL_Renderer* renderer) {
     }
     x = new_position.x - collider.x - collider.w * 0.5; 
     y = new_position.y - collider.y - collider.h * 0.5; 
+
 }
 
 
@@ -738,12 +739,12 @@ Enemy::set_NextMoves(const std::vector<bool>& grid) {
 
 
 void
-Enemy::set_AlignPosition(const Character& player) {
+Enemy::set_AlignPosition(const SDL_FPoint& pos) {
     float dist = dist_min + (dist_max-dist_min)/2;
     //std::cout << "DS: " << dist.x << " " << dist.y <<std::endl;
 
-    const float px = player.collider_world.x;
-    const float py = player.collider_world.y;
+    const float px = pos.x;
+    const float py = pos.y;
 
     float nx = collider_world.x;
     float ny = collider_world.y;
@@ -792,15 +793,14 @@ Enemy::set_AlignPosition(const Character& player) {
 }
 
 void
-Enemy::update(Character& player) {
-    handle_Animation();
-
+Enemy::Update(Character& player) {
 
     if (stats.health <= 0) {
         if (status == EnemyStatus::DYING ||
                    status == EnemyStatus::DEAD) {
             death();
         }
+        handle_Animation();
         return;
     }
 
@@ -812,22 +812,29 @@ Enemy::update(Character& player) {
         return;
     }
 
-    float  d_x  = player.collider_world.x - collider_world.x;
-    float  d_y  = player.collider_world.y - collider_world.y;
+    if (moving_logic.attack_type == EnemyMovingType::NONE) {
+        moving_logic.attack_type = EnemyMovingType::CHARACTER;
+        moving_logic.object   = static_cast<void*>(&player);
+        moving_logic.position = player.collider_world;
+    } else
+    if (moving_logic.attack_type == EnemyMovingType::CHARACTER) {
+        if ( player.collider_world.x != moving_logic.position.x ||
+             player.collider_world.y != moving_logic.position.y ) {
+            moving_logic.position = player.collider_world;
+            moving_logic.position_changed = true;
+        } else
+            moving_logic.position_changed = false;
+    }
+    if (moving_logic.position_changed)
+        set_AlignPosition(moving_logic.position);
+
+    float  d_x  = moving_logic.position.x - collider_world.x;
+    float  d_y  = moving_logic.position.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));
 
     bool   dist_ok_x = (dc_x >= dist_min && dc_x <= weapon.dimension.x) && (dc_y <= weapon.dimension.y/2);
     bool   dist_ok_y = (dc_y >= dist_min && dc_y <= weapon.dimension.x) && (dc_x <= weapon.dimension.y/2);
-    // SET WALK DIRECTION
-    flip = (d_x < 0);
-    if (fabs(d_x) > 0.0f && fabs(d_x) > fabs(d_y)) {
-        direction = (d_x < 0 ? WalkDirection::LEFT : WalkDirection::RIGHT);
-    } else if (d_y > 0.0f) {
-        direction = WalkDirection::DOWN;
-    } else if (d_y < .0f) {
-        direction = WalkDirection::UP;
-    }
 
     point_in_reach = false;
     /*
@@ -841,8 +848,17 @@ Enemy::update(Character& player) {
     std::cout << collider_world.x - player.collider_world.x << " " << collider_world.y - player.collider_world.y;
     */
     if (!stats.is_attacking) {
-        set_AlignPosition(player);
         move_Path();
+
+        // SET WALK DIRECTION
+        flip = (d_x < 0);
+        if (fabs(d_x) > 0.0f && fabs(d_x) > fabs(d_y)) {
+            direction = (d_x < 0 ? WalkDirection::LEFT : WalkDirection::RIGHT);
+        } else if (d_y > 0.0f) {
+            direction = WalkDirection::DOWN;
+        } else if (d_y < .0f) {
+            direction = WalkDirection::UP;
+        }
         // std::cout << next_moves.size() << std::endl;
     }
     // std::cout << dist_ok_x << " " << dist_ok_y << " " <<  next_moves.size() << std::endl;
@@ -852,27 +868,14 @@ Enemy::update(Character& player) {
     }
 
     if (!stats.is_attacking) {
-        /*
-        if (player.moving) {
-            if (dc_x > 0.0f && dc_x > dc_y) {
-                direction = (d_x < 0 ? WalkDirection::LEFT : WalkDirection::RIGHT);
-            } else if (d_y > 0.0f) {
-                direction = WalkDirection::DOWN;
-            } else if (d_y < .0f) {
-                direction = WalkDirection::UP;
-            }
-        }
-        flip = (d_x < 0);
-        */
-
-        //next_position = {player.collider_world.x, player.collider_world.y};
-
         if (point_in_reach && is_player_alive)
             attack_Start();
     } else {
         attack_Character(player, point_in_reach);
     }
 
+
+    handle_Animation();
 }
 
 void 
diff --git a/src/character/enemy.hpp b/src/character/enemy.hpp
index 07c4973..529fb08 100644
--- a/src/character/enemy.hpp
+++ b/src/character/enemy.hpp
@@ -58,6 +58,18 @@ struct EnemyMoveThreadData {
     bool is_moving;
 };
 
+enum class EnemyMovingType {
+    NONE,
+    CHARACTER,
+    OBJECT
+};
+struct EnemyMovingLogic {
+    EnemyMovingType attack_type = EnemyMovingType::NONE;
+    void* object = nullptr;
+    SDL_FPoint position = {0,0};
+    bool position_changed = true;
+};
+
 class Enemy {
     float  speed;
     bool   flip;
@@ -70,6 +82,8 @@ class Enemy {
     static std::vector<SpawnParams> enemies_to_spawn;
 
 
+    EnemyMovingLogic moving_logic = {EnemyMovingType::NONE, nullptr};
+
     bool moving;
     float x, y;
     SDL_FPoint next_position = {0,0};
@@ -170,7 +184,7 @@ class Enemy {
     // move_ToPosition(SDL_FPoint move_to_position);
 
     void
-    set_AlignPosition(const Character& player);
+    set_AlignPosition(const SDL_FPoint& player);
 
     void
     set_NextMoves(const std::vector<bool>& map_tiles);
@@ -179,7 +193,7 @@ class Enemy {
     move_Path();
 
     void
-    update(Character& player);
+    Update(Character& player);
 
     void
     attack_Start();
diff --git a/src/game/state_playing.cpp b/src/game/state_playing.cpp
index a5a4849..94b47e0 100644
--- a/src/game/state_playing.cpp
+++ b/src/game/state_playing.cpp
@@ -218,7 +218,7 @@ void StatePlaying::Update(Game* game) {
 
     // update moves
     for (Enemy* enemy : Enemy::enemies)
-        enemy->update(*player);
+        enemy->Update(*player);
 
     // clear dead
     Enemy::remove_Dead();
diff --git a/src/map/map.hpp b/src/map/map.hpp
index 15afe13..3c1408f 100644
--- a/src/map/map.hpp
+++ b/src/map/map.hpp
@@ -78,6 +78,10 @@ namespace Map {
     }
     inline size_t
     get_IdFromTilePoint(SDL_Point point) {
+        if (point.x < 0) point.x = 0;
+        if (point.y < 0) point.y = 0;
+        if (point.x >= Map::size.x) point.x = Map::size.x-1;
+        if (point.y >= Map::size.y) point.y = Map::size.y-1;
         size_t id =  static_cast<size_t>(point.y * static_cast<int>(Map::size.x) + point.x);
         return id > static_cast<size_t>(Map::size.x*Map::size.y-1) ? SIZE_MAX : id;
     }