GUI for combat now works better and I can create sprites for things if I want.

master
Zed A. Shaw 3 weeks ago
parent 46de98e6f4
commit 49a71e257e
  1. 1
      assets/config.json
  2. 27
      combat_ui.cpp
  3. 2
      combat_ui.hpp
  4. 15
      ecs_gui.cpp
  5. 9
      ecs_gui.hpp
  6. 6
      gui.cpp
  7. 2
      raycaster.cpp
  8. 6
      tests/ecs_gui.cpp
  9. 4
      texture.cpp
  10. 1
      texture.hpp

@ -19,6 +19,7 @@
"cinqueda": "assets/cinqueda_1-256.png",
"left_gui": "assets/left_gui.png",
"blood_splatter": "assets/blood_splatter-256.png",
"trash_button": "assets/trash_button.png",
"hairy_spider": "assets/hairy_spider-256.png"
},
"enemy": {

@ -9,22 +9,31 @@ 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 | *%.(200,50)bar_hp | _ ]");
render();
"[ >.%(100,50)label_hp | *%.(100,50)bar_hp | _ ]");
}
void CombatUI::render() {
void CombatUI::render(TexturePack& textures) {
auto& world = $gui.world();
for(auto& [name, cell] : $gui.cells()) {
auto button = $gui.entity(name);
world.set<lel::Cell>(button, cell);
world.set<Rectangle>(button, {});
world.set<Clickable>(button, {100});
world.set<Textual>(button, {name});
if(name.starts_with("button_")) {
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, {name});
} else if(name.starts_with("bar_")) {
auto meter = $gui.entity(name);
world.set<lel::Cell>(meter, cell);
world.set<Rectangle>(meter, {});
world.set<Meter>(meter, {});
} else {
// ignored, it's just space
$gui.entity(name);
}
}
$gui.init();
$gui.init(textures);
}
void CombatUI::draw(sf::RenderWindow& window) {

@ -13,7 +13,7 @@ namespace gui {
CombatUI(GameLevel level);
void render();
void render(TexturePack& texture);
void draw(sf::RenderWindow& window);
void update_level(GameLevel &level) { $level = level; }
void click(int x, int y);

@ -20,7 +20,7 @@ DinkyECS::Entity GUECS::entity(std::string name) {
return entity;
}
void GUECS::init() {
void GUECS::init(TexturePack& textures) {
$world.query<lel::Cell, Rectangle>([](const auto &, auto& cell, auto& rect) {
rect.init(cell);
});
@ -28,6 +28,15 @@ void GUECS::init() {
$world.query<lel::Cell, Textual>([this](const auto &, auto& cell, auto& text) {
text.init(cell, $font);
});
$world.query<lel::Cell, Sprite>([&](const auto &, auto &cell, auto &sprite) {
auto sprite_texture = textures.get(sprite.name);
sprite.texture = sprite_texture.texture;
sprite.sprite = make_shared<sf::Sprite>(*sprite.texture);
sprite.sprite->setPosition({float(cell.x + 5), float(cell.y + 5)});
auto size = sprite.texture->getSize();
sprite.sprite->setScale({float(cell.w - 10) / size.x, float(cell.h - 10) / size.y});
});
}
void GUECS::render(sf::RenderWindow& window) {
@ -44,6 +53,10 @@ void GUECS::render(sf::RenderWindow& window) {
window.draw(*rect.shape);
});
$world.query<Sprite>([&](const auto &, const auto& sprite) {
window.draw(*sprite.sprite);
});
$world.query<Textual>([&](const auto &, const auto& text) {
window.draw(*text.text);
});

@ -5,6 +5,7 @@
#include <string>
#include <memory>
#include <SFML/Graphics.hpp>
#include "texture.hpp"
using std::shared_ptr, std::make_shared;
@ -28,6 +29,12 @@ struct Clickable {
int event = 0;
};
struct Sprite {
std::string name;
std::shared_ptr<sf::Sprite> sprite = nullptr;
std::shared_ptr<sf::Texture> texture = nullptr;
};
struct Rectangle {
shared_ptr<sf::RectangleShape> shape = nullptr;
@ -71,7 +78,7 @@ class GUECS {
return $world;
}
void init();
void init(TexturePack& textures);
void render(sf::RenderWindow& window);
void mouse(sf::RenderWindow &window);
};

@ -51,6 +51,8 @@ namespace gui {
$rayview.set_position(RAY_VIEW_X, RAY_VIEW_Y);
$rayview.position_camera($player.x + 0.5, $player.y + 0.5);
$combat_view.render($textures);
$status_view.create_render();
$status_view.log("Welcome to the game!");
@ -254,10 +256,6 @@ namespace gui {
case KEY::R:
$stats.reset();
break;
case KEY::G:
$combat_view.render();
event(Event::TICK);
break;
case KEY::M:
event(Event::MAP_OPEN);
break;

@ -353,7 +353,7 @@ void Raycaster::set_level(GameLevel level) {
// player doesn't need a sprite
if(player.entity == ent) return;
fmt::println("entity {} will have sprite named {}", ent, sprite.name);
auto sprite_txt = $textures.sprite_textures.at(sprite.name);
auto sprite_txt = $textures.get(sprite.name);
$sprites.try_emplace(ent, sprite_txt);
});
}

@ -2,10 +2,14 @@
#include <fmt/core.h>
#include "constants.hpp"
#include "ecs_gui.hpp"
#include "texture.hpp"
TEST_CASE("prototype one gui", "[ecs-gui]") {
GUECS gui;
TexturePack textures;
textures.load_sprites();
gui.position(0, 0, 1000, 500);
gui.layout("[test1|test2|test3][test4|_|test5]");
@ -18,7 +22,7 @@ TEST_CASE("prototype one gui", "[ecs-gui]") {
world.set<Textual>(button, {name});
}
gui.init();
gui.init(textures);
// at this point it's mostly ready but I'd need to render it to a window real quick
sf::RenderWindow window;

@ -64,3 +64,7 @@ matrix::Matrix TexturePack::convert_char_to_texture(matrix::Matrix &tile_ids) {
return result;
}
SpriteTexture TexturePack::get(std::string name) {
return sprite_textures.at(name);
}

@ -23,6 +23,7 @@ struct TexturePack {
void load_tiles();
void load_sprites();
SpriteTexture get(std::string name);
sf::Image load_image(std::string filename);
const uint32_t* get_surface(size_t num);

Loading…
Cancel
Save