Skip to content

Commit

Permalink
Merge pull request #8 from adamhutchings/inventory
Browse files Browse the repository at this point in the history
Add hotbar
  • Loading branch information
adamhutchings authored Jul 13, 2020
2 parents b652267 + ca9adab commit fab8718
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 9 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
OBJECTS = ./bin/objects/entity/player.o ./bin/objects/entity/entity.o ./bin/objects/entity/item.o \
./bin/objects/render/m_window.o ./bin/objects/render/renderable.o ./bin/objects/world/block.o \
./bin/objects/world/world.o ./bin/objects/item/item_stack.o ./bin/objects/item/inventory.o \
./bin/objects/gui/m_button.o ./bin/objects/main.o
./bin/objects/gui/m_button.o ./bin/objects/main.o ./bin/objects/gui/hotbar_renderer.o

SFML_PATH = /usr/local/Cellar/sfml/2.5.1

Expand All @@ -13,7 +13,7 @@ GCC = g++ -std=c++17
$(GCC) $(OBJECTS) $(SFML_ARGS) -o ./bin/mc2d

./bin/objects/entity/player.o : ./src/entity/player.cpp ./src/entity/player.h ./src/world/world.h \
./src/entity/entity.h ./src/render/m_window.h ./src/main.h ./src/item/inventory.h
./src/entity/entity.h ./src/render/m_window.h ./src/main.h ./src/item/inventory.h ./src/gui/hotbar_renderer.h
$(GCC) -c ./src/entity/player.cpp -o ./bin/objects/entity/player.o

./bin/objects/entity/entity.o : ./src/entity/entity.cpp ./src/entity/entity.h \
Expand Down Expand Up @@ -49,5 +49,9 @@ GCC = g++ -std=c++17
$(GCC) -c ./src/gui/m_button.cpp -o ./bin/objects/gui/m_button.o

./bin/objects/main.o : ./src/main.cpp ./src/main.h ./src/world/block.h ./src/render/m_window.h ./src/world/world.h \
./src/entity/player.h ./src/gui/m_button.h
./src/entity/player.h ./src/gui/m_button.h ./src/gui/hotbar_renderer.h
$(GCC) -c ./src/main.cpp -o ./bin/objects/main.o

./bin/objects/gui/hotbar_renderer.o : ./src/gui/hotbar_renderer.cpp ./src/gui/hotbar_renderer.h \
./src/render/m_window.h ./src/render/renderable.h ./src/item/inventory.h ./src/main.h
$(GCC) -c ./src/gui/hotbar_renderer.cpp -o ./bin/objects/gui/hotbar_renderer.o
1 change: 1 addition & 0 deletions src/entity/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Player::Player(M2DWorld& world)
void Player::render(MWindow& window) const {
window.draw(facing ? headRight : headLeft); // Brilliance in a nutshell
window.draw(body);
renderHotbar(window, inventory->sections[0]); // Render the hotbar
}

void Player::updateSpritePosition(MWindow& relativeTo) {
Expand Down
1 change: 1 addition & 0 deletions src/entity/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../render/m_window.h"
#include "../main.h"
#include "../item/inventory.h"
#include "../gui/hotbar_renderer.h"

// Hardcoded player head coords for now
#define PLAYER_HEAD_X 1050
Expand Down
49 changes: 49 additions & 0 deletions src/gui/hotbar_renderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "hotbar_renderer.h"

InventoryFrame::InventoryFrame(MWindow& window, int x, int y)
:
// Initialize the rects
b(sf::Vector2f(FRAME_SIZE, FRAME_THICKNESS)),
t(sf::Vector2f(FRAME_SIZE, FRAME_THICKNESS)),
l(sf::Vector2f(FRAME_THICKNESS, FRAME_SIZE)),
r(sf::Vector2f(FRAME_THICKNESS, FRAME_SIZE)) {
// Giving positions to the various elements
b.setPosition(sf::Vector2f(x - (FRAME_SIZE / 2), y + (FRAME_SIZE / 2) - FRAME_THICKNESS));
t.setPosition(sf::Vector2f(x - (FRAME_SIZE / 2), y - (FRAME_SIZE / 2)));
l.setPosition(sf::Vector2f(x - (FRAME_SIZE / 2), y - (FRAME_SIZE / 2)));
r.setPosition(sf::Vector2f(x + (FRAME_SIZE / 2) - (FRAME_THICKNESS / 2), y - FRAME_SIZE / 2));
}

void InventoryFrame::render(MWindow& window) {
// Just draw all the rectangles
window.draw(b);
window.draw(t);
window.draw(l);
window.draw(r);
}

void renderHotbar(MWindow& window, InventorySection* section) {
int centerX;
sf::Text text; // For later
for (int i = 0; i < section->size; i++) {
centerX = WN_WIDTH / 2 - (section->size * FRAME_SIZE / 2) + (i * FRAME_SIZE);
InventoryFrame(window, centerX, HOTBAR_DISPLAY_HEIGHT).render(window);
section->contents[i]->spr.setPosition(
centerX - (200 * 0.4 / 2), HOTBAR_DISPLAY_HEIGHT - (200 * 0.4 / 2)
);
if (section->contents[i]->name != "empty") {
window.draw(section->contents[i]->spr);
if (section->contents[i]->count > 1) {
// The item counter display
text.setCharacterSize(INVENTORY_FONT_SIZE);
text.setFont(INVENTORY_FONT);
text.setPosition(
centerX - 100 + FONT_X_OFFSET,
HOTBAR_DISPLAY_HEIGHT - 100 + FONT_Y_OFFSET
);
text.setString(std::to_string(section->contents[i]->count));
window.draw(text);
}
}
}
}
36 changes: 36 additions & 0 deletions src/gui/hotbar_renderer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef M_HB_RENDERER_H
#define M_HB_RENDERER_H

#include <SFML/Graphics.hpp>
#include <string>
#include "../render/m_window.h"
#include "../item/inventory.h"
#include "../render/renderable.h"
#include "../main.h"

#define FRAME_THICKNESS 20
#define FRAME_SIZE 160

#define HOTBAR_DISPLAY_HEIGHT 1400

#define INVENTORY_FONT_SIZE 30

#define FONT_X_OFFSET 130
#define FONT_Y_OFFSET 130

inline sf::Font INVENTORY_FONT;

class InventorySection;

class InventoryFrame {
public:
sf::RectangleShape b, t, l, r; // The four sides
InventoryFrame(MWindow& window, int xPos, int yPos);
void render(MWindow& window);
};

// This provides a method to render an inventory section in a straight line
// at the bottom of the screen.
void renderHotbar(MWindow& window, InventorySection* section);

#endif // M_HB_RENDERER_H
12 changes: 7 additions & 5 deletions src/item/inventory.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "inventory.h"

InventorySection::InventorySection(MWindow& renderOn, int width) {
size = width;
this->contents = new ItemStack*[width];
for (int i = 0; i < width; i++) {
this->contents[i] = new ItemStack("empty", ItemStackState::INVENTORY, 0);
}
}

void InventorySection::addItem(ItemStack& is, int pos) {
if ((pos >= sizeof(this->contents) / sizeof(this->contents[0]))
if ((pos >= size - 1)
|| ((is.name != contents[pos]->name) && contents[pos]->name != "empty")){
// Illegal move! Nooo!
return;
Expand All @@ -20,6 +21,7 @@ void InventorySection::addItem(ItemStack& is, int pos) {
}

Inventory::Inventory(int num) {
size = num;
sections = new InventorySection* [num];
}

Expand All @@ -29,9 +31,9 @@ void Inventory::addItem(ItemStack& is) {
ItemStack* itemStack; // for later

// See if there are any matching item stacks
for (int i = 0; i < sizeof(sections) / sizeof(sections[0]); i++) {
for (int i = 0; i < size; i++) {
inventorySection = sections[i];
for (int j = 0; j < sizeof(inventorySection) / sizeof(inventorySection[0]); j++) {
for (int j = 0; j < inventorySection->size; j++) {
itemStack = inventorySection->contents[j];
if ((itemStack->count != 0) && (itemStack->name == is.name)) {
inventorySection->addItem(is, j);
Expand All @@ -41,9 +43,9 @@ void Inventory::addItem(ItemStack& is) {
}

// If there was no available slot
for (int i = 0; i < sizeof(sections) / sizeof(sections[0]); i++) {
for (int i = 0; i < size; i++) {
inventorySection = sections[i];
for (int j = 0; j < sizeof(inventorySection) / sizeof(inventorySection[0]); j++) {
for (int j = 0; j < inventorySection->size; j++) {
itemStack = inventorySection->contents[j];
if ((itemStack->count == 0) && (itemStack->name == "empty")) {
inventorySection->addItem(is, j);
Expand Down
2 changes: 2 additions & 0 deletions src/item/inventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

class InventorySection {
public:
int size;
ItemStack** contents;
InventorySection(MWindow& renderOn, int size);
void addItem(ItemStack& is, int pos);
Expand All @@ -18,6 +19,7 @@ class InventorySection {
// Abstract class
class Inventory {
public:
int size;
Inventory(int num);
InventorySection** sections;
void addItem(ItemStack& is);
Expand Down
11 changes: 10 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ void loadButtonFont() {
}
}

void loadInventoryFont() {
if (!INVENTORY_FONT.loadFromFile("res/fonts/Minecraft.ttf")) {
std::cout << "Whoops! An error occurred.\n";
exit(EXIT_FAILURE);
}
}

void loadTextures() {
if (!GRASS_TEX.loadFromFile("res/tex/grass.jpg") ||
!DIRT_TEX.loadFromFile("res/tex/dirt.jpg") ||
Expand Down Expand Up @@ -44,13 +51,15 @@ int main() {

// Setup
loadTextures();

loadInventoryFont();
loadButtonFont();

// For now, just create a new window
MWindow wn(WN_WIDTH, WN_HEIGHT, "2D Minecraft");
wn.color = WN_BG_COLOR;

InventoryFrame test(wn, 1000, 1000);

// Buttons for actions
MButton startGame(wn, startNewGame, "New Game", sf::Color(100, 100, 100), 1000, 1000);
MButton retButton(wn, returnToGame, "Resume", sf::Color(100, 100, 100), 1000, 800, true);
Expand Down
2 changes: 2 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "world/world.h"
#include "entity/player.h"
#include "gui/m_button.h"
#include "gui/hotbar_renderer.h"

// Window settings
#define WN_WIDTH 2000
Expand All @@ -20,6 +21,7 @@
#define WALK_SPEED 0.005

void loadButtonFont();
void loadInventoryFont();
void loadTextures();

void startNewGame(MWindow& window);
Expand Down

0 comments on commit fab8718

Please sign in to comment.