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.gzFix stuff i think
src/character/character.cpp | 3 +++ src/map/map.cpp | 58 +++++++++++++++++++++++++++++++++++---------- src/objects/object.cpp | 15 +++++++++--- src/objects/object.hpp | 4 ++-- 4 files changed, 62 insertions(+), 18 deletions(-) diff --git a/src/character/character.cpp b/src/character/character.cpp index ed38916..7ec1c02 100644 --- a/src/character/character.cpp +++ b/src/character/character.cpp @@ -1277,6 +1277,9 @@ Character::handle_Input(Input& input, SDL_Rect win_size) { input.click_left_released && !BuildingData::active_build.is_colliding) { Map::place_ConstructionSide(); + BuildingData::active_build.type = BuildingType::NONE; + BuildingData::active_build.data = nullptr;; + is_placing_build = false; return; } diff --git a/src/map/map.cpp b/src/map/map.cpp index ba189ee..f22ddd6 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -31,8 +31,8 @@ namespace Map { std::vector<bool> tile_collission_map; int frame_water = 0; - float frame_water_time = 0.0f; - float frame_water_time_max = 0.6f; + float global_frame_time = 0.0f; + float global_frame_time_max = 0.6f; int frame_meadow = 0; float frame_meadow_time = 0.0f; @@ -631,6 +631,23 @@ namespace Map { y > win.h); } + inline int + waveFrame( float t, int x_i, int y_i, int frame_count, float speed = 1.8f) { + float wave = + sinf(t * speed + x_i * 0.4f + y_i * 0.6f) + + sinf(t * speed * 0.66f - x_i * 0.7f + y_i * 0.3f) * 0.5f; + + wave *= (1.0f / 1.5f); + + float scaled = (wave + 1.0f) * (frame_count * 0.5f); + int frame = (int)scaled; + + if (frame < 0) frame = 0; + if (frame >= frame_count) frame = frame_count - 1; + + return frame; + } + void draw_MapTiles(Game* game) { // std::cout << "FL " << Map::offset.x << " " << Map::offset.y << " " << Default::game_scale << std::endl; @@ -646,21 +663,23 @@ namespace Map { int size = Tile::get_Size(); - frame_water_time += Default::delta_time; - if (frame_water_time >= frame_water_time_max) { + global_frame_time += Default::delta_time; + /* + if (global_frame_time >= global_frame_time_max) { frame_water = frame_water > 0 ? frame_water-1 : 3; - frame_water_time = 0.0f; + global_frame_time = 0.0f; if (frame_water == 0) - frame_water_time_max = 0.3f + ((float)rand() / (float)RAND_MAX) * 1.2f; - //std::cout << "frame_water_time_max: " << frame_water_time_max << std::endl; + global_frame_time_max = 0.3f + ((float)rand() / (float)RAND_MAX) * 1.2f; + //std::cout << "global_frame_time_max: " << global_frame_time_max << std::endl; } + */ frame_meadow_time += Default::delta_time; if (frame_meadow_time >= frame_meadow_time_max) { frame_meadow++; frame_meadow %= 3; frame_meadow_time = 0.0f; if (frame_meadow == 0) - frame_meadow_time_max = 0.3f + ((float)rand() / (float)RAND_MAX) * 1.2f; + frame_meadow_time_max = 1.4f + ((float)rand() / (float)RAND_MAX) * 2.3f; //std::cout << "frame_meadow_time_max: " << frame_meadow_time_max << std::endl; } for (size_t i = 0; i < Map::tile_map.size(); ++i) { @@ -692,11 +711,17 @@ namespace Map { }; SDL_Point set_position = tile.set_position; + + if (tile.set_category == TileCategory::TILE_WATER && set_position.y < 5) { - set_position.x += (frame_water + y_i+x_i) % 4*6; - } else - if (tile.set_category == TileCategory::TILE_GROUND && set_position.y == 10) { - set_position.x += (frame_meadow + y_i+x_i) % 3; + float local_speed = 1.8f * (0.8f + 0.4f * sinf(x_i*0.3f + y_i*0.5f)); + int frame = waveFrame(global_frame_time, x_i, y_i, 4, local_speed); + set_position.x += frame * 6; + } + else if (tile.set_category == TileCategory::TILE_GROUND && set_position.y == 10) { + float local_speed = 1.5f * (0.85f + 0.3f * sinf(x_i*0.25f + y_i*0.35f)); + int frame = waveFrame(global_frame_time, x_i, y_i, 3, local_speed); + set_position.x += frame; } SDL_FRect src { static_cast<float>(set_position.x) * Tile::tile_size, @@ -807,7 +832,6 @@ namespace Map { // Render Character if no object is hiding it player_behind = false; - // DRAW BIRD for (TileObject& o : tilemap.objects) { Object* object = Object::get_ObjectById(o.object_id); // o = relative position, @@ -825,6 +849,7 @@ namespace Map { // START DEBUG RECT #endif //if (o.position.x != 0 || o.position.y != o.position.h-1) continue; + // real object 0,0 start in map pixel float m_xf = xf - o.position.x*static_cast<float>(Tile::tile_size); float m_yf = yf - o.position.y*static_cast<float>(Tile::tile_size); @@ -874,11 +899,18 @@ namespace Map { } j++; } + SDL_FRect char_r = {character_collider_x, + character_collider_y, + player.collider.w, + player.collider.h}; + SDL_FRect object_rect = {m_xf, m_yf, static_cast<float>(object->dimension.x), static_cast<float>(object->dimension.y)}; + bool is_char_obj_intersec = SDL_HasRectIntersectionFloat(&object_rect, &char_r); if ( character_collider_x >= m_xf && character_collider_y >= m_yf && character_collider_x + player.collider.w <= m_point.x && character_collider_y <= m_point.y && object->z_index > 0) { + std::cout << "ID " << object->z_index << std::endl; player_behind = true; if ( object->is_InFrontOf({m_xf, m_yf}, diff --git a/src/objects/object.cpp b/src/objects/object.cpp index 7ccf3d8..3fdad44 100644 --- a/src/objects/object.cpp +++ b/src/objects/object.cpp @@ -63,13 +63,13 @@ Object::get_SetPosition() { } bool -Object::is_InFrontOf(SDL_FPoint position, SDL_FRect rect) { +Object::is_InFrontOf(SDL_FPoint world_position, SDL_FRect rect) { const int scale = Tile::tile_size / Tile::tile_collider_size; const int obj_w = dimension.x * scale; const int obj_h = dimension.y * scale; - const int obj_x = position.x / scale; - const int obj_y = position.y / scale; + const int obj_x = world_position.x / scale; + const int obj_y = world_position.y / scale; const int rect_x = rect.x / Tile::tile_collider_size; const int rect_y = rect.y / Tile::tile_collider_size; @@ -97,10 +97,12 @@ Object::is_InFrontOf(SDL_FPoint position, SDL_FRect rect) { bool anywhere_above = false; bool anywhere_below = false; std::vector<bool> collission_map = get_CollisionMap(); + bool has_collision = false; for (int y = 0; y < obj_h; ++y) { int base = y * row_width; for (int x = 0; x < obj_w; ++x) { if (collission_map[static_cast<size_t>(base + x)]) { + has_collision = true; if (y < rect_row) { //std::cout << "ABOVE \n"; anywhere_above = true; @@ -120,6 +122,13 @@ Object::is_InFrontOf(SDL_FPoint position, SDL_FRect rect) { } } } + if ( !has_collision ) { + SDL_FRect temp_rect = { world_position.x, world_position.y, static_cast<float>(dimension.x*Tile::tile_size), static_cast<float>(dimension.y*Tile::tile_size) }; + std::cout << "COLL " << SDL_HasRectIntersectionFloat(&temp_rect, &rect) << " " << world_position.x << " " << world_position.y << " " << rect.x << " " << rect.y <<std::endl; + std::cout << "WIDH " << dimension.x *Tile::tile_size<< " " << dimension.y *Tile::tile_size<< " RE " << rect.w << " " << rect.h << std::endl; + return SDL_HasRectIntersectionFloat(&temp_rect, &rect); + } + if ((collission_below || anywhere_below) && !anywhere_above) return true; if (!anywhere_above && !collission_below) diff --git a/src/objects/object.hpp b/src/objects/object.hpp index 708a994..0d7b4c9 100644 --- a/src/objects/object.hpp +++ b/src/objects/object.hpp @@ -61,8 +61,8 @@ class Object { size_t tileset_object_id; SDL_Point world_tile_position; - SDL_Point dimension; // not needed - int z_index; // not needed + SDL_Point dimension; + int z_index; Object( ObjectType ot,