Basic UI grid around the work computer.
This commit is contained in:
parent
56f36b33ee
commit
93e258cb1b
1 changed files with 89 additions and 36 deletions
125
main.cpp
125
main.cpp
|
@ -5,17 +5,18 @@
|
||||||
#include <fmt/xchar.h>
|
#include <fmt/xchar.h>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include "dbc.hpp"
|
||||||
|
|
||||||
constexpr const int SCREEN_WIDTH=1280;
|
constexpr const int SCREEN_WIDTH=1280;
|
||||||
constexpr const int SCREEN_HEIGHT=720;
|
constexpr const int SCREEN_HEIGHT=720;
|
||||||
constexpr const int FRAME_LIMIT=60;
|
constexpr const int FRAME_LIMIT=60;
|
||||||
constexpr const bool VSYNC=true;
|
constexpr const bool VSYNC=true;
|
||||||
bool GO_TO_WORK = false;
|
|
||||||
|
|
||||||
using std::string, std::wstring;
|
using std::string, std::wstring;
|
||||||
|
|
||||||
enum class Event {
|
enum class Event {
|
||||||
CLICKER, A_BUTTON
|
CLICKER, GIVE_TREAT, GIVE_FOOD, GIVE_WATER, GIVE_PETS, WORK
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Shake {
|
struct Shake {
|
||||||
|
@ -68,6 +69,27 @@ struct Shake {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct GameState {
|
||||||
|
bool at_work = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
static GameState GAME;
|
||||||
|
std::unordered_map<std::string, long> STATS {
|
||||||
|
{"Food", 0L},
|
||||||
|
{"Water", 0L},
|
||||||
|
{"Money", 100L},
|
||||||
|
{"Mood", 0L}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unordered_map<std::string, Event> EVENTS {
|
||||||
|
{"GiveTreat", Event::GIVE_TREAT},
|
||||||
|
{"GiveFood", Event::GIVE_FOOD},
|
||||||
|
{"GiveWater", Event::GIVE_WATER},
|
||||||
|
{"GivePets", Event::GIVE_PETS},
|
||||||
|
{"Work", Event::WORK}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
struct ClickerUI {
|
struct ClickerUI {
|
||||||
guecs::UI $gui;
|
guecs::UI $gui;
|
||||||
guecs::Entity $clicker;
|
guecs::Entity $clicker;
|
||||||
|
@ -75,11 +97,16 @@ struct ClickerUI {
|
||||||
ClickerUI() {
|
ClickerUI() {
|
||||||
$gui.position(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
$gui.position(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||||
$gui.layout(
|
$gui.layout(
|
||||||
"[FoodBowl|_|*%(300,400)clicker|_|_|_]"
|
"[Food|_|*%(300,400)clicker|_|_|_]"
|
||||||
"[WaterBowl|_|_ |_|_|_]"
|
"[Water|_|_ |_|_|_]"
|
||||||
"[Mood|_|_ |_|_|_]"
|
"[Mood|_|_ |_|_|_]"
|
||||||
"[Money|_|_ |_|_|_]"
|
"[Money|_|_ |_|_|_]"
|
||||||
"[Treat|Food|Water|Pet|Work]");
|
"[GiveTreat|GiveFood|GiveWater|GivePets|Work]");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::wstring make_stat_label(const std::string& name) {
|
||||||
|
return fmt::format(L"{}:\n{}", guecs::to_wstring(name),
|
||||||
|
STATS.at(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
|
@ -89,18 +116,16 @@ struct ClickerUI {
|
||||||
auto id = $gui.entity(name);
|
auto id = $gui.entity(name);
|
||||||
if(name != "clicker") {
|
if(name != "clicker") {
|
||||||
$gui.set<guecs::Rectangle>(id, {});
|
$gui.set<guecs::Rectangle>(id, {});
|
||||||
$gui.set<guecs::Effect>(id, {});
|
if(STATS.contains(name)) {
|
||||||
$gui.set<guecs::Label>(id, { guecs::to_wstring(name) });
|
$gui.set<guecs::Textual>(id, { make_stat_label(name) });
|
||||||
|
|
||||||
if(name == "Work") {
|
|
||||||
$gui.set<guecs::Clickable>(id, { [&](auto, auto) {
|
|
||||||
GO_TO_WORK = true;
|
|
||||||
fmt::println("Going to Work!");
|
|
||||||
} });
|
|
||||||
} else {
|
} else {
|
||||||
|
dbc::check(EVENTS.contains(name), fmt::format("INVALID EVENT {}, not in EVENTS map", name));
|
||||||
|
|
||||||
|
$gui.set<guecs::Effect>(id, {});
|
||||||
$gui.set<guecs::Clickable>(id, {
|
$gui.set<guecs::Clickable>(id, {
|
||||||
[&](auto, auto) { handle_button(Event::A_BUTTON); }
|
[&](auto, auto) { handle_button(EVENTS.at(name)); }
|
||||||
});
|
});
|
||||||
|
$gui.set<guecs::Label>(id, { guecs::to_wstring(name) });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -136,22 +161,52 @@ struct ClickerUI {
|
||||||
$gui.mouse(x, y, hover);
|
$gui.mouse(x, y, hover);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void update_stats() {
|
||||||
|
for(auto& [name, stat] : STATS) {
|
||||||
|
auto gui_id = $gui.entity(name);
|
||||||
|
auto& text = $gui.get<guecs::Textual>(gui_id);
|
||||||
|
text.update(make_stat_label(name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void handle_button(Event ev) {
|
void handle_button(Event ev) {
|
||||||
|
bool is_happy = false;
|
||||||
|
auto& shaker = $gui.get<Shake>($clicker);
|
||||||
|
auto& sprite = $gui.get<guecs::Sprite>($clicker);
|
||||||
|
|
||||||
using enum Event;
|
using enum Event;
|
||||||
switch(ev) {
|
switch(ev) {
|
||||||
case CLICKER: {
|
case CLICKER: // fallthrough
|
||||||
auto& shaker = $gui.get<Shake>($clicker);
|
case GIVE_PETS:
|
||||||
auto& sprite = $gui.get<guecs::Sprite>($clicker);
|
STATS["Mood"]++;
|
||||||
shaker.play(sprite);
|
is_happy = true;
|
||||||
fmt::println("CLICKER LOVES YOU!");
|
break;
|
||||||
} break;
|
case GIVE_TREAT:
|
||||||
case A_BUTTON:
|
STATS["Food"]++;
|
||||||
fmt::println("a button clicked");
|
STATS["Mood"]++;
|
||||||
break;
|
STATS["Money"]--;
|
||||||
|
is_happy = true;
|
||||||
|
break;
|
||||||
|
case GIVE_FOOD:
|
||||||
|
STATS["Food"]++;
|
||||||
|
STATS["Money"]--;
|
||||||
|
is_happy = true;
|
||||||
|
break;
|
||||||
|
case GIVE_WATER:
|
||||||
|
STATS["Water"]++;
|
||||||
|
STATS["Money"]--;
|
||||||
|
is_happy = true;
|
||||||
|
break;
|
||||||
|
case WORK:
|
||||||
|
GAME.at_work = true;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
assert(false && "invalid event");
|
assert(false && "invalid event");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
update_stats();
|
||||||
|
if(is_happy) shaker.play(sprite);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -161,26 +216,24 @@ struct WorkComputerUI {
|
||||||
|
|
||||||
WorkComputerUI() {
|
WorkComputerUI() {
|
||||||
$gui.position(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
$gui.position(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||||
$gui.layout("[computer]");
|
$gui.layout(
|
||||||
|
"[*%(100,200)clicker|a2 |a3|a4|a5 |*%(100,200)postit1]"
|
||||||
|
"[_ |a9 |*%(300,300)computer|_|_|_]"
|
||||||
|
"[a15 |a16|_ |_|_|*%(100,200)postit2]"
|
||||||
|
"[*%(100,200)coffee |a23|_ |_|_|_]"
|
||||||
|
"[_ |a50|a51|a52|a53|a54]");
|
||||||
}
|
}
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
guecs::Background bg{$gui.$parser};
|
guecs::Background bg{$gui.$parser};
|
||||||
bg.set_sprite("work_computer");
|
bg.set_sprite("work_computer");
|
||||||
$gui.set<guecs::Background>($gui.MAIN, bg);
|
$gui.set<guecs::Background>($gui.MAIN, bg);
|
||||||
|
|
||||||
auto computer = $gui.entity("computer");
|
|
||||||
$gui.set<guecs::Label>(computer, {L"Work Computer"});
|
|
||||||
$gui.set<guecs::Clickable>(computer, { [&](auto, auto) {
|
|
||||||
GO_TO_WORK = false;
|
|
||||||
fmt::println("Leaving Work!");
|
|
||||||
} });
|
|
||||||
|
|
||||||
$gui.init();
|
$gui.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void render(sf::RenderWindow& window) {
|
void render(sf::RenderWindow& window) {
|
||||||
$gui.render(window);
|
$gui.render(window);
|
||||||
|
$gui.debug_layout(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mouse(float x, float y, bool hover) {
|
void mouse(float x, float y, bool hover) {
|
||||||
|
@ -213,7 +266,7 @@ int main() {
|
||||||
if(const auto* mouse = event->getIf<sf::Event::MouseButtonPressed>()) {
|
if(const auto* mouse = event->getIf<sf::Event::MouseButtonPressed>()) {
|
||||||
if(mouse->button == sf::Mouse::Button::Left) {
|
if(mouse->button == sf::Mouse::Button::Left) {
|
||||||
sf::Vector2f pos = window.mapPixelToCoords(mouse->position);
|
sf::Vector2f pos = window.mapPixelToCoords(mouse->position);
|
||||||
if(GO_TO_WORK) {
|
if(GAME.at_work) {
|
||||||
computer.mouse(pos.x, pos.y, false);
|
computer.mouse(pos.x, pos.y, false);
|
||||||
} else {
|
} else {
|
||||||
clicker.mouse(pos.x, pos.y, false);
|
clicker.mouse(pos.x, pos.y, false);
|
||||||
|
@ -222,7 +275,7 @@ int main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(GO_TO_WORK) {
|
if(GAME.at_work) {
|
||||||
window.clear();
|
window.clear();
|
||||||
computer.render(window);
|
computer.render(window);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue