Skip to content

Commit

Permalink
Creative window added on key 'H'.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Feb 29, 2020
1 parent 5d7e515 commit 45c797c
Show file tree
Hide file tree
Showing 21 changed files with 302 additions and 83 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The long-term goal of this project is to provide a viable alternative to Minecra

- Movement: <kbd>W</kbd><kbd>A</kbd><kbd>S</kbd><kbd>D</kbd>
- Inventory: <kbd>E</kbd>
- Creative window: <kbd>H</kbd>
- Chat: <kbd>T</kbd>
- Jump: <kbd>Space</kbd>
- Sprint: <kbd>Ctrl</kbd>
Expand Down
10 changes: 8 additions & 2 deletions client/source/gui/InventoryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,19 @@ void InventoryWidget::onMouseEvent(const SDL_Event &event, MouseItemWidget &mous
}
}
else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT && m_currentItemWidget) {
mouseItemWidget.swapItems(*m_currentItemWidget, isReadOnly);
if (m_inventory && !m_inventory->isUnlimited())
mouseItemWidget.swapItems(*m_currentItemWidget, isReadOnly);
else if (m_inventory && mouseItemWidget.getStack().amount() == 0 && m_currentItemWidget->stack().amount() != 0)
mouseItemWidget.setStack(m_currentItemWidget->stack().item().stringID(), 64);

sendUpdatePacket();
}
else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_RIGHT && m_currentItemWidget) {
if (!isReadOnly) {
mouseItemWidget.putItem(*m_currentItemWidget);
if (m_inventory && !m_inventory->isUnlimited())
mouseItemWidget.putItem(*m_currentItemWidget);
else if (m_inventory && mouseItemWidget.getStack().amount() == 0 && m_currentItemWidget->stack().amount() != 0)
mouseItemWidget.setStack(m_currentItemWidget->stack().item().stringID(), 1);

sendUpdatePacket();
}
Expand Down
7 changes: 7 additions & 0 deletions client/source/network/ClientCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ void ClientCommandHandler::sendPlayerInventoryRequest() {
m_client.send(packet);
}

void ClientCommandHandler::sendPlayerCreativeWindowRequest() {
sf::Packet packet;
packet << Network::Command::PlayerCreativeWindow
<< u16(Config::screenWidth) << u16(Config::screenHeight) << u8(Config::guiScale);
m_client.send(packet);
}

void ClientCommandHandler::sendBlockActivated(const glm::ivec4 &selectedBlock) {
sf::Packet packet;
packet << Network::Command::BlockActivated
Expand Down
1 change: 1 addition & 0 deletions client/source/network/ClientCommandHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ClientCommandHandler {
void sendPlayerDigBlock(const glm::ivec4 &selectedBlock);
void sendPlayerPlaceBlock(s32 x, s32 y, s32 z, u32 block);
void sendPlayerInventoryRequest();
void sendPlayerCreativeWindowRequest();
void sendBlockActivated(const glm::ivec4 &selectedBlock);
void sendBlockInvUpdate(Inventory &inventory);
void sendChunkRequest(s32 chunkX, s32 chunkY, s32 chunkZ);
Expand Down
3 changes: 3 additions & 0 deletions client/source/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ void GameState::update() {
if (gk::GamePad::isKeyPressedOnce(GameKey::Inventory)) {
m_clientCommandHandler.sendPlayerInventoryRequest();
}
else if (gk::GamePad::isKeyPressedOnce(GameKey::CreativeWindow)) {
m_clientCommandHandler.sendPlayerCreativeWindowRequest();
}
}

m_player.updatePosition(m_world);
Expand Down
42 changes: 33 additions & 9 deletions client/source/states/LuaGUIState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,11 @@ void LuaGUIState::draw(gk::RenderTarget &target, gk::RenderStates states) const
void LuaGUIState::loadGUI(sf::Packet &packet) {
u8 type;
std::string name;
packet >> type >> name;

s32 x, y;
packet >> type >> name >> x >> y;
if (type != LuaWidget::Inventory)
packet >> x >> y;

if (type == LuaWidget::Image)
loadImage(name, x, y, packet);
Expand All @@ -145,6 +148,8 @@ void LuaGUIState::loadGUI(sf::Packet &packet) {
loadProgressBarWidget(name, x, y, packet);
else if (type == LuaWidget::CraftingWidget)
loadCraftingWidget(name, x, y, packet);
else if (type == LuaWidget::Inventory)
loadInventory(name, packet);
}

void LuaGUIState::loadImage(const std::string &, s32 x, s32 y, sf::Packet &packet) {
Expand All @@ -169,10 +174,10 @@ void LuaGUIState::loadTextButton(const std::string &, s32 x, s32 y, sf::Packet &
m_widgets.emplace_back(button);
}

void LuaGUIState::loadInventoryWidget(const std::string &, s32 x, s32 y, sf::Packet &packet) {
void LuaGUIState::loadInventoryWidget(const std::string &name, s32 x, s32 y, sf::Packet &packet) {
std::string inventory, playerName, inventory_name;
gk::Vector3i block;
float width, height;
u16 width, height;
u16 offset, count;
packet >> inventory >> playerName >> inventory_name
>> block.x >> block.y >> block.z
Expand All @@ -191,6 +196,19 @@ void LuaGUIState::loadInventoryWidget(const std::string &, s32 x, s32 y, sf::Pac

widgetInventory = &data->inventory;
}
else if (inventory == "temp") {
if (inventory_name.empty()) {
m_inventories.emplace("_temp", Inventory{width, height});
widgetInventory = &m_inventories.at("_temp");
}
else {
auto it = m_inventories.find(inventory_name);
if (it == m_inventories.end())
DEBUG("ERROR: Unable to find inventory '" + inventory_name + "' for widget '" + name + "'");

widgetInventory = &it->second;
}
}

if (widgetInventory) {
m_inventoryWidgets.emplace_back(m_client, &m_mainWidget);
Expand All @@ -200,7 +218,7 @@ void LuaGUIState::loadInventoryWidget(const std::string &, s32 x, s32 y, sf::Pac
inventoryWidget.init(*widgetInventory, offset, count);
}
else {
DEBUG("ERROR: Widget inventory is invalid");
DEBUG("ERROR: Inventory widget '" + name + "' is invalid");
}
}

Expand All @@ -216,14 +234,14 @@ void LuaGUIState::loadCraftingWidget(const std::string &, s32 x, s32 y, sf::Pack
BlockData *data = m_world.getBlockData(block.x, block.y, block.z);
if (!data) {
DEBUG("ERROR: No inventory found at", block.x, block.y, block.z);
return;
}

craftingInventory = &data->inventory;
else {
craftingInventory = &data->inventory;
}
}
else if (inventory == "temp") {
craftingInventory = &m_inventory;
m_inventory.resize(size, size);
m_inventories.emplace("_temp", Inventory{size, size});
craftingInventory = &m_inventories.at("_temp");
}

if (craftingInventory) {
Expand Down Expand Up @@ -264,3 +282,9 @@ void LuaGUIState::loadProgressBarWidget(const std::string &, s32 x, s32 y, sf::P
m_widgets.emplace_back(widget);
}

void LuaGUIState::loadInventory(const std::string &name, sf::Packet &packet) {
m_inventories.emplace(name, Inventory{});

packet >> m_inventories.at(name);
}

4 changes: 2 additions & 2 deletions client/source/states/LuaGUIState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class LuaGUIState : public InterfaceState {
void loadInventoryWidget(const std::string &name, s32 x, s32 y, sf::Packet &packet);
void loadCraftingWidget(const std::string &name, s32 x, s32 y, sf::Packet &packet);
void loadProgressBarWidget(const std::string &name, s32 x, s32 y, sf::Packet &packet);
void loadInventory(const std::string &name, sf::Packet &packet);

ClientCommandHandler &m_client;

Expand All @@ -68,8 +69,7 @@ class LuaGUIState : public InterfaceState {
std::deque<InventoryWidget> m_inventoryWidgets;
std::vector<std::unique_ptr<Widget>> m_widgets;
std::vector<std::unique_ptr<gk::Drawable>> m_drawables;

Inventory m_inventory;
std::unordered_map<std::string, Inventory> m_inventories;

ClientPlayer &m_player;
ClientWorld &m_world;
Expand Down
1 change: 1 addition & 0 deletions common/source/core/LuaWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace LuaWidget {
InventoryWidget = 2,
CraftingWidget = 3,
ProgressBarWidget = 4,
Inventory = 5,
};
}

Expand Down
2 changes: 2 additions & 0 deletions common/source/core/input/GameKey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ namespace GameKey {
Use,

Inventory,
CreativeWindow,

Chat,
Command,
};
Expand Down
16 changes: 12 additions & 4 deletions common/source/inventory/Inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ void Inventory::addStack(const std::string &stringID, u16 amount) {
}

void Inventory::serialize(sf::Packet &packet) const {
packet << m_width << m_height << u8(m_inBlock)
<< s32(m_blockPos.x) << s32(m_blockPos.y) << s32(m_blockPos.z);
packet << m_width << m_height << m_name << u8(m_inBlock)
<< s32(m_blockPos.x) << s32(m_blockPos.y) << s32(m_blockPos.z)
<< m_isUnlimited;

packet << u16(m_items.size());

int i = 0;
for (auto &it : m_items) {
Expand All @@ -62,19 +65,24 @@ void Inventory::serialize(sf::Packet &packet) const {
void Inventory::deserialize(sf::Packet &packet) {
u8 inBlock;
s32 bx, by, bz;
packet >> m_width >> m_height >> inBlock >> bx >> by >> bz;
packet >> m_width >> m_height >> m_name >> inBlock >> bx >> by >> bz >> m_isUnlimited;

m_inBlock = inBlock;
m_blockPos = gk::Vector3i{bx, by, bz};

if (m_items.size() != m_width * m_height)
m_items.resize(m_width * m_height);

u16 itemListSize, i = 0;
packet >> itemListSize;

std::string name;
u16 amount;
u8 x, y;
while (!packet.endOfPacket()) {
while (i < itemListSize) {
packet >> name >> amount >> x >> y;
setStack(x, y, name, amount);
++i;
}
}

12 changes: 10 additions & 2 deletions common/source/inventory/Inventory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
class Inventory : public ISerializable {
public:
Inventory() = default;
Inventory(u16 width, u16 height)
: m_width(width), m_height(height) { m_items.resize(width * height); }
Inventory(u16 width, u16 height, const std::string &name = "")
: m_name(name), m_width(width), m_height(height) { m_items.resize(width * height); }

const ItemStack &getStack(u16 x, u16 y) const { return m_items.at(x + y * m_width); }
ItemStack &getStackRef(u16 x, u16 y) { return m_items.at(x + y * m_width); }
Expand All @@ -48,6 +48,8 @@ class Inventory : public ISerializable {
void serialize(sf::Packet &packet) const override;
void deserialize(sf::Packet &packet) override;

const std::string &name() const { return m_name; }

u16 width() const { return m_width; }
u16 height() const { return m_height; }
void resize(u16 width, u16 height) { m_width = width; m_height = height; m_items.resize(width * height); }
Expand All @@ -63,7 +65,12 @@ class Inventory : public ISerializable {
bool hasChanged() const { return m_hasChanged; }
void setChanged(bool hasChanged) { m_hasChanged = hasChanged; }

bool isUnlimited() const { return m_isUnlimited; }
void setUnlimited(bool isUnlimited) { m_isUnlimited = isUnlimited; }

private:
std::string m_name;

u16 m_width = 0;
u16 m_height = 0;

Expand All @@ -73,6 +80,7 @@ class Inventory : public ISerializable {
gk::Vector3i m_blockPos{0, 0, 0};

bool m_hasChanged = false; // Used to send inventory update packets
bool m_isUnlimited = false;
};

#endif // INVENTORY_HPP_
41 changes: 21 additions & 20 deletions common/source/network/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,33 @@

std::string Network::commandToString(Network::Command command) {
std::map<Network::Command, std::string> commandNames = {
{Network::Command::ClientConnect, "ClientConnect"},
{Network::Command::ClientDisconnect, "ClientDisconnect"},
{Network::Command::ClientOk, "ClientOk"},
{Network::Command::ClientRefused, "ClientRefused"},
{Network::Command::ClientConnect, "ClientConnect"},
{Network::Command::ClientDisconnect, "ClientDisconnect"},
{Network::Command::ClientOk, "ClientOk"},
{Network::Command::ClientRefused, "ClientRefused"},

{Network::Command::KeyState, "KeyState"},
{Network::Command::KeyState, "KeyState"},

{Network::Command::ChunkData, "ChunkData"},
{Network::Command::ChunkRequest, "ChunkRequest"},
{Network::Command::ChunkData, "ChunkData"},
{Network::Command::ChunkRequest, "ChunkRequest"},

{Network::Command::PlayerPlaceBlock, "PlayerPlaceBlock"},
{Network::Command::PlayerDigBlock, "PlayerDigBlock"},
{Network::Command::PlayerInvUpdate, "PlayerInvUpdate"},
{Network::Command::PlayerPosUpdate, "PlayerPosUpdate"},
{Network::Command::PlayerSpawn, "PlayerSpawn"},
{Network::Command::PlayerInventory, "PlayerInventory"},
{Network::Command::PlayerPlaceBlock, "PlayerPlaceBlock"},
{Network::Command::PlayerDigBlock, "PlayerDigBlock"},
{Network::Command::PlayerInvUpdate, "PlayerInvUpdate"},
{Network::Command::PlayerPosUpdate, "PlayerPosUpdate"},
{Network::Command::PlayerSpawn, "PlayerSpawn"},
{Network::Command::PlayerInventory, "PlayerInventory"},
{Network::Command::PlayerCreativeWindow, "PlayerCreativeWindow"},

{Network::Command::BlockUpdate, "BlockUpdate"},
{Network::Command::BlockActivated, "BlockActivated"},
{Network::Command::BlockGUIData, "BlockGUIData"},
{Network::Command::BlockInvUpdate, "BlockInvUpdate"},
{Network::Command::BlockDataUpdate, "BlockDataUpdate"},
{Network::Command::BlockUpdate, "BlockUpdate"},
{Network::Command::BlockActivated, "BlockActivated"},
{Network::Command::BlockGUIData, "BlockGUIData"},
{Network::Command::BlockInvUpdate, "BlockInvUpdate"},
{Network::Command::BlockDataUpdate, "BlockDataUpdate"},

{Network::Command::RegistryData, "RegistryData"},
{Network::Command::RegistryData, "RegistryData"},

{Network::Command::ChatMessage, "ChatMessage"}
{Network::Command::ChatMessage, "ChatMessage"}
};
return commandNames[command];
}
Expand Down
41 changes: 21 additions & 20 deletions common/source/network/Network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,39 @@
namespace Network {
enum class Command {
// Client commands
ClientConnect = 0, // <TCP> [NetworkCommand][u16 udp port] (from Client only)
ClientDisconnect = 1, // <TCP> [NetworkCommand] (from Client only)
ClientOk = 2, // <TCP> [NetworkCommand][u16 client id] (from Server only)
ClientRefused = 3, // <TCP> [NetworkCommand] (from Server only)
ClientConnect = 0, // <TCP> [NetworkCommand][u16 udp port] (from Client only)
ClientDisconnect = 1, // <TCP> [NetworkCommand] (from Client only)
ClientOk = 2, // <TCP> [NetworkCommand][u16 client id] (from Server only)
ClientRefused = 3, // <TCP> [NetworkCommand] (from Server only)

// Input commands
KeyState = 4, // <UDP> [NetworkCommand][u32 timestamp][u16 client id][u32 keycode][bool isPressed]...
KeyState = 4, // <UDP> [NetworkCommand][u32 timestamp][u16 client id][u32 keycode][bool isPressed]...

// Chunk commands
ChunkData = 5, // <TCP> [NetworkCommand][s32 cx, cy, cz][u32...] (from Server only)
ChunkRequest = 6, // <TCP> [NetworkCommand][s32 cx, cy, cz] (from Client only)
ChunkData = 5, // <TCP> [NetworkCommand][s32 cx, cy, cz][u32...] (from Server only)
ChunkRequest = 6, // <TCP> [NetworkCommand][s32 cx, cy, cz] (from Client only)

// Player commands
PlayerPlaceBlock = 7, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Client only)
PlayerDigBlock = 8, // <TCP> [NetworkCommand][s32 x, y, z] (from Client only)
PlayerInvUpdate = 9, // <TCP> [NetworkCommand][u16 client id][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME]
PlayerPosUpdate = 10, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z][bool isTeleportation] (both) // FIXME
PlayerSpawn = 11, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z] (from Server only)
PlayerInventory = 12, // <TCP> [NetworkCommand][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
PlayerPlaceBlock = 7, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Client only)
PlayerDigBlock = 8, // <TCP> [NetworkCommand][s32 x, y, z] (from Client only)
PlayerInvUpdate = 9, // <TCP> [NetworkCommand][u16 client id][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME]
PlayerPosUpdate = 10, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z][bool isTeleportation] (both) // FIXME
PlayerSpawn = 11, // <TCP> [NetworkCommand][u16 client id][s32 x, y, z] (from Server only)
PlayerInventory = 12, // <TCP> [NetworkCommand][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
PlayerCreativeWindow = 13, // <TCP> [NetworkCommand][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)

// Block commands
BlockUpdate = 13, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Server only)
BlockActivated = 14, // <TCP> [NetworkCommand][s32 x, y, z][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
BlockGUIData = 15, // <TCP> [NetworkCommand][LuaGUIData data] (from Server only)
BlockInvUpdate = 16, // <TCP> [NetworkCommand][s32 x, y, z][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME]
BlockDataUpdate = 17, // <TCP> [NetworkCommand][s32 x, y, z][u64 data] (both) [FIXME]
BlockUpdate = 14, // <TCP> [NetworkCommand][s32 x, y, z][u32 block] (from Server only)
BlockActivated = 15, // <TCP> [NetworkCommand][s32 x, y, z][u16 screenWidth, screenHeight][u8 guiScale] (from Client only)
BlockGUIData = 16, // <TCP> [NetworkCommand][LuaGUIData data] (from Server only)
BlockInvUpdate = 17, // <TCP> [NetworkCommand][s32 x, y, z][[std::string item][u16 amount][u8 x, y]...] (both) [FIXME]
BlockDataUpdate = 18, // <TCP> [NetworkCommand][s32 x, y, z][u64 data] (both) [FIXME]

// Registry commands
RegistryData = 18, // <TCP> [NetworkCommand][Block block] (from Server only)
RegistryData = 19, // <TCP> [NetworkCommand][Block block] (from Server only)

// Chat commands
ChatMessage = 19, // <TCP> [NetworkCommand][u16 client id][std::string message] (both)
ChatMessage = 20, // <TCP> [NetworkCommand][u16 client id][std::string message] (both)
};

std::string commandToString(Command command);
Expand Down
Loading

0 comments on commit 45c797c

Please sign in to comment.