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 inventory text
src/building/building_data.hpp | 2 +- src/character/character.cpp | 8 +- src/game/game.cpp | 4 - src/inventory/inventory.cpp | 256 +++++++++++++++++++++++++++++------------ src/inventory/inventory.hpp | 3 +- src/inventory/item.hpp | 2 +- src/map/map.cpp | 4 +- src/objects/object.hpp | 2 +- src/resource/resource.hpp | 2 +- src/vegetables/vegetables.hpp | 4 +- 10 files changed, 200 insertions(+), 87 deletions(-) diff --git a/src/building/building_data.hpp b/src/building/building_data.hpp index 5bbddac..e2ba577 100644 --- a/src/building/building_data.hpp +++ b/src/building/building_data.hpp @@ -2,7 +2,7 @@ #include "objects/object.hpp" -enum class ItemType; +enum class ItemType : uint32_t ; enum class BuildingType { NONE, diff --git a/src/character/character.cpp b/src/character/character.cpp index ac2fcf7..2013441 100644 --- a/src/character/character.cpp +++ b/src/character/character.cpp @@ -270,7 +270,7 @@ Character::Draw(SDL_Renderer* renderer) { SDL_Texture* txt = active_anim->texture; - if (death_shuffler) + if (status == CharacterStatus::DEAD && death_shuffler) txt = SDL_CreateTextureFromSurface(renderer, death_shuffler->getSurface()); SDL_RenderTextureRotated(renderer, txt, &src, &dst, @@ -484,10 +484,12 @@ Character::collect_Start(TileObject* map_object_p, SDL_Point tile_point) { return; } collecting_logic.duration_max = 3; + // COLLECT RESOURCE if (object->object_type != ObjectType::VEGETABLE) { collecting_logic.resource = Tile::get_ResourceFromTileSet(object->tileset_id, object->tileset_object_id); collecting_logic.duration_max = collecting_logic.resource.collecting_duration; } else { + // COLLECT VEGETABLE collecting_logic.duration_max = static_cast<Vegetable*>(object)->collecting_duration_max; } collecting_logic.is_collecting = true; @@ -1364,6 +1366,8 @@ Character::handle_Input(Input& input, SDL_Rect win_size) { if (Inventory::cursor_OverBuildMenu(input.mouse_state_raw)) { if (input.click_left_released) { Inventory::selected_build_item = Inventory::hit_BuildMenu(input.mouse_state_raw, win_size, researched_items); + std::cout << "SLE IN " << (int)Inventory::selected_build_item << std::endl; + inventory->selected_inventory.is_valid = false; Item::craft_Stop(); set_Status(CharacterStatus::IDLE); } @@ -1384,6 +1388,8 @@ Character::handle_Input(Input& input, SDL_Rect win_size) { } if (Inventory::cursor_OverInventoryGrid(input.mouse_state_raw)){ if (input.click_left_released) { inventory->selected_inventory = inventory->hit_InventoryGrid(input.mouse_state_raw); + Inventory::selected_item = &inventory->selected_inventory; + Inventory::selected_build_item = ItemType::NONE; } return; } diff --git a/src/game/game.cpp b/src/game/game.cpp index e9443ea..1c78479 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -118,7 +118,6 @@ void Game::set_State(GameState_e new_state, NewGameState_e game_state_e) { // STATE_PLAYING // START CURSOR - bool ret = false; switch (new_state) { case GameState_e::STATE_MODIFY: cursor->set_State(CursorState::NORMAL); @@ -130,9 +129,6 @@ void Game::set_State(GameState_e new_state, NewGameState_e game_state_e) { cursor->set_State(CursorState::HIDDEN); break; } - if (!ret) { - std::cout << "ERROR: " << SDL_GetError() << std::endl; - } // END CURSOR state_changed = true; diff --git a/src/inventory/inventory.cpp b/src/inventory/inventory.cpp index 1768ed0..26614da 100644 --- a/src/inventory/inventory.cpp +++ b/src/inventory/inventory.cpp @@ -35,6 +35,7 @@ Equipement Inventory::hotbar[HOTBAR_SLOTS]; InventoryItem* Inventory::hotbar_hands = nullptr; ItemType Inventory::selected_build_item = ItemType::NONE; +InventorySelection* Inventory::selected_item = nullptr; @@ -51,6 +52,7 @@ Inventory::Inventory() { hotbar_hands = new InventoryItem(0, ItemType::HAND); next_item_id = 1; + Inventory::selected_item = nullptr; for (int i = 1; i < HOTBAR_SLOTS; i++) { Inventory::hotbar[i].item_id = 0; @@ -293,14 +295,14 @@ Inventory::cursor_OverCraftButton(const SDL_FPoint& mouse) { void Inventory::draw_InfoPanel(SDL_Renderer* renderer, TTF_Font* font, - ItemType t, bool is_hovering_craftbutton) { - + ItemType /*unused*/, + bool is_hovering_craftbutton) +{ const float panel_w = INFO_PANEL_WIDTH; const float panel_h = menu_inner.h; const float panel_x = menu_inner.x + menu_inner.w + 10.f; const float panel_y = menu_inner.y; - - const float border = 4.f; + const float border = 4.f; SDL_SetRenderDrawColor(renderer, 60, 60, 70, 255); SDL_FRect panel_border{ @@ -315,21 +317,25 @@ Inventory::draw_InfoPanel(SDL_Renderer* renderer, SDL_FRect panel_bg{ panel_x, panel_y, panel_w, panel_h }; SDL_RenderFillRect(renderer, &panel_bg); - if (t == ItemType::NONE) return; - - const float line_h = 20.f; - const float spacing = 4.f; - + const float line_h = 20.f; + const float spacing = 4.f; float y = panel_y + 6.f; - auto draw_line = [&](const std::string& s) { + auto draw_line = [&](const std::string& text) + { SDL_Color white{255,255,255,255}; - SDL_Surface* surf = TTF_RenderText_Solid(font, s.c_str(), s.length(), white); + SDL_Surface* surf = TTF_RenderText_Solid(font, text.c_str(), text.length(), white); if (!surf) return; SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf); - if (tex) { - SDL_FRect dst{ panel_x + 6.f, y, (float)surf->w, (float)surf->h }; + if (tex) + { + SDL_FRect dst{ + panel_x + 6.f, + y, + (float)surf->w, + (float)surf->h + }; SDL_RenderTexture(renderer, tex, nullptr, &dst); SDL_DestroyTexture(tex); } @@ -338,62 +344,84 @@ Inventory::draw_InfoPanel(SDL_Renderer* renderer, SDL_DestroySurface(surf); }; - const ItemStats& s = Item::get(t); + auto resource_name = [](ResourceType type) -> const std::string& + { + size_t idx = static_cast<size_t>(type); + if (idx < ResourceData::names.size()) + return ResourceData::names[idx]; + return ResourceData::names.back(); + }; + + const ItemCrafting& ci = Item::crafting_item; - // Item name - draw_line("ITEM:"); - switch (t) { - case ItemType::SWORD_WOOD: draw_line("Wood Sword"); break; - case ItemType::BUILDING_TOOL: draw_line("Building tool"); break; - case ItemType::BONE_BREASTPLATE: draw_line("Breastplate"); break; - default: break; - } - y += spacing; - - // Cost - draw_line("COST:"); - for (auto& c : s.cost) { - std::string name; - switch (c.type) { - case ResourceType::WOOD: name = "Wood"; break; - case ResourceType::STONE: name = "Stone"; break; - case ResourceType::CLAY: name = "Clay"; break; - case ResourceType::BONE: name = "Bone"; break; - default: name = "?"; break; + // ================================================================ + // BUILD ITEM SELECTED + // ================================================================ + if (selected_build_item != ItemType::NONE) + { + const ItemStats& s = Item::get(selected_build_item); + + const bool craftable = + (s.crafting_duration > 0.f) && + (s.cost.size() != 0); + + draw_line("ITEM:"); + draw_line(s.title); + y += spacing; + + if (craftable) + { + draw_line("COST:"); + for (const auto& c : s.cost) + { + const std::string& name = resource_name(c.type); + draw_line(name + ": " + std::to_string(c.amount)); + } + y += spacing; } - draw_line(name + ": " + std::to_string(c.amount)); - } - y += spacing; - // Stats - draw_line("STATS:"); - if (s.base_damage > 0) draw_line("Damage: " + std::to_string(s.base_damage)); - if (s.base_armor > 0) draw_line("Armor: " + std::to_string(s.base_armor)); - draw_line("Health: " + std::to_string(s.base_health)); - draw_line("Max Lvl: " + std::to_string(s.max_level)); + draw_line("STATS:"); - // Craft button - const float btn_h = 24.f; - const float btn_w = panel_w - 12.f; - const float btn_x = panel_x + 6.f; - const float btn_y = panel_y + panel_h - btn_h - 6.f; + if (s.base_damage > 0) + draw_line("Damage: " + std::to_string(s.base_damage)); - SDL_SetRenderDrawColor(renderer, 40, 140, 40, 255); - SDL_FRect craft_btn{ btn_x-1, btn_y-1, btn_w+2, btn_h+2 }; - SDL_RenderFillRect(renderer, &craft_btn); + if (s.base_armor > 0) + draw_line("Armor: " + std::to_string(s.base_armor)); - if (is_hovering_craftbutton) - SDL_SetRenderDrawColor(renderer, MENU_HOVER_BORDER_C.r, MENU_HOVER_BORDER_C.g, MENU_HOVER_BORDER_C.b, MENU_HOVER_BORDER_C.a); - else - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); - SDL_RenderRect(renderer, &craft_btn); + draw_line("Health: " + std::to_string(s.base_health)); - const ItemCrafting& ci = Item::crafting_item; + if (s.base_speed > 0.f) + draw_line("Speed: " + std::to_string(s.base_speed)); + + if (s.max_level > 0) + draw_line("Max Lvl: " + std::to_string(s.max_level)); + + if (!craftable) + return; + + const float btn_h = 24.f; + const float btn_w = panel_w - 12.f; + const float btn_x = panel_x + 6.f; + const float btn_y = panel_y + panel_h - btn_h - 6.f; - if (ci.is_crafting) { - if (ci.stats.crafting_duration > 0.0f) { + SDL_SetRenderDrawColor(renderer, 40, 140, 40, 255); + SDL_FRect craft_btn{ btn_x - 1.f, btn_y - 1.f, btn_w + 2.f, btn_h + 2.f }; + SDL_RenderFillRect(renderer, &craft_btn); - float p = ci.duration / ci.stats.crafting_duration; + if (is_hovering_craftbutton) + SDL_SetRenderDrawColor(renderer, + MENU_HOVER_BORDER_C.r, + MENU_HOVER_BORDER_C.g, + MENU_HOVER_BORDER_C.b, + MENU_HOVER_BORDER_C.a); + else + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + + SDL_RenderRect(renderer, &craft_btn); + + if (ci.is_crafting && ci.stats.type == s.type) + { + float p = ci.duration / s.crafting_duration; if (p < 0.f) p = 0.f; if (p > 1.f) p = 1.f; @@ -401,22 +429,104 @@ Inventory::draw_InfoPanel(SDL_Renderer* renderer, SDL_FRect fill{ btn_x, btn_y, btn_w * p, btn_h }; SDL_RenderFillRect(renderer, &fill); } + + SDL_Surface* surf = + TTF_RenderText_Solid(Ui::font_menu_bold, "Craft", 5, SDL_Color{255,255,255,255}); + + if (surf) + { + SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf); + if (tex) + { + SDL_FRect dst{ + btn_x + btn_w * 0.5f - surf->w * 0.5f, + btn_y + btn_h * 0.5f - surf->h * 0.5f, + (float)surf->w, + (float)surf->h + }; + SDL_RenderTexture(renderer, tex, nullptr, &dst); + SDL_DestroyTexture(tex); + } + SDL_DestroySurface(surf); + } + + return; } - SDL_Surface* surf = TTF_RenderText_Solid(Ui::font_menu_bold, "Craft", 5, SDL_Color{255,255,255,255}); - if (surf) { - SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf); - if (tex) { - SDL_FRect dst{ - btn_x + btn_w*0.5f - surf->w*0.5f, - btn_y + btn_h*0.5f - surf->h*0.5f, - (float)surf->w, - (float)surf->h - }; - SDL_RenderTexture(renderer, tex, nullptr, &dst); - SDL_DestroyTexture(tex); + // ================================================================ + // IF CRAFTING BUT NOTHING SELECTED -> SHOW CRAFTING ITEM + // ================================================================ + if (ci.is_crafting) + { + const ItemStats& s = ci.stats; + + draw_line("CRAFTING:"); + draw_line(s.title); + y += spacing; + + const float btn_h = 24.f; + const float btn_w = panel_w - 12.f; + const float btn_x = panel_x + 6.f; + const float btn_y = panel_y + panel_h - btn_h - 6.f; + + float p = 0.f; + if (s.crafting_duration > 0.f) + p = ci.duration / s.crafting_duration; + + if (p < 0.f) p = 0.f; + if (p > 1.f) p = 1.f; + + SDL_SetRenderDrawColor(renderer, 80, 200, 80, 255); + SDL_FRect fill{ btn_x, btn_y, btn_w * p, btn_h }; + SDL_RenderFillRect(renderer, &fill); + + return; + } + + // ================================================================ + // INVENTORY SELECTION + // ================================================================ + if (selected_item && selected_item->is_valid) + { + draw_line("ITEM:"); + + if (selected_item->resource != ResourceType::NONE) + { + draw_line(resource_name(selected_item->resource)); + return; + } + + if (selected_item->vegetable != VegetableData::Type::NONE) + { + const auto& def = + VegetableData::crop_defs[static_cast<uint32_t>(selected_item->vegetable)]; + draw_line(def.name); + return; + } + + if (selected_item->item) + { + auto* inv_item = + static_cast<InventoryItem*>(selected_item->item); + const ItemStats& s = Item::get(inv_item->type); + draw_line(s.title); + draw_line("STATS:"); + + if (s.base_damage > 0) + draw_line("Damage: " + std::to_string(s.base_damage)); + + if (s.base_armor > 0) + draw_line("Armor: " + std::to_string(s.base_armor)); + + draw_line("Health: " + std::to_string(s.base_health)); + + if (s.base_speed > 0.f) + draw_line("Speed: " + std::to_string(s.base_speed)); + + if (s.max_level > 0) + draw_line("Max Lvl: " + std::to_string(s.max_level)); + return; } - SDL_DestroySurface(surf); } } diff --git a/src/inventory/inventory.hpp b/src/inventory/inventory.hpp index 7d6f7e9..e4aefe7 100644 --- a/src/inventory/inventory.hpp +++ b/src/inventory/inventory.hpp @@ -38,7 +38,7 @@ struct InventoryItem : Inventory_s { : Inventory_s(d, InventoryObjectType::ITEM), type(t) { - const auto &info = Item::get(t); + const ItemStats& info = Item::get(t); health_current = info.base_health; health_max = info.base_health; speed = info.base_speed; @@ -78,6 +78,7 @@ public: static bool draw_menu; static ItemType selected_build_item; + static InventorySelection* selected_item; static bool clicked_button_craft; static Equipement diff --git a/src/inventory/item.hpp b/src/inventory/item.hpp index e30f076..6075d01 100644 --- a/src/inventory/item.hpp +++ b/src/inventory/item.hpp @@ -21,7 +21,7 @@ enum class EquipmentSlot { // also add Item::stats if adding to ItemType! -enum class ItemType { +enum class ItemType : uint32_t { NONE, HAND, BUILDING_TOOL, diff --git a/src/map/map.cpp b/src/map/map.cpp index 04bf62b..e8039c1 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -931,7 +931,7 @@ namespace Map { continue; bird->Render(renderer); - std::cout << "I AM NOT GETTING CALLED BIRD AMP " << bird->pos_y << " " << bird->altitude << std::endl; + // std::cout << "I AM GETTING CALLED BIRD AMP " << bird->pos_y << " " << bird->altitude << std::endl; bird->drawn = true; } //std::cout << player_behind << std::endl; @@ -970,7 +970,7 @@ namespace Map { } for (Bird* bird : Bird::birds) if (!bird->drawn) { - std::cout << "BIRD AMP " << bird->pos_y << " " << bird->altitude << std::endl; + //std::cout << "BIRD AMP " << bird->pos_y << " " << bird->altitude << std::endl; bird->Render(renderer); } } diff --git a/src/objects/object.hpp b/src/objects/object.hpp index 3586bb1..d2be430 100644 --- a/src/objects/object.hpp +++ b/src/objects/object.hpp @@ -9,7 +9,7 @@ class Inventory; class Messages; -enum class ItemType; +enum class ItemType : uint32_t ; enum class ObjectCategory { diff --git a/src/resource/resource.hpp b/src/resource/resource.hpp index 139f203..8f1f47c 100644 --- a/src/resource/resource.hpp +++ b/src/resource/resource.hpp @@ -3,7 +3,7 @@ #include "objects/object.hpp" #include "resource/resource_data.hpp" -enum class ItemType; +enum class ItemType : uint32_t ; class Resource : public Object { public: diff --git a/src/vegetables/vegetables.hpp b/src/vegetables/vegetables.hpp index bba1593..f5c4b99 100644 --- a/src/vegetables/vegetables.hpp +++ b/src/vegetables/vegetables.hpp @@ -5,7 +5,7 @@ class Inventory; class Messages; -enum class ItemType; +enum class ItemType : uint32_t ; enum class EquipmentSlot; class Vegetable; @@ -13,7 +13,7 @@ class Vegetable; namespace VegetableData { extern std::vector<Vegetable*> all_vegetables; - enum class Type { + enum class Type : uint32_t { NONE, CARROT, RADDISH,