Add test Container Window, building out the items system.

This commit is contained in:
2025-03-18 19:26:22 -05:00
parent 809abae082
commit cc00350dc3
6 changed files with 94 additions and 1 deletions

View File

@@ -1,3 +1,14 @@
/// CaveGame - A procedural 2D platformer sandbox.
/// Created by Josh O'Leary @ Redacted Software, 2020-2025
/// Contact: josh@redacted.cc
/// Contributors: william@redacted.cc maxi@redacted.cc
/// This work is dedicated to the public domain.
/// @file ContainerWindow.hpp
/// @desc The UI window for a container that holds items.
/// @edit 3/18/2025
/// @auth Josh O'Leary
#pragma once
#include <JUI/Widgets/Window.hpp>
@@ -5,10 +16,34 @@
namespace CaveGame
{
using namespace JUI::UDimLiterals;
/// An inventory container window widget.
class ContainerWindow : public JUI::Window
class ContainerWindow : public JUI::Window
{
public:
ContainerWindow(Widget* parent, int rows, int cols, std::string title = "Container")
: JUI::Window(parent)
{
auto layout = this->ViewportInstance();
this->SetTitle(title);
this->Size(JUI::UDim2::FromPixels(48*rows, 48*cols));
this->MinSize(Vector2(48*rows, 48*cols));
this->SetResizable(false);
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
auto* cell = new JUI::Rect(layout);
cell->Name(std::format("cell_{},{}", row, col));
cell->Size({48_px, 48_px});
cell->Position(JUI::UDim2::FromPixels(48*row, 48*col));
}
}
}
void SetContainerSize(int rows, int cols);
void RebuildContainerElements();
protected:
private:
};

View File

@@ -23,6 +23,7 @@
#include <JUI/Widgets/TextRect.hpp>
#include <Core/Item.hpp>
#include "TileTool.hpp"
#include "ContainerWindow.hpp"
namespace CaveGame::Client {
using namespace Core;
@@ -112,6 +113,7 @@ namespace CaveGame::Client {
JUI::Scene* hud = nullptr;
Vector2 mouse_pos;
RNG tool_rng;
ContainerWindow* test_container = nullptr;
private:

View File

@@ -0,0 +1,6 @@
#include <Client/ContainerWindow.hpp>
namespace CaveGame::Client {
}

View File

@@ -37,6 +37,8 @@ void CaveGame::Client::GameSession::Load() {
for (int i = 0; i < steps; i++)
World()->DoTileTiccs(0.f);
};
test_container = new ContainerWindow(hud, 5, 5, "Test Container");
}
void CaveGame::Client::GameSession::Draw() {

View File

@@ -5,6 +5,20 @@
namespace CaveGame::Core
{
/// Implementation of items
/// 1. Need to create a TileItem associated with each tile, automatically.
/// 2. Not a strict requirement, but might like to be able to read item data from a data file.
/// This would remove the need to re-compile the entire program when items are changed.
/// 3. Each item in-game should be a single instance of the Item class.
/// 4. Items are represented via ItemStacks and ItemStack entities, which represent quantities of items
/// in a container, or dropped on the ground.
/// 5. Support Item Animated Sprites
/// 6. Support lots of unique item abilities and effects.
/// 7. Support attaching custom metadata to certain items.
/// This enum defines combinatorial category flags to items to enable smart sorting.
enum class ItemCategory: u8
{
@@ -48,11 +62,23 @@ namespace CaveGame::Core
class Item
{
public:
explicit Item(const std::string& name);
protected:
private:
};
class TileItem : public Item {
};
void RegisterItem(const std::string& name, Item* item);
Item* GetItem(const std::string& name);
class EmptyBottle : public Item
{
//REGISTER("EmptyBottle", Item);

22
Core/src/Core/Item.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include <Core/Item.hpp>
#include <string>
#include <map>
namespace CaveGame::Core {
std::map<std::string, Item*> item_registry;
void RegisterItem(const std::string& name, Item* item)
{
item_registry.insert({name, item});
}
Item* GetItem(const std::string& name)
{
return item_registry.at(name);
}
Item::Item(const std::string& name) {
RegisterItem(name, this);
}
}