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.gzAdd ugly boundary check for map size,.......(fix path finding problem)
src/character/character.cpp | 8 ++++ src/character/character.hpp | 3 +- src/character/enemy.cpp | 97 ++++++++++++++++++++++++++++++++++++++------- src/character/enemy.hpp | 1 + src/game/state_playing.cpp | 8 ++-- src/map/map.cpp | 4 +- src/map/map.hpp | 2 + 7 files changed, 103 insertions(+), 20 deletions(-) diff --git a/src/character/character.cpp b/src/character/character.cpp index d07a2d3..aa182b9 100644 --- a/src/character/character.cpp +++ b/src/character/character.cpp @@ -1630,6 +1630,13 @@ Character::handle_Input(Input& input, SDL_Rect win_size) { if (!Item::craft_Item(inventory.get(), Item::crafting_item, messages.get())) { set_Status(CharacterStatus::IDLE); Item::craft_Stop(); + } else { + if (Item::crafting_item.type == ItemType::SWORD_WOOD) { + if (!is_attackable) { + std::cout << "IM ATTACKABLE NOW!\n"; + is_attackable = true; + } + } } return; } @@ -2113,6 +2120,7 @@ void Character::Update() { //bird->set_Position({x,y}); bird->Update(Default::delta_time, collider_world); + // std::cout <<" COLOLOL " << collider_world.x << " " << collider_world.y << std::endl; reduce_StatsBonus(); if (status == CharacterStatus::HIT && active_anim->current_frame == 3) { diff --git a/src/character/character.hpp b/src/character/character.hpp index 39a3bfc..040f37a 100644 --- a/src/character/character.hpp +++ b/src/character/character.hpp @@ -156,7 +156,6 @@ class Character { float arm_reach_radius = 0; bool is_placing_build = false; - std::vector<BuildingType> unclocked__buildings; @@ -172,6 +171,8 @@ class Character { CharacterSprintStartLogic sprintstart_logic; Bird* bird = nullptr; + bool is_attackable = false; + std::vector<CharacterStatsBonus> stats_bonus; PixelShuffler* death_shuffler; diff --git a/src/character/enemy.cpp b/src/character/enemy.cpp index cf60789..7acdb85 100644 --- a/src/character/enemy.cpp +++ b/src/character/enemy.cpp @@ -372,7 +372,7 @@ void Enemy::run_GetMovesThreads() // ---- First pass: mark grid exactly like old version ---- for (Enemy* e : Enemy::enemies) { - if (e->moving_logic.move_type != EnemyMovingType::WALK || + if (e->status != EnemyStatus::RUN || !checks.count(e->next_moves.size())) continue; @@ -403,9 +403,14 @@ void Enemy::run_GetMovesThreads() std::lock_guard<std::mutex> lock(queue_mutex); for (Enemy* e : Enemy::enemies) { + if (e->status != EnemyStatus::RUN) + continue; if (e->point_in_reach) continue; + if (e->next_position.x == 0 || e->next_position.y == 0 ) + continue; + job_queue.push_back({ e, grid_ptr }); ++jobs_remaining; } @@ -872,6 +877,7 @@ Enemy::set_NextMoves(const std::vector<bool>& grid) { // std::cout << "TG: " << target_pos.x << " " << target_pos.y << std::endl; // std::cout << "DS: " << dist_max-dist_min << std::endl; next_moves = find_Path({collider_world.x, collider_world.y, collider.w, collider.h}, next_position, dist_max-dist_min, 0, grid); + std::cout << "NM " << next_moves.size() << " " << collider_world.x << " " << collider_world.y << " " << next_position.x << " " << next_position.y << std::endl; } @@ -937,7 +943,9 @@ Enemy::set_MoveLogic(EnemyMovingType& type, Character& player) { EnemyStatus sta = EnemyStatus::IDLE; point_in_reach = true; moving_logic.avoid_blockage = false; + stuck_moves = 0; + std::cout << "FUCK " << (int)type << std::endl; switch (type) { case EnemyMovingType::NONE: break; @@ -945,12 +953,13 @@ Enemy::set_MoveLogic(EnemyMovingType& type, Character& player) { moving_logic.move_type = EnemyMovingType::NONE; break; case EnemyMovingType::IDLE: - moving_logic.duration = (300 + (rand() % 700))/100.0f; + moving_logic.duration = (180 + (rand() % 200))/100.0f; sta = EnemyStatus::IDLE; break; case EnemyMovingType::WALK: { SDL_FRect test_pos = collider; int chance_move_far = rand() % 10; + if (chance_move_far < 3) { moving_logic.min_distance = 100; moving_logic.max_distance = 140; @@ -958,12 +967,56 @@ Enemy::set_MoveLogic(EnemyMovingType& type, Character& player) { moving_logic.min_distance = 50; moving_logic.max_distance = 101; } - test_pos.x = moving_logic.min_distance + (rand() % moving_logic.max_distance); - test_pos.y = moving_logic.min_distance + (rand() % moving_logic.max_distance); - moving_logic.moving_distance = {0,0}; - moving_logic.position = - Map::get_ClosestPointWithoutCollision(test_pos); - set_AlignPosition(moving_logic.position); + + // Retry generating a test position until it's valid + SDL_FPoint test_pos_c = {0,0}; + // START CLAMP NEW POSITION TO BOUNDARIES WHICH SUUUUUUCKS!!! + int i = 0; + + const int map_max_x = Map::boundaries.x * Tile::tile_size; + const int map_max_y = Map::boundaries.y * Tile::tile_size; + + do { + if (i++ > 3) { + moving_logic.move_type = EnemyMovingType::NONE; + return; + } + + int dist_x = moving_logic.min_distance + + rand() % (moving_logic.max_distance - moving_logic.min_distance + 1); + + int dist_y = moving_logic.min_distance + + rand() % (moving_logic.max_distance - moving_logic.min_distance + 1); + + if (rand() % 2) dist_x = -dist_x; + if (rand() % 2) dist_y = -dist_y; + + int new_x = static_cast<int>(collider_world.x) + dist_x; + int new_y = static_cast<int>(collider_world.y) + dist_y; + + if (new_x < 0 || new_y < 0 || + new_x >= map_max_x || new_y >= map_max_y) + continue; + + test_pos.x = new_x; + test_pos.y = new_y; + + test_pos_c = Map::get_ClosestPointWithoutCollision(test_pos); + + if (player.is_attackable && test_pos_c.x <= 530 && test_pos_c.y <= 334) + continue; + + break; + + } while (true); + // END CLAMP NEW POSITION TO BOUNDARIES WHICH SUUUUUUCkS!!! + + moving_logic.moving_distance = {0, 0}; + // moving_logic.position = Map::get_ClosestPointWithoutCollision(test_pos); + next_position = test_pos_c; + //set_AlignPosition(moving_logic.position); + std::cout << " MEEE1 " << player.is_attackable << " " << collider_world.x << " " << collider_world.y << std::endl; + std::cout << " MEEE2 " << player.is_attackable << " " << next_position.x << " " << next_position.y << std::endl; sta = EnemyStatus::RUN; point_in_reach = false; break; @@ -999,13 +1052,15 @@ Enemy::Update(Character& player) { point_in_reach = false; - is_character_in_range = character_InRange(player.collider_world); - if (!is_character_in_range && moving_logic.move_type == EnemyMovingType::CHARACTER) { - next_moves.clear(); - move_focus = EnemyMovingType::NONE; - moving_logic.move_type = EnemyMovingType::NONE; - attack_Stop(); - } + if (player.is_attackable) { + is_character_in_range = character_InRange(player.collider_world); + if (!is_character_in_range && moving_logic.move_type == EnemyMovingType::CHARACTER) { + next_moves.clear(); + move_focus = EnemyMovingType::NONE; + moving_logic.move_type = EnemyMovingType::NONE; + attack_Stop(); + } +} // IS DEAD if (stats.health <= 0) { @@ -1036,6 +1091,7 @@ Enemy::Update(Character& player) { if ( moving_logic.moving_distance.x >= moving_logic.max_distance || moving_logic.moving_distance.y >= moving_logic.max_distance ) { moving_logic.move_type = EnemyMovingType::NONE; + next_moves.clear(); handle_Animation(); return; } @@ -1080,7 +1136,18 @@ Enemy::Update(Character& player) { std::cout << "Walk DONE !!!!\n"; return; } + } else if (next_moves.size() < 2) { + ++stuck_moves; + if (stuck_moves > 20) { + std::cout << "IM STUCK!!\n"; + set_Status(EnemyStatus::IDLE); + moving_logic.move_type = EnemyMovingType::IDLE; + next_position = collider_world; + next_moves.clear(); + return; + } } + if (move_focus == EnemyMovingType::CHARACTER) { moving_logic.duration -= Default::delta_time; // std::cout << moving_logic.duration << std::endl; diff --git a/src/character/enemy.hpp b/src/character/enemy.hpp index e21364a..678df3b 100644 --- a/src/character/enemy.hpp +++ b/src/character/enemy.hpp @@ -125,6 +125,7 @@ class Enemy { float dist_max = 32; bool is_character_in_range = false; + int stuck_moves = 0; std::vector<SDL_FPoint> next_moves; SDL_Point last_player_pos{}; diff --git a/src/game/state_playing.cpp b/src/game/state_playing.cpp index adc6933..b5a4646 100644 --- a/src/game/state_playing.cpp +++ b/src/game/state_playing.cpp @@ -83,9 +83,11 @@ bool StatePlaying::Init(Game* game) { // Enemy Enemy::enemies.clear(); Enemy::enemies_to_spawn.clear(); - Enemy::init_SpawnEnemy(0, WeaponType::SWORD, WeaponMaterial::WOOD, {850, 520}, {16,16,2,false,false,0,1.2}); - Enemy::init_SpawnEnemy(0, WeaponType::SWORD, WeaponMaterial::WOOD, {450, 520}, {16,16,2,false,false,0,1.2}); - Enemy::init_SpawnEnemy(0, WeaponType::SWORD, WeaponMaterial::WOOD, {150, 520}, {16,16,2,false,false,0,1.2}); + /* + */ + Enemy::init_SpawnEnemy(0, WeaponType::SWORD, WeaponMaterial::WOOD, {850, 480}, {16,16,2,false,false,0,1.2}); + Enemy::init_SpawnEnemy(0, WeaponType::SWORD, WeaponMaterial::WOOD, {450, 480}, {16,16,2,false,false,0,1.2}); + Enemy::init_SpawnEnemy(0, WeaponType::SWORD, WeaponMaterial::WOOD, {150, 480}, {16,16,2,false,false,0,1.2}); /* Enemy::init_SpawnEnemy(1, WeaponType::SWORD, WeaponMaterial::WOOD, {140, 100}, {6,6,1,false,false,0,1.2}); Enemy::init_SpawnEnemy(1, WeaponType::SWORD, WeaponMaterial::WOOD, {100, 10}, {6,6,1,false,false,0,1.2}); diff --git a/src/map/map.cpp b/src/map/map.cpp index 071dce1..af4dc5e 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -25,6 +25,8 @@ namespace Map { SDL_Point offset = {0, 0}; SDL_Point size = {160, 100}; // map size in tiles... + SDL_Point boundaries = {63, 39}; // JEee fucked up the size, + // to much work to resize (i have an outer cage which fucks the path finding) std::vector<TileMap> tile_map; @@ -1170,7 +1172,7 @@ namespace Map { } std::cout << "No point found!\n"; - return { rect.x, rect.y }; + return { 0,0 }; } }; diff --git a/src/map/map.hpp b/src/map/map.hpp index 53062b3..d186b86 100644 --- a/src/map/map.hpp +++ b/src/map/map.hpp @@ -32,6 +32,8 @@ namespace Map { extern SDL_Point offset_to_set; extern SDL_Point offset; extern SDL_Point size; + extern SDL_Point boundaries; // JEee fucked up the size, + // to much work to resize (i have an outer cage which fucks the path finding) extern int frame_water; extern float frame_water_time;