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.gzsrc/inventory/item.cpp raw
#include "inventory/item.hpp"
#include <iostream>
#include "inventory/inventory.hpp"
#include "character/message.hpp"
namespace Item {
ItemCrafting crafting_item;
std::vector<ItemStats> stats = {
// NONE
{
ItemType::NONE,
"None",
{}, // empty cost
0, 0, 0, 0,
0, 0, 0,
0, 0,
EquipmentSlot::NONE,
0,
BuildingType::NONE
},
// HAND
{
ItemType::HAND,
"Hands",
{}, // empty cost
0, 0, 0, 0,
0, 0, 0,
0, 0,
EquipmentSlot::HAND_RIGHT,
0,
BuildingType::NONE
},
// BUILDING_TOOL
{
ItemType::BUILDING_TOOL,
"Building tool",
{ {ResourceType::WOOD, 2}, {ResourceType::STONE, 1} },
0, // base_damage
0, // base_armor
20, // base_health
1.2, // base_speed
0, // dmg_per_level
0, // armor_per_level
5, // health_per_level
3,
4.0,
EquipmentSlot::HAND_RIGHT,
0,
BuildingType::NONE
},
// SWORD_WOOD
{
ItemType::SWORD_WOOD,
"Sword wood",
{ {ResourceType::WOOD, 6} },
2, // base_damage
0, // base_armor
20, // base_health
0.8, // base_speed
1, // dmg_per_level
0, // armor_per_level
5, // health_per_level
3, // max_level
6.0,
EquipmentSlot::HAND_RIGHT,
1,
BuildingType::RESEARCH_WOOD_1
},
// SWORD_STONE
{
ItemType::SWORD_STONE,
"Sword stone",
{ {ResourceType::WOOD, 2}, {ResourceType::STONE, 4} },
4, // base_damage
0, // base_armor
30, // base_health
1.6, // base_speed
1, // dmg_per_level
0, // armor_per_level
5, // health_per_level
3, // max_level
6.0,
EquipmentSlot::HAND_RIGHT,
3,
BuildingType::RESEARCH_WOOD_1
},
// AXE_STONE
{
ItemType::WOODAXE_STONE,
"Wood Axe made of stone",
{ {ResourceType::WOOD, 3}, {ResourceType::STONE, 5} },
2, // base_damage
0, // base_armor
30, // base_health
1.5, // base_speed
1, // dmg_per_level
0, // armor_per_level
5, // health_per_level
3, // max_level
6.0,
EquipmentSlot::HAND_RIGHT,
1,
BuildingType::RESEARCH_WOOD_1
},
// BONE_BREASTPLATE
{
ItemType::BONE_BREASTPLATE,
"Bone breasplate",
{ {ResourceType::BONE, 10}, {ResourceType::WOOD, 2} },
0, // base_damage
5, // base_armor
30, // base_health
0, // base_speed
0, // dmg_per_level
2, // armor_per_level
5, // health_per_level
3,
7.0,
EquipmentSlot::CHEST,
3,
BuildingType::RESEARCH_WOOD_2
},
// PICKAXE_STONE
{
ItemType::PICKAXE_STONE,
"Pickaxe Stone",
{ {ResourceType::WOOD, 3}, {ResourceType::STONE, 5} },
0, // base_damage
0, // base_armor
30, // base_health
0, // base_speed
0, // dmg_per_level
2, // armor_per_level
5, // health_per_level
3,
8.0,
EquipmentSlot::HAND_RIGHT,
0,
BuildingType::RESEARCH_WOOD_1
},
// CULTIVATOR
{
ItemType::CULTIVATOR,
"Cultivator",
{ {ResourceType::WOOD, 4} },
0, // base_damage
0, // base_armor
30, // base_health
0, // base_speed
0, // dmg_per_level
2, // armor_per_level
5, // health_per_level
3,
4.0,
EquipmentSlot::HAND_RIGHT,
0,
BuildingType::RESEARCH_WOOD_1
},
// LAST
{
ItemType::LAST,
"None",
{}, // empty cost
0, 0, 0, 0,
0, 0, 0,
0, 0,
EquipmentSlot::NONE,
0,
BuildingType::NONE
}
};
bool is_Weapon(ItemType& item) {
switch (item) {
case ItemType::SWORD_WOOD:
case ItemType::SWORD_STONE:
case ItemType::WOODAXE_STONE:
return true;
default:
return false;
}
}
const ItemStats& get(ItemType t) {
return stats[static_cast<size_t>(t)];
}
int get_Damage(ItemType t, int lvl) {
const ItemStats& s = get(t);
if (lvl > s.max_level) lvl = s.max_level;
return s.base_damage + s.dmg_per_level * (lvl - 1);
}
int get_Health(ItemType t, int lvl) {
const ItemStats& s = get(t);
return s.base_health + s.health_per_level * (lvl - 1);
}
bool craft_ItemStart(Inventory* inv, ItemType type, ItemCrafting& crafting_it) {
if (type == ItemType::NONE) return false;
const ItemStats& stats = Item::get(type);
crafting_it = {
type,
{},
0,
stats.crafting_duration,
false,
};
// check resources
for (auto& cost : stats.cost)
if (inv->get_Resource(cost.type) < cost.amount)
return false;
crafting_it.stats = stats;
crafting_it.is_crafting = true;
std::cout << "CRAFTING STARTED!\n";
return true;
}
void craft_Stop(ItemCrafting& crafting_it) {
std::cout << "Crafting stopped!\n";
ItemCrafting reset;
crafting_it = reset;
}
bool craft_Item(Inventory* inv, ItemCrafting& crafting_it, Messages* messages) {
// check resources
for (auto& cost : crafting_it.stats.cost)
if (inv->get_Resource(cost.type) < cost.amount)
return false;
crafting_it.duration += Default::delta_time;
if (crafting_it.duration >= crafting_it.stats.crafting_duration) {
// remove resources
for (auto& cost : crafting_it.stats.cost)
inv->remove_Resource(cost.type, cost.amount);
// add item instance
InventoryItem* item = inv->add_Item(crafting_it.type, messages);
if (!item) {
std::cout << "Someting failed while adding crafted item!\n";
return false;
}
std::cout << "Item crafted!\n";
return false;
}
return true;
}
std::vector<ItemType>
get_ResearchedItems(const std::vector<bool>& researched) {
std::vector<ItemType> result;
result.reserve(static_cast<size_t>(ItemType::LAST));
for (size_t i = 0; i < researched.size(); i++) {
if (researched[i]) {
result.push_back(static_cast<ItemType>(i));
}
}
return result;
}
std::string
to_String(ItemType type) {
switch (type) {
case ItemType::NONE: return "None";
case ItemType::HAND: return "Hand";
case ItemType::BUILDING_TOOL: return "Construction tool";
case ItemType::SWORD_WOOD: return "Sword wood";
case ItemType::SWORD_STONE: return "Sword stone";
case ItemType::WOODAXE_STONE: return "Woodaxe stone";
case ItemType::BONE_BREASTPLATE: return "Bone Breastplate";
case ItemType::PICKAXE_STONE: return "Pickaxe stone";
case ItemType::CULTIVATOR: return "Cultivator";
case ItemType::LAST: return "Last";
}
return "";
}
}