Clicker now moves when you click him.
This commit is contained in:
parent
9a4b0adc1f
commit
a9e219ea96
4 changed files with 100 additions and 25 deletions
|
@ -3,6 +3,7 @@
|
||||||
#include "guecs/ui.hpp"
|
#include "guecs/ui.hpp"
|
||||||
#include <fmt/xchar.h>
|
#include <fmt/xchar.h>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
constexpr const int WINDOW_WIDTH=1280;
|
constexpr const int WINDOW_WIDTH=1280;
|
||||||
constexpr const int WINDOW_HEIGHT=720;
|
constexpr const int WINDOW_HEIGHT=720;
|
||||||
|
@ -15,9 +16,59 @@ enum class Event {
|
||||||
CLICKER, A_BUTTON
|
CLICKER, A_BUTTON
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Shake {
|
||||||
|
float scale_factor = 0.05f;
|
||||||
|
int frames = 10;
|
||||||
|
float ease_rate = 0.1f;
|
||||||
|
bool playing = false;
|
||||||
|
int current = 0;
|
||||||
|
float x=0.0;
|
||||||
|
float y=0.0;
|
||||||
|
float w=0.0;
|
||||||
|
float h=0.0;
|
||||||
|
sf::Vector2f initial_scale;
|
||||||
|
|
||||||
|
float ease() {
|
||||||
|
float tick = float(frames) / float(current) * ease_rate;
|
||||||
|
return (std::sin(tick) + 1.0) / 2.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void init(lel::Cell& cell) {
|
||||||
|
x = cell.x;
|
||||||
|
y = cell.y;
|
||||||
|
w = cell.w;
|
||||||
|
h = cell.h;
|
||||||
|
}
|
||||||
|
|
||||||
|
void play(guecs::Sprite& sprite) {
|
||||||
|
if(!playing) {
|
||||||
|
playing = true;
|
||||||
|
current = 0;
|
||||||
|
initial_scale = sprite.sprite->getScale();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void render(guecs::Sprite& sprite) {
|
||||||
|
current++;
|
||||||
|
|
||||||
|
if(playing && current < frames) {
|
||||||
|
float tick = ease();
|
||||||
|
sf::Vector2f scale{
|
||||||
|
std::lerp(initial_scale.x, initial_scale.x + scale_factor, tick),
|
||||||
|
std::lerp(initial_scale.y, initial_scale.y + scale_factor, tick)};
|
||||||
|
|
||||||
|
sprite.sprite->setScale(scale);
|
||||||
|
} else {
|
||||||
|
playing = false;
|
||||||
|
current = 0;
|
||||||
|
sprite.sprite->setScale(initial_scale);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct ClickerUI {
|
struct ClickerUI {
|
||||||
guecs::UI $gui;
|
guecs::UI $gui;
|
||||||
|
guecs::Entity $clicker;
|
||||||
|
|
||||||
ClickerUI() {
|
ClickerUI() {
|
||||||
$gui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
|
$gui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||||
|
@ -30,7 +81,7 @@ struct ClickerUI {
|
||||||
}
|
}
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
$gui.set<guecs::Background>($gui.MAIN, {});
|
$gui.set<guecs::Background>($gui.MAIN, {$gui.$parser, {0, 0, 0, 255}});
|
||||||
|
|
||||||
for(auto& [name, cell] : $gui.cells()) {
|
for(auto& [name, cell] : $gui.cells()) {
|
||||||
auto id = $gui.entity(name);
|
auto id = $gui.entity(name);
|
||||||
|
@ -42,21 +93,32 @@ struct ClickerUI {
|
||||||
$gui.set<guecs::Clickable>(id, {
|
$gui.set<guecs::Clickable>(id, {
|
||||||
[&](auto, auto) { handle_button(Event::A_BUTTON); }
|
[&](auto, auto) { handle_button(Event::A_BUTTON); }
|
||||||
});
|
});
|
||||||
} else {
|
}
|
||||||
$gui.set<guecs::Sprite>(id, {"clicker_the_dog"});
|
}
|
||||||
$gui.set<guecs::Sound>(id, {"clicker_bark"});
|
|
||||||
$gui.set<guecs::Effect>(id, {0.1f, "ui_shader"});
|
$clicker = $gui.entity("clicker");
|
||||||
$gui.set<guecs::Clickable>(id, {
|
$gui.set<guecs::Sprite>($clicker, {"clicker_the_dog"});
|
||||||
|
$gui.set<guecs::Sound>($clicker, {"clicker_bark"});
|
||||||
|
$gui.set<guecs::Clickable>($clicker, {
|
||||||
[&](auto, auto) { handle_button(Event::CLICKER); }
|
[&](auto, auto) { handle_button(Event::CLICKER); }
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
// custom components need to be initialized manually
|
||||||
|
$gui.set_init<Shake>($clicker, {});
|
||||||
|
|
||||||
$gui.init();
|
$gui.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void render(sf::RenderWindow& window) {
|
void render(sf::RenderWindow& window) {
|
||||||
|
auto& shaker = $gui.get<Shake>($clicker);
|
||||||
|
if(shaker.playing) {
|
||||||
|
auto& sprite = $gui.get<guecs::Sprite>($clicker);
|
||||||
|
shaker.render(sprite);
|
||||||
|
window.clear();
|
||||||
|
}
|
||||||
|
|
||||||
$gui.render(window);
|
$gui.render(window);
|
||||||
|
|
||||||
// $gui.debug_layout(window);
|
// $gui.debug_layout(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,10 +129,12 @@ struct ClickerUI {
|
||||||
void handle_button(Event ev) {
|
void handle_button(Event ev) {
|
||||||
using enum Event;
|
using enum Event;
|
||||||
switch(ev) {
|
switch(ev) {
|
||||||
case CLICKER:
|
case CLICKER: {
|
||||||
|
auto& shaker = $gui.get<Shake>($clicker);
|
||||||
|
auto& sprite = $gui.get<guecs::Sprite>($clicker);
|
||||||
|
shaker.play(sprite);
|
||||||
fmt::println("CLICKER LOVES YOU!");
|
fmt::println("CLICKER LOVES YOU!");
|
||||||
break;
|
} break;
|
||||||
|
|
||||||
case A_BUTTON:
|
case A_BUTTON:
|
||||||
fmt::println("a button clicked");
|
fmt::println("a button clicked");
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -97,7 +97,7 @@ namespace guecs {
|
||||||
float y = 0.0f;
|
float y = 0.0f;
|
||||||
float w = 0.0f;
|
float w = 0.0f;
|
||||||
float h = 0.0f;
|
float h = 0.0f;
|
||||||
sf::Color color = THEME.BG_COLOR;
|
sf::Color color=THEME.BG_COLOR;
|
||||||
shared_ptr<sf::RectangleShape> shape = nullptr;
|
shared_ptr<sf::RectangleShape> shape = nullptr;
|
||||||
|
|
||||||
Background(lel::Parser& parser, sf::Color bg_color=THEME.BG_COLOR) :
|
Background(lel::Parser& parser, sf::Color bg_color=THEME.BG_COLOR) :
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "guecs/ui.hpp"
|
#include "guecs/ui.hpp"
|
||||||
#include "guecs/sfml/backend.hpp"
|
#include "guecs/sfml/backend.hpp"
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace guecs {
|
namespace guecs {
|
||||||
using std::make_shared;
|
using std::make_shared;
|
||||||
|
@ -88,10 +89,19 @@ namespace guecs {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Background::init() {
|
void Background::init() {
|
||||||
|
assert(w > 0.0f && "Background.w must be > 0.0f. Forgot $gui.$parser?");
|
||||||
|
assert(h > 0.0f && "Background.h must be > 0.0f. Forgot $gui.$parser?");
|
||||||
|
|
||||||
|
std::cout << "x=" << x << " y=" << y << " w=" << w << " h=" << h << std::endl;
|
||||||
sf::Vector2f size{float(w), float(h)};
|
sf::Vector2f size{float(w), float(h)};
|
||||||
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size);
|
if(shape == nullptr) {
|
||||||
|
shape = make_shared<sf::RectangleShape>(size);
|
||||||
|
} else {
|
||||||
|
shape->setSize(size);
|
||||||
|
}
|
||||||
shape->setPosition({float(x), float(y)});
|
shape->setPosition({float(x), float(y)});
|
||||||
shape->setFillColor(color);
|
shape->setFillColor(color);
|
||||||
|
assert(shape != nullptr && "failed to make rectangle");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Effect::init(lel::Cell &cell) {
|
void Effect::init(lel::Cell &cell) {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <typeinfo>
|
#include <typeinfo>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace guecs {
|
namespace guecs {
|
||||||
using std::make_shared;
|
using std::make_shared;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue