Skip to content

Commit

Permalink
Block selector: highlight and key controls
Browse files Browse the repository at this point in the history
  • Loading branch information
SmallJoker committed Feb 19, 2023
1 parent 02babe1 commit cdae68f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 11 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@
A 2D game block building inspired by Everybody Edits.
Code structure inspired by Minetest practices.

## Gameplay

**Hotkeys**

* W/A/S/D or arrow keys: move player
* Space: Jump
* Left click: Place the selected block
* Right click or Shift+Left click: Block eraser
* 1-9: Hotbar block selector
* `/` or T: Focus chat
* Enter: Send chat message
* E: Toggle block selector
* G: Toggle god mode
* M: Toggle minimap


## Available archives

### Linux
Expand Down
Binary file added assets/textures/block_selected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 70 additions & 4 deletions src/gui/blockselector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include "core/blockmanager.h"
#include <IGUIButton.h>
#include <IGUIEnvironment.h>
#include <IGUIImage.h>
#include <IGUITabControl.h>
#include <ITexture.h>
#include <IVideoDriver.h>

enum ElementId : int {
ID_HOTBAR_0 = 300, // Counted by button number
Expand Down Expand Up @@ -44,6 +46,11 @@ void SceneBlockSelector::draw()
rect += core::vector2di(5, 0);
m_showmore = m_gui->addButton(rect, nullptr, ID_SHOWMORE);
drawBlockSelector();

m_highlight = m_gui->addImage(rect);
// Texture size must match BTN_SIZE to display properly
m_highlight->setImage(m_gui->getVideoDriver()->getTexture("assets/textures/block_selected.png"));
selectBlockId(m_selected_bid, false);
}

void SceneBlockSelector::step(float dtime)
Expand Down Expand Up @@ -97,15 +104,13 @@ bool SceneBlockSelector::OnEvent(const SEvent &e)
if (e.EventType == EET_GUI_EVENT && e.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) {
int id = e.GUIEvent.Caller->getID();
// Select one from the hotbar
if (id >= ID_HOTBAR_0 && id < ID_HOTBAR_0 + (int)m_hotbar_ids.size()) {
m_selected_bid = m_hotbar_ids.at(id - ID_HOTBAR_0);

if (selectBlockId(id, true)) {
m_gui->setFocus(nullptr);
return true;
}
if (id >= ID_SELECTOR_0 && id < ID_SELECTOR_MAX) {
// Almost the same as with the hotbar
m_selected_bid = id - ID_SELECTOR_0;
selectBlockId(id - ID_SELECTOR_0, false);

m_gui->setFocus(nullptr);
return true;
Expand All @@ -117,6 +122,32 @@ bool SceneBlockSelector::OnEvent(const SEvent &e)
return true;
}
}
if (e.EventType == EET_KEY_INPUT_EVENT) {
if (!e.KeyInput.PressedDown)
return false;

switch (e.KeyInput.Key) {
case KEY_KEY_E:
toggleShowMore();
return true;
case KEY_KEY_1:
case KEY_KEY_2:
case KEY_KEY_3:
case KEY_KEY_4:
case KEY_KEY_5:
case KEY_KEY_6:
case KEY_KEY_7:
case KEY_KEY_8:
case KEY_KEY_9:
{
int id = e.KeyInput.Key - KEY_KEY_1 + ID_HOTBAR_0;
if (selectBlockId(id, true))
return true;
}
break;
default: break;
}
}
return false;
}

Expand Down Expand Up @@ -216,3 +247,38 @@ void SceneBlockSelector::drawBlockSelector()
}
}

bool SceneBlockSelector::selectBlockId(int what, bool is_element_id)
{
bid_t selected;
s32 hotbar_index = -1;
if (is_element_id) {
// Get block ID from element ID
if (what < ID_HOTBAR_0 || what >= ID_HOTBAR_0 + (int)m_hotbar_ids.size())
return false;

hotbar_index = what - ID_HOTBAR_0;
selected = m_hotbar_ids.at(hotbar_index);
} else {
// Try to get element ID based on block ID
selected = what;
for (size_t i = 0; i < m_hotbar_ids.size(); ++i) {
if (m_hotbar_ids[i] == selected) {
hotbar_index = i;
break;
}
}
}

if (hotbar_index >= 0) {
m_highlight->setVisible(true);
m_highlight->setRelativePosition(
m_hotbar_pos + core::vector2di(BTN_SIZE.Width * hotbar_index, 0)
);

} else {
m_highlight->setVisible(false);
}

m_selected_bid = selected;
return true;
}
3 changes: 3 additions & 0 deletions src/gui/blockselector.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace irr {
namespace gui {
class IGUIElement;
class IGUIEnvironment;
class IGUIImage;
}
}

Expand All @@ -29,6 +30,7 @@ class SceneBlockSelector : public IEventReceiver {
private:
bool drawBlockButton(bid_t bid, const core::recti &rect, gui::IGUIElement *parent, int id);
void drawBlockSelector();
bool selectBlockId(int what, bool is_element_id);

gui::IGUIEnvironment *m_gui = nullptr;

Expand All @@ -39,4 +41,5 @@ class SceneBlockSelector : public IEventReceiver {
bool m_show_selector = false;
bid_t m_dragged_bid = Block::ID_INVALID;
gui::IGUIElement *m_showmore = nullptr;
gui::IGUIImage *m_highlight = nullptr;
};
7 changes: 0 additions & 7 deletions src/gui/gameplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,6 @@ bool SceneGameplay::OnEvent(const SEvent &e)

// The Client performs physics of all players, including ours.
switch (keycode) {
case KEY_KEY_E:
if (down) {
if (m_blockselector)
m_blockselector->toggleShowMore();
return true;
}
break;
case KEY_KEY_M:
if (down) {
if (m_minimap)
Expand Down

0 comments on commit cdae68f

Please sign in to comment.