mapcreator git · master
SDL3 2.5D game and engine using assets, with map editor
C++ 99.3%git clone https://git.christianimmanuel.de/sdl-graphics/mapcreator.gitwget https://git.christianimmanuel.de/sdl-graphics/mapcreator/archive/mapcreator.tar.gzUpdate enemy blocked better sorting a little bit but get stuck sometimes..
src/character/enemy.cpp | 50 ++++++++++++++++++++++++++++++++++++------------- src/character/enemy.hpp | 6 ++++-- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/character/enemy.cpp b/src/character/enemy.cpp index 050ab78..ab3bc0d 100644 --- a/src/character/enemy.cpp +++ b/src/character/enemy.cpp @@ -419,8 +419,6 @@ void Enemy::run_GetMovesThreads() void Enemy::Decide(Character& player) { - std::cout << "DEC " << (int) status << std::endl; - //float d_x = player.collider_world.x - collider_world.x; //float d_y = player.collider_world.y - collider_world.y; //size_t dc_x = static_cast<size_t>(abs(d_x)); @@ -429,7 +427,6 @@ void Enemy::Decide(Character& player) { r = EnemyMovingType::CHARACTER; set_MoveLogic(r, player); - std::cout << "DEB " << (int) status << std::endl; } inline bool same_corner(const SDL_FPoint& a, const SDL_FPoint& b, float epsilon = 1.0f) { @@ -738,21 +735,45 @@ void Enemy::move_Path() { SDL_FRect test = collider; - test.x = x + collider.x + dx; - test.y = y + collider.y; bool is_actually_colliding = is_Colliding({collider.x + x, collider.y + y, collider.w, collider.h}); - if (!is_Colliding(test) || is_actually_colliding) { - x += dx; - moving_logic.moving_distance.x += std::abs(dx); - } - test.x = x + collider.x; + test.x = x + collider.x + dx; test.y = y + collider.y + dy; if (!is_Colliding(test) || is_actually_colliding) { + x += dx; + moving_logic.moving_distance.x += std::abs(dx); y += dy; moving_logic.moving_distance.y += std::abs(dy); + moving_logic.path_blocked = 0.0; + } else { + float dx = next_position.x - collider_world.x; + float dy = next_position.y - collider_world.y; + float len = std::sqrt(dx * dx + dy * dy); + dx /= len; + dy /= len; + bool left = (std::rand() % 2) == 0; + float px = left ? -dy : dy; + float py = left ? dx : -dx; + float distance = 40.0f + static_cast<float>(std::rand()) / + (static_cast<float>(RAND_MAX) / 40.0f); + SDL_FPoint result = { + collider_world.x + px * distance, + collider_world.y + py * distance + }; + std::cout << "GETING OUTTTTT\n"; + moving_logic.position = Map::get_ClosestPointWithoutCollision({result.x, result.y, collider.w, collider.h}); + next_position = moving_logic.position; + moving_logic.moving_distance.x = 0.0f; + moving_logic.moving_distance.y = 0.0f; + point_in_reach = false; + moving_logic.path_blocked = 0.0; + set_Status(EnemyStatus::RUN); + moving_logic.avoid_blockage = true; + moving_logic.move_type = EnemyMovingType::WALK; + handle_Animation(); + return; } @@ -892,6 +913,7 @@ Enemy::set_MoveLogic(EnemyMovingType& type, Character& player) { EnemyStatus sta = EnemyStatus::IDLE; point_in_reach = true; + moving_logic.avoid_blockage = false; switch (type) { case EnemyMovingType::NONE: @@ -968,13 +990,14 @@ Enemy::Update(Character& player) { // DECIDE // REDECIDE if ( moving_logic.move_type == EnemyMovingType::NONE || ( moving_logic.move_type == EnemyMovingType::IDLE && - moving_logic.duration <= 0)) { + moving_logic.duration <= 0 && !moving_logic.avoid_blockage)) { Decide(player); handle_Animation(); //std::cout << "MVT1 " << (int)moving_logic.move_type << std::endl; return; } else if (moving_logic.move_type == EnemyMovingType::IDLE) { + std::cout << "moving_logic.duration" << moving_logic.duration << std::endl; moving_logic.duration -= Default::delta_time; handle_Animation(); return; @@ -1014,6 +1037,7 @@ Enemy::Update(Character& player) { } if (dist_sq >= min_sq && dist_sq <= max_sq) { point_in_reach = true; + moving_logic.avoid_blockage = false; next_moves.clear(); } if (next_moves.empty()) { @@ -1032,8 +1056,8 @@ Enemy::Update(Character& player) { if (move_focus == EnemyMovingType::CHARACTER) { moving_logic.duration -= Default::delta_time; // std::cout << moving_logic.duration << std::endl; - std::cout << "NEXT MOVES: " << next_moves.size() << std::endl; - if ((point_moved && moving_logic.duration <= 0) || moving_logic.move_type != EnemyMovingType::CHARACTER) { + //std::cout << "NEXT MOVES: " << next_moves.size() << std::endl; + if (!moving_logic.avoid_blockage && ((point_moved && moving_logic.duration <= 0) || moving_logic.move_type != EnemyMovingType::CHARACTER)) { set_MoveLogic(moving_logic.move_type, player); return; } diff --git a/src/character/enemy.hpp b/src/character/enemy.hpp index 7cc893c..21c763a 100644 --- a/src/character/enemy.hpp +++ b/src/character/enemy.hpp @@ -74,8 +74,10 @@ struct EnemyMovingLogic { SDL_FPoint position = {0,0}; bool position_changed = true; float duration = 0.0f; - int max_distance = 0; - int min_distance = 0; + int max_distance = 120; + int min_distance = 80; + double path_blocked = 0; // time when blocked? + bool avoid_blockage = false; SDL_FPoint moving_distance = {0,0}; };