Skip to content

Commit

Permalink
Finish player crafting refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmMoltony committed Jan 28, 2024
1 parent bef1b09 commit 711a382
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 43 deletions.
2 changes: 1 addition & 1 deletion include/crafting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CraftingRecipe
*
* The full name is used in the crafting recipe screen
*/
std::string getFullName(Player *player);
std::string getFullName(Inventory *inventory);

/**
* @brief Get output item
Expand Down
6 changes: 6 additions & 0 deletions include/inventory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,10 @@ class Inventory
* @param item Item to remove
*/
void remove(InventoryItem item);

/**
* @brief Count how many items with the given ID are in the inventory
* @param id item ID to count
*/
u32 count(InventoryItem::ID id);
};
6 changes: 0 additions & 6 deletions include/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,6 @@ class Player
*/
s16 getHealth(void);

/**
* @brief Count how many items with the given ID the player has
* @param item item ID to check
*/
u16 countItems(InventoryItem::ID item);

/**
* @brief Get the player's bottom hitbox
*/
Expand Down
6 changes: 4 additions & 2 deletions include/playercrafting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ class Player::Crafting
static std::vector<CraftingRecipe> craftingRecipes;
static u8 craftingSelect;

static bool canCraft(CraftingRecipe recipe);

public:
static void init(void);
static void draw(Font &font, Font &fontRu, Player *player);
static void update(Player *player); // TODO the only reason this (and draw()) needs a player pointer is for _canCraft and other functions depending on Player::countItems (and probably other inventory-management functions made before Inventory class), which can be implemented with just the inventory
static void draw(Font &font, Font &fontRu);
static void update(void);

static void setInventory(Inventory *inv);
};
6 changes: 3 additions & 3 deletions source/crafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ CraftingRecipe::CraftingRecipe(const std::string &recipeFile)
construct(recipeFile.c_str());
}

std::string CraftingRecipe::getFullName(Player *player)
std::string CraftingRecipe::getFullName(Inventory *inventory)
{
Language lang = Game::instance->lang;

Expand All @@ -99,10 +99,10 @@ std::string CraftingRecipe::getFullName(Player *player)
StringVector itemVec;

std::transform(recipe.begin(), recipe.end(), itemVec.begin(), std::back_inserter(itemVec),
[player, lang](InventoryItem item, const std::string &str)
[inventory, lang](InventoryItem item, const std::string &str)
{
(void)str; // trash the string
return std::to_string(player->countItems(item.id)) + '/' +
return std::to_string(inventory->count(item.id)) + '/' +
std::to_string(item.amount) + ' ' +
item.getName(); });

Expand Down
19 changes: 18 additions & 1 deletion source/inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void Inventory::remove(InventoryItem::ID id)
return;
}

for (u8 i = 0; i < 20; ++i)
for (u8 i = 0; i < numSlots; ++i)
{
// cheeck if the item exists and it has the desired ID
if (items[i].id == id && items[i].amount > 0)
Expand All @@ -198,3 +198,20 @@ void Inventory::remove(InventoryItem item)
{
remove(item.id, item.amount);
}

u32 Inventory::count(InventoryItem::ID id)
{
if (id == InventoryItem::ID::AnyPlanks)
{
u32 c = 0;
for (int i = 0; i < InventoryItem::numPlanksItemIDs; ++i)
c += count(InventoryItem::planksItemIDs[i]);
return c;
}

u32 count = 0;
for (u8 i = 0; i < numSlots; i++)
if (items[i].id == id)
count += items[i].amount;
return count;
}
24 changes: 2 additions & 22 deletions source/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ void Player::drawInventory(Font &font, Font &fontRu)
}

if (inventoryCrafting) // when crafting
Crafting::draw(font, fontRu, this);
Crafting::draw(font, fontRu);
else
{
_drawInventory(inventory, font, inventorySelect, inventoryMoveSelect);
Expand Down Expand Up @@ -932,7 +932,7 @@ void Player::updateFullInventory(void)
}

if (inventoryCrafting)
Crafting::update(this);
Crafting::update();
else
updateInventory();
}
Expand Down Expand Up @@ -2924,26 +2924,6 @@ s16 Player::getHealth(void)
return health;
}

u16 Player::countItems(InventoryItem::ID item)
{
// special case for any planks
// (we just count all planks types)
if (item == InventoryItem::ID::AnyPlanks)
{
u16 c = 0;
for (int i = 0; i < InventoryItem::numPlanksItemIDs; ++i)
c += countItems(InventoryItem::planksItemIDs[i]);
return c;
}

u16 count = 0;
for (u8 i = 0; i < 20; ++i)
// if target item, add its amount
if (inventory[i].id == item)
count += inventory[i].amount;
return count;
}

// hitboxes

Rect Player::getRectBottom()
Expand Down
16 changes: 8 additions & 8 deletions source/playercrafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ void Player::Crafting::init(void)
highest, highestName.c_str());
}

static bool _canCraft(Player *pThis, CraftingRecipe recipe)
bool Player::Crafting::canCraft(CraftingRecipe recipe)
{
const std::vector<InventoryItem> *rvec = recipe.getRecipe(); // recipe vector
for (auto item : *rvec)
{
u16 playerItemCount = pThis->countItems(item.id);
u16 playerItemCount = playerInventory->count(item.id);
u8 recipeItemCount = item.amount;
if (playerItemCount < recipeItemCount)
return false;
Expand All @@ -78,7 +78,7 @@ static constexpr u8 CRAFTING_SLOTS_Y = 60;
static constexpr u8 RECIPE_NAME_X = 16;
static constexpr u8 RECIPE_NAME_Y = 35;

void Player::Crafting::draw(Font &font, Font &fontRu, Player *player)
void Player::Crafting::draw(Font &font, Font &fontRu)
{
size_t numRecipes = craftingRecipes.size();

Expand All @@ -90,7 +90,7 @@ void Player::Crafting::draw(Font &font, Font &fontRu, Player *player)

CraftingRecipe recipe = craftingRecipes[i];

bool cc = _canCraft(player, recipe);
bool cc = canCraft(recipe);

// if can't craft, make slot red
if (!cc)
Expand Down Expand Up @@ -186,15 +186,15 @@ void Player::Crafting::draw(Font &font, Font &fontRu, Player *player)
switch (Game::instance->lang)
{
case Language::English:
font.printShadow(RECIPE_NAME_X, RECIPE_NAME_Y, recipe.getFullName(player));
font.printShadow(RECIPE_NAME_X, RECIPE_NAME_Y, recipe.getFullName(playerInventory));
break;
case Language::Russian:
fontRu.printShadow(RECIPE_NAME_X, RECIPE_NAME_Y, recipe.getFullName(player));
fontRu.printShadow(RECIPE_NAME_X, RECIPE_NAME_Y, recipe.getFullName(playerInventory));
break;
}
}

void Player::Crafting::update(Player *player)
void Player::Crafting::update(void)
{
u32 kdown = keysDown();
if (kdown & KEY_A)
Expand All @@ -203,7 +203,7 @@ void Player::Crafting::update(Player *player)

CraftingRecipe recipe = craftingRecipes[craftingSelect];

bool cc = _canCraft(player, recipe);
bool cc = canCraft(recipe);
if (cc)
{
crafted = true;
Expand Down

0 comments on commit 711a382

Please sign in to comment.