[Text] Added. [ItemWidget] Now displays item amount.
This commit is contained in:
parent
5c0735da26
commit
e01c6da109
6
TODO
6
TODO
@ -48,9 +48,9 @@ TODO
|
|||||||
|
|
||||||
## Items/Blocks
|
## Items/Blocks
|
||||||
|
|
||||||
• TODO: Créer un registre d’information pour les blocs/items (utiliser `ResourceHandler`?)
|
• DONE: Créer un registre d’information pour les blocs/items (utiliser `ResourceHandler`?)
|
||||||
• TODO: Créer une classe `ItemStack` et l’utiliser dans `Inventory`
|
• DONE: Créer une classe `ItemStack` et l’utiliser dans `Inventory`
|
||||||
• TODO: Rajouter les classes `BitmapFont` et `Text` pour afficher le nombre d’items dans un stack
|
• DONE: Rajouter les classes `BitmapFont` et `Text` pour afficher le nombre d’items dans un stack
|
||||||
• TODO: Essayer d’utiliser un seul `Inventory` pour le joueur
|
• TODO: Essayer d’utiliser un seul `Inventory` pour le joueur
|
||||||
→ Probablement en ajoutant un offset dans `InventoryWidget` et `Hotbar`
|
→ Probablement en ajoutant un offset dans `InventoryWidget` et `Hotbar`
|
||||||
• TODO: Rajouter le système de craft
|
• TODO: Rajouter le système de craft
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
#include "Image.hpp"
|
#include "Image.hpp"
|
||||||
#include "Inventory.hpp"
|
#include "Inventory.hpp"
|
||||||
|
#include "Text.hpp"
|
||||||
#include "Widget.hpp"
|
#include "Widget.hpp"
|
||||||
|
|
||||||
class ItemWidget : public Widget {
|
class ItemWidget : public Widget {
|
||||||
@ -24,8 +25,8 @@ class ItemWidget : public Widget {
|
|||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
u16 item() const { return m_inventory.getStack(m_x, m_y).item().id(); }
|
const ItemStack &stack() const { return m_inventory.getStack(m_x, m_y); }
|
||||||
void setItem(unsigned int id);
|
void setStack(unsigned int id, unsigned int amount = 1);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void draw(RenderTarget &target, RenderStates states) const override;
|
void draw(RenderTarget &target, RenderStates states) const override;
|
||||||
@ -36,6 +37,7 @@ class ItemWidget : public Widget {
|
|||||||
unsigned int m_y = 0;
|
unsigned int m_y = 0;
|
||||||
|
|
||||||
Image m_image;
|
Image m_image;
|
||||||
|
Text m_text;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ITEMWIDGET_HPP_
|
#endif // ITEMWIDGET_HPP_
|
||||||
|
@ -23,6 +23,7 @@ class MouseItemWidget : public ItemWidget {
|
|||||||
void onEvent(const SDL_Event &event);
|
void onEvent(const SDL_Event &event);
|
||||||
|
|
||||||
void swapItems(ItemWidget &widget);
|
void swapItems(ItemWidget &widget);
|
||||||
|
void putItem(ItemWidget &widget);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void updatePosition(float x, float y);
|
void updatePosition(float x, float y);
|
||||||
|
38
include/gui/Text.hpp
Normal file
38
include/gui/Text.hpp
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* =====================================================================================
|
||||||
|
*
|
||||||
|
* Filename: Text.hpp
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
* Created: 24/06/2018 01:48:24
|
||||||
|
*
|
||||||
|
* Author: Quentin Bazin, <quent42340@gmail.com>
|
||||||
|
*
|
||||||
|
* =====================================================================================
|
||||||
|
*/
|
||||||
|
#ifndef TEXT_HPP_
|
||||||
|
#define TEXT_HPP_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "IDrawable.hpp"
|
||||||
|
#include "Transformable.hpp"
|
||||||
|
#include "VertexBuffer.hpp"
|
||||||
|
|
||||||
|
class Text : public IDrawable, public Transformable {
|
||||||
|
public:
|
||||||
|
Text();
|
||||||
|
|
||||||
|
void setText(const std::string &text) { m_text = text; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void draw(RenderTarget &target, RenderStates states) const override;
|
||||||
|
|
||||||
|
std::string m_text;
|
||||||
|
|
||||||
|
Texture &m_texture;
|
||||||
|
VertexBuffer m_vbo;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // TEXT_HPP_
|
@ -23,7 +23,7 @@ class ItemStack {
|
|||||||
|
|
||||||
const Item &item() const { return *m_item; }
|
const Item &item() const { return *m_item; }
|
||||||
|
|
||||||
u16 amount() const;
|
u16 amount() const { return m_amount; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const Item *m_item = nullptr;
|
const Item *m_item = nullptr;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<textures>
|
<textures>
|
||||||
<texture name="blocks" path="textures/blocks.png" />
|
<texture name="blocks" path="textures/blocks.png" />
|
||||||
<texture name="widgets" path="textures/widgets.png" />
|
<texture name="font" path="textures/font.png" />
|
||||||
|
<texture name="widgets" path="textures/widgets.png" />
|
||||||
<texture name="workbench" path="textures/workbench.png" />
|
<texture name="workbench" path="textures/workbench.png" />
|
||||||
</textures>
|
</textures>
|
||||||
|
@ -39,6 +39,13 @@ void InventoryWidget::onEvent(const SDL_Event &event, MouseItemWidget &mouseItem
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_RIGHT) {
|
||||||
|
for (std::size_t i = 0 ; i < m_itemWidgets.size() ; ++i) {
|
||||||
|
if (m_itemWidgets[i].isPointInWidget(event.button.x / 3.0, event.button.y / 3.0)) {
|
||||||
|
mouseItemWidget.putItem(m_itemWidgets.at(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void InventoryWidget::draw(RenderTarget &target, RenderStates states) const {
|
void InventoryWidget::draw(RenderTarget &target, RenderStates states) const {
|
||||||
|
@ -19,15 +19,18 @@ ItemWidget::ItemWidget(Inventory &inventory, u16 x, u16 y, Widget *parent)
|
|||||||
m_image.load("texture-blocks");
|
m_image.load("texture-blocks");
|
||||||
m_image.setScale(2.0f / 3.0f, 2.0f / 3.0f, 1.0f);
|
m_image.setScale(2.0f / 3.0f, 2.0f / 3.0f, 1.0f);
|
||||||
m_image.setPosition(3, 3, 0);
|
m_image.setPosition(3, 3, 0);
|
||||||
m_image.setClipRect(0, 0, 0, 0);
|
m_image.setClipRect(0, 0, 16, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ItemWidget::update() {
|
void ItemWidget::update() {
|
||||||
m_image.setClipRect(item() * 16, item() / 16 * 16, 16, 16);
|
m_image.setClipRect(stack().item().id() * 16, stack().item().id() / 16 * 16, 16, 16);
|
||||||
|
|
||||||
|
m_text.setText(std::to_string(stack().amount()));
|
||||||
|
m_text.setPosition(16 - 4 - 6 * floor(log10(stack().amount())), 16 - 6, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ItemWidget::setItem(unsigned int id) {
|
void ItemWidget::setStack(unsigned int id, unsigned int amount) {
|
||||||
m_inventory.setStack(m_x, m_y, id);
|
m_inventory.setStack(m_x, m_y, id, amount);
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,5 +38,8 @@ void ItemWidget::draw(RenderTarget &target, RenderStates states) const {
|
|||||||
applyTransform(states);
|
applyTransform(states);
|
||||||
|
|
||||||
target.draw(m_image, states);
|
target.draw(m_image, states);
|
||||||
|
|
||||||
|
if (stack().item().id() && stack().amount() > 1)
|
||||||
|
target.draw(m_text, states);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
#include "MouseItemWidget.hpp"
|
#include "MouseItemWidget.hpp"
|
||||||
|
|
||||||
void MouseItemWidget::onEvent(const SDL_Event &event) {
|
void MouseItemWidget::onEvent(const SDL_Event &event) {
|
||||||
if (event.type == SDL_MOUSEMOTION && item() != 0) {
|
if (event.type == SDL_MOUSEMOTION && stack().item().id() != 0) {
|
||||||
updatePosition(event.motion.x, event.motion.y);
|
updatePosition(event.motion.x, event.motion.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,9 +24,31 @@ void MouseItemWidget::onEvent(const SDL_Event &event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MouseItemWidget::swapItems(ItemWidget &widget) {
|
void MouseItemWidget::swapItems(ItemWidget &widget) {
|
||||||
u16 widgetItem = widget.item();
|
u32 id = widget.stack().item().id();
|
||||||
widget.setItem(item());
|
u32 amount = widget.stack().amount();
|
||||||
setItem(widgetItem);
|
if (stack().item().id() != id) {
|
||||||
|
widget.setStack(stack().item().id(), stack().amount());
|
||||||
|
setStack(id, amount);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
widget.setStack(widget.stack().item().id(), widget.stack().amount() + stack().amount());
|
||||||
|
setStack(0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MouseItemWidget::putItem(ItemWidget &widget) {
|
||||||
|
if (!widget.stack().item().id()) {
|
||||||
|
widget.setStack(stack().item().id(), 1);
|
||||||
|
setStack(stack().item().id(), stack().amount() - 1);
|
||||||
|
}
|
||||||
|
else if (widget.stack().item().id() == stack().item().id()) {
|
||||||
|
widget.setStack(stack().item().id(), widget.stack().amount() + 1);
|
||||||
|
setStack(stack().item().id(), stack().amount() - 1);
|
||||||
|
}
|
||||||
|
else if (stack().item().id() == 0) {
|
||||||
|
setStack(widget.stack().item().id(), ceil(widget.stack().amount() / 2.0));
|
||||||
|
widget.setStack(widget.stack().item().id(), widget.stack().amount() / 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MouseItemWidget::updatePosition(float x, float y) {
|
void MouseItemWidget::updatePosition(float x, float y) {
|
||||||
|
36
source/gui/Text.cpp
Normal file
36
source/gui/Text.cpp
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* =====================================================================================
|
||||||
|
*
|
||||||
|
* Filename: Text.cpp
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
* Created: 24/06/2018 01:50:39
|
||||||
|
*
|
||||||
|
* Author: Quentin Bazin, <quent42340@gmail.com>
|
||||||
|
*
|
||||||
|
* =====================================================================================
|
||||||
|
*/
|
||||||
|
#include "ResourceHandler.hpp"
|
||||||
|
#include "Text.hpp"
|
||||||
|
#include "Texture.hpp"
|
||||||
|
|
||||||
|
#include "Sprite.hpp"
|
||||||
|
|
||||||
|
Text::Text() : m_texture(ResourceHandler::getInstance().get<Texture>("texture-font")) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void Text::draw(RenderTarget &target, RenderStates states) const {
|
||||||
|
applyTransform(states);
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
// FIXME: USE A VBO INSTEAD
|
||||||
|
for(char c : m_text) {
|
||||||
|
Sprite sprite{"texture-font", 8, 8};
|
||||||
|
sprite.setCurrentFrame(c);
|
||||||
|
sprite.setPosition(i * 6, 0, 0);
|
||||||
|
target.draw(sprite, states);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -38,7 +38,7 @@ Hotbar::Hotbar(Inventory &inventory) : m_inventory(inventory) {
|
|||||||
|
|
||||||
void Hotbar::update() {
|
void Hotbar::update() {
|
||||||
for (u16 i = 0 ; i < 9 ; ++i) {
|
for (u16 i = 0 ; i < 9 ; ++i) {
|
||||||
m_items[i].setItem(m_inventory.getStack(i, 0).item().id());
|
m_items[i].setStack(m_inventory.getStack(i, 0).item().id(), m_inventory.getStack(i, 0).amount());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ GameState::GameState() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (u16 i = 16 ; i < 25 ; ++i) {
|
for (u16 i = 16 ; i < 25 ; ++i) {
|
||||||
m_playerInventory.addStack(i);
|
m_playerInventory.addStack(i, 64);
|
||||||
}
|
}
|
||||||
|
|
||||||
initShaders();
|
initShaders();
|
||||||
|
BIN
textures/font.png
Normal file
BIN
textures/font.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.0 KiB |
Loading…
x
Reference in New Issue
Block a user