Skip to content

Commit

Permalink
Refactor player crafting
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmMoltony committed Jan 28, 2024
1 parent 37a9154 commit bef1b09
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 47 deletions.
25 changes: 2 additions & 23 deletions include/player.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @file player.hpp
* @brief Player, crafting and inventory management.
* @note i should refactor this
*/

#pragma once
Expand All @@ -28,6 +27,8 @@
class Player
{
public:
class Crafting;

/**
* @brief Number of items in the player's inventory
*/
Expand Down Expand Up @@ -106,11 +107,6 @@ class Player
*/
u8 inventoryMoveSelect;

/**
* @brief Index of the selected crafting recipe in the crafting menu
*/
u8 craftingSelect;

/**
* @brief Index of the selected item in chest
*/
Expand Down Expand Up @@ -315,13 +311,6 @@ class Player
*/
void draw(const Camera &camera, Font &font, Font &fontRu);

/**
* @brief Draw crafting screen
* @param font English font
* @param fontRu Russian font
*/
void drawCrafting(Font &font, Font &fontRu);

/**
* @brief Draw the player's body (and head)
* @param camera camera to use
Expand Down Expand Up @@ -371,11 +360,6 @@ class Player
*/
void updateFullInventory(void);

/**
* @brief Update crafting screen
*/
void updateCrafting(void);

/**
* @brief Update inventory screen
*/
Expand Down Expand Up @@ -635,9 +619,4 @@ class Player
* @brief Get a reference to the player's inventory
*/
Inventory &getInventory(void);

/**
* @brief Initialize the crafting system
*/
static void initCrafting(void);
};
26 changes: 26 additions & 0 deletions include/playercrafting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file playercrafting.hpp
* @brief Player crafting
*/

#pragma once

#include "player.hpp"
#include "inventory.hpp"
#include "crafting.hpp"
#include <vector>

class Player::Crafting
{
private:
static Inventory *playerInventory;
static std::vector<CraftingRecipe> craftingRecipes;
static u8 craftingSelect;

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 setInventory(Inventory *inv);
};
3 changes: 2 additions & 1 deletion source/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "controlsmgr.hpp"
#include "settingsmgr.hpp"
#include "ui.hpp"
#include "playercrafting.hpp"
#include "util.h"
#include "mtnconfig.h"
#include "mtnconfigversion.h"
Expand Down Expand Up @@ -410,7 +411,7 @@ void Game::init(int argc, char **argv)
mtnlogMessageTagC(MTNLOG_INFO, "init", "Initializing crafting");

// init crafting
Player::initCrafting();
Player::Crafting::init();

mtnlogMessageTagC(MTNLOG_INFO, "init", "Loading general assets");

Expand Down
11 changes: 7 additions & 4 deletions source/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "stats.hpp"
#include "controlsmgr.hpp"
#include "settingsmgr.hpp"
#include "playercrafting.hpp"
#include "util.h"
#include "mtnconfig.h"
#include "glext.h"
Expand Down Expand Up @@ -160,7 +161,7 @@ void Player::unloadSounds(void)
unloadsfx4(LADDER);
}

Player::Player() : x(0), y(0), aimX(0), aimY(0), spawnX(0), spawnY(0), health(FULL_HEALTH), airY(0), hotbarSelect(0), inventorySelect(0), inventoryMoveSelect(NUM_INVENTORY_ITEMS), craftingSelect(0),
Player::Player() : x(0), y(0), aimX(0), aimY(0), spawnX(0), spawnY(0), health(FULL_HEALTH), airY(0), hotbarSelect(0), inventorySelect(0), inventoryMoveSelect(NUM_INVENTORY_ITEMS),
chestSelect(0), chestMoveSelect(40), normalSpriteFPI(0), spawnImmunity(SPAWN_IMMUNITY), velX(0), velY(0), falling(true), jumping(false), fullInventory(false), inventoryCrafting(false),
chestOpen(false), sneaking(false), facing(Facing::Right), inventory(20),
chest(nullptr), sign(nullptr), bodySprite(AnimatedSprite(5, AnimatedSpriteMode::ReverseLoop,
Expand All @@ -170,6 +171,9 @@ Player::Player() : x(0), y(0), aimX(0), aimY(0), spawnX(0), spawnY(0), health(FU
normalSpriteFPI = bodySprite.getFramesPerImage();
// clear the inventory just in case
inventory.clear();

// give inventory to crafting
Crafting::setInventory(&inventory);
}

// function for drawing an inventory aka list of items
Expand Down Expand Up @@ -519,7 +523,7 @@ void Player::drawInventory(Font &font, Font &fontRu)
}

if (inventoryCrafting) // when crafting
drawCrafting(font, fontRu);
Crafting::draw(font, fontRu, this);
else
{
_drawInventory(inventory, font, inventorySelect, inventoryMoveSelect);
Expand Down Expand Up @@ -918,7 +922,6 @@ void Player::updateFullInventory(void)
{
// when l is pressed, open crafting (or close)
inventoryCrafting = !inventoryCrafting;
craftingSelect = 0;
inventorySelect = 0;
mmEffectEx(&Game::instance->sndClick);
}
Expand All @@ -929,7 +932,7 @@ void Player::updateFullInventory(void)
}

if (inventoryCrafting)
updateCrafting();
Crafting::update(this);
else
updateInventory();
}
Expand Down
45 changes: 26 additions & 19 deletions source/playercrafting.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#include "player.hpp"
#include "playercrafting.hpp"
#include "crafting.hpp"
#include "game.hpp"
#include "mtnconfig.h"
#include "mtnlog.h"
#include "glext.h"
#include <fstream>

static std::vector<CraftingRecipe> _craftingRecipes;
Inventory *Player::Crafting::playerInventory = nullptr;
std::vector<CraftingRecipe> Player::Crafting::craftingRecipes;
u8 Player::Crafting::craftingSelect = 0;

void Player::initCrafting(void)
void Player::Crafting::init(void)
{
std::map<std::string, float> loadTimes;

Expand All @@ -24,7 +26,7 @@ void Player::initCrafting(void)
if (!line.empty() && line[0] != '#')
{
cpuStartTiming(0); // start measuring time
_craftingRecipes.push_back(CraftingRecipe(line)); // parse + add the recipe
craftingRecipes.push_back(CraftingRecipe(line)); // parse + add the recipe
float timeTook = (float)cpuEndTiming() / BUS_CLOCK; // get how much time it took
mtnlogMessageTagC(MTNLOG_INFO, "crafting", "loaded %s in %f s", line.c_str(), timeTook); // print how much it took
loadTimes[line] = timeTook; // put the time into the list
Expand Down Expand Up @@ -76,19 +78,19 @@ static constexpr u8 CRAFTING_SLOTS_Y = 60;
static constexpr u8 RECIPE_NAME_X = 16;
static constexpr u8 RECIPE_NAME_Y = 35;

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

for (size_t i = 0; i < numRecipes; ++i)
{
// calculate the position of the current slot
u8 slotX = CRAFTING_SLOTS_X + (i % RECIPES_PER_ROW) * 16;
u8 slotY = CRAFTING_SLOTS_Y + (i / RECIPES_PER_ROW) * 16;

CraftingRecipe recipe = _craftingRecipes[i];
CraftingRecipe recipe = craftingRecipes[i];

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

// if can't craft, make slot red
if (!cc)
Expand Down Expand Up @@ -180,37 +182,37 @@ void Player::drawCrafting(Font &font, Font &fontRu)
}

// print recipe full name
CraftingRecipe recipe = _craftingRecipes[craftingSelect];
CraftingRecipe recipe = craftingRecipes[craftingSelect];
switch (Game::instance->lang)
{
case Language::English:
font.printShadow(RECIPE_NAME_X, RECIPE_NAME_Y, recipe.getFullName(this));
font.printShadow(RECIPE_NAME_X, RECIPE_NAME_Y, recipe.getFullName(player));
break;
case Language::Russian:
fontRu.printShadow(RECIPE_NAME_X, RECIPE_NAME_Y, recipe.getFullName(this));
fontRu.printShadow(RECIPE_NAME_X, RECIPE_NAME_Y, recipe.getFullName(player));
break;
}
}

void Player::updateCrafting(void)
void Player::Crafting::update(Player *player)
{
u32 kdown = keysDown();
if (kdown & KEY_A)
{
bool crafted = false;

CraftingRecipe recipe = _craftingRecipes[craftingSelect];
CraftingRecipe recipe = craftingRecipes[craftingSelect];

bool cc = _canCraft(this, recipe);
bool cc = _canCraft(player, recipe);
if (cc)
{
crafted = true;
inventory.add(recipe.getOutput());
playerInventory->add(recipe.getOutput());

// remove the recipe ingredients
const std::vector<InventoryItem> *rvec = recipe.getRecipe();
for (auto item : *rvec)
inventory.remove(item);
playerInventory->remove(item);
}

// play click sound if crafted successfully
Expand All @@ -222,18 +224,18 @@ void Player::updateCrafting(void)
if (kdown & KEY_LEFT)
{
if (craftingSelect - 1 < 0)
craftingSelect = _craftingRecipes.size() - 1;
craftingSelect = craftingRecipes.size() - 1;
else
--craftingSelect;
}
else if (kdown & KEY_RIGHT)
{
if (++craftingSelect > _craftingRecipes.size() - 1)
if (++craftingSelect > craftingRecipes.size() - 1)
craftingSelect = 0;
}
else if (kdown & KEY_DOWN)
{
if ((std::vector<CraftingRecipe>::size_type)(craftingSelect + RECIPES_PER_ROW) <= _craftingRecipes.size() - 1)
if ((std::vector<CraftingRecipe>::size_type)(craftingSelect + RECIPES_PER_ROW) <= craftingRecipes.size() - 1)
craftingSelect += 14;
}
else if (kdown & KEY_UP)
Expand All @@ -242,3 +244,8 @@ void Player::updateCrafting(void)
craftingSelect -= 14;
}
}

void Player::Crafting::setInventory(Inventory *inv)
{
playerInventory = inv;
}

0 comments on commit bef1b09

Please sign in to comment.