Combat UI can now send events to the GUIwhen they click on buttons, and I've got a crappy wood texture for the buttons to test that.
This commit is contained in:
parent
722d55d948
commit
69a810b5a1
7 changed files with 38 additions and 20 deletions
BIN
assets/trash_button.png
Normal file
BIN
assets/trash_button.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 36 KiB |
|
@ -1,6 +1,7 @@
|
|||
#include "combat_ui.hpp"
|
||||
#include "constants.hpp"
|
||||
#include "color.hpp"
|
||||
#include "events.hpp"
|
||||
|
||||
namespace gui {
|
||||
using namespace guecs;
|
||||
|
@ -11,7 +12,7 @@ namespace gui {
|
|||
$gui.position(RAY_VIEW_X, RAY_VIEW_HEIGHT, RAY_VIEW_WIDTH, SCREEN_HEIGHT - RAY_VIEW_HEIGHT);
|
||||
$gui.layout(
|
||||
"[*%(100,150)button_attack1 | *%(100,150)button_attack2 | *%(100,150)button_attack3 | *%(100,150)button_heal]"
|
||||
"[ >.%(100,50)label_hp | *%.(190,50)bar_hp | _ ]");
|
||||
"[ >.%(100,50)label_hp | *%.(198,50)bar_hp | _ ]");
|
||||
}
|
||||
|
||||
void CombatUI::render(TexturePack& textures) {
|
||||
|
@ -22,8 +23,9 @@ namespace gui {
|
|||
auto button = $gui.entity(name);
|
||||
world.set<lel::Cell>(button, cell);
|
||||
world.set<Sprite>(button, {"trash_button"});
|
||||
world.set<Clickable>(button, {100});
|
||||
world.set<Textual>(button, {"Button"});
|
||||
world.set<Clickable>(button,
|
||||
guecs::make_action(*$level.world, Events::GUI::ATTACK));
|
||||
world.set<Textual>(button, {"Attack"});
|
||||
} else if(name.starts_with("bar_")) {
|
||||
$meter = $gui.entity(name);
|
||||
world.set<lel::Cell>($meter, cell);
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Events {
|
||||
enum GUI {
|
||||
START, COMBAT, LOOT, DEATH, STAIRS_UP, STAIRS_DOWN, TRAP,
|
||||
COMBAT_START, NO_NEIGHBORS
|
||||
COMBAT_START, NO_NEIGHBORS, ATTACK
|
||||
};
|
||||
|
||||
struct Combat {
|
||||
|
|
|
@ -77,10 +77,15 @@ namespace guecs {
|
|||
(pos.y >= cell.y && pos.y <= cell.y + cell.h))
|
||||
{
|
||||
auto& cn = $world.get<CellName>(ent);
|
||||
fmt::println("clicked on entity {} with name {} and event {}",
|
||||
ent, cn.name, clicked.event);
|
||||
clicked.action(ent, cn.name);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Clickable make_action(DinkyECS::World& target, Events::GUI event) {
|
||||
return {[&, event](auto ent, auto&){
|
||||
target.send<Events::GUI>(event, ent, {});
|
||||
}};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#include <memory>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "texture.hpp"
|
||||
#include <functional>
|
||||
#include "events.hpp"
|
||||
|
||||
namespace guecs {
|
||||
using std::shared_ptr, std::make_shared;
|
||||
|
@ -27,7 +29,7 @@ namespace guecs {
|
|||
};
|
||||
|
||||
struct Clickable {
|
||||
int event = 0;
|
||||
std::function<void(DinkyECS::Entity ent, std::string &name)> action;
|
||||
};
|
||||
|
||||
struct Sprite {
|
||||
|
@ -88,4 +90,6 @@ namespace guecs {
|
|||
void render(sf::RenderWindow& window);
|
||||
void mouse(sf::RenderWindow &window);
|
||||
};
|
||||
|
||||
Clickable make_action(DinkyECS::World& target, Events::GUI event);
|
||||
}
|
||||
|
|
32
gui.cpp
32
gui.cpp
|
@ -108,6 +108,9 @@ namespace gui {
|
|||
dbc::log("Exiting ATTACKING STATE");
|
||||
state(State::IDLE);
|
||||
break;
|
||||
case ATTACK:
|
||||
// ignore these since they're just from SFML not having discrete events
|
||||
break;
|
||||
default:
|
||||
dbc::log(fmt::format("In ATTACKING state, unhandled event {}", (int)ev));
|
||||
}
|
||||
|
@ -249,10 +252,6 @@ namespace gui {
|
|||
case KEY::A:
|
||||
event(Event::MOVE_LEFT);
|
||||
break;
|
||||
case KEY::Space:
|
||||
$rotation = 0;
|
||||
event(Event::ATTACK);
|
||||
break;
|
||||
case KEY::R:
|
||||
$stats.reset();
|
||||
break;
|
||||
|
@ -262,15 +261,8 @@ namespace gui {
|
|||
case KEY::Escape:
|
||||
event(Event::CLOSE);
|
||||
break;
|
||||
case KEY::P: {
|
||||
auto& debug = $level.world->get_the<Debug>();
|
||||
debug.FPS = !debug.FPS;
|
||||
debug.PATHS = !debug.PATHS;
|
||||
auto player = $level.world->get_the<Player>();
|
||||
auto& player_combat = $level.world->get<Combat>(player.entity);
|
||||
player_combat.hp = player_combat.max_hp;
|
||||
$combat_view.set_damage(float(player_combat.hp) / float(player_combat.max_hp));
|
||||
} break;
|
||||
case KEY::P:
|
||||
debug();
|
||||
default:
|
||||
break; // ignored
|
||||
}
|
||||
|
@ -278,6 +270,16 @@ namespace gui {
|
|||
}
|
||||
}
|
||||
|
||||
void FSM::debug() {
|
||||
auto& debug = $level.world->get_the<Debug>();
|
||||
debug.FPS = !debug.FPS;
|
||||
debug.PATHS = !debug.PATHS;
|
||||
auto player = $level.world->get_the<Player>();
|
||||
auto& player_combat = $level.world->get<Combat>(player.entity);
|
||||
player_combat.hp = player_combat.max_hp;
|
||||
$combat_view.set_damage(float(player_combat.hp) / float(player_combat.max_hp));
|
||||
}
|
||||
|
||||
void FSM::draw_weapon() {
|
||||
return;
|
||||
auto weapon = $rayview.$textures.sword.sprite;
|
||||
|
@ -426,6 +428,10 @@ namespace gui {
|
|||
$status_view.log("You picked up an item.");
|
||||
}
|
||||
break;
|
||||
case eGUI::ATTACK:
|
||||
$rotation = 0;
|
||||
event(Event::ATTACK);
|
||||
break;
|
||||
default:
|
||||
$status_view.log(fmt::format("INVALID EVENT! {},{}", evt, entity));
|
||||
}
|
||||
|
|
1
gui.hpp
1
gui.hpp
|
@ -81,6 +81,7 @@ namespace gui {
|
|||
void draw_stats();
|
||||
void draw_gui();
|
||||
void draw_blood();
|
||||
void debug();
|
||||
void render();
|
||||
void mouse();
|
||||
void generate_map();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue