mirror of
https://github.com/kierankihn/uno-game.git
synced 2025-12-27 02:13:18 +08:00
feat(ui): enhance GameUI with active game state updates and player actions
- Added callbacks for playing and drawing cards. - Integrated hand card and player list updates during active game stages. - Implemented discard pile, current color, and game direction display.
This commit is contained in:
@@ -29,6 +29,28 @@ namespace UNO::UI {
|
|||||||
this->callback_({CLIENT::PlayerActionType::START_GAME, payload});
|
this->callback_({CLIENT::PlayerActionType::START_GAME, payload});
|
||||||
this->window_->set_is_ready(true);
|
this->window_->set_is_ready(true);
|
||||||
});
|
});
|
||||||
|
window_->on_request_play_card([this](const auto &card_id, const auto &card_color) {
|
||||||
|
GAME::CardColor game_card_color = GAME::CardColor::RED;
|
||||||
|
switch (card_color) {
|
||||||
|
case CardColor::Red: game_card_color = GAME::CardColor::RED; break;
|
||||||
|
case CardColor::Green: game_card_color = GAME::CardColor::GREEN; break;
|
||||||
|
case CardColor::Blue: game_card_color = GAME::CardColor::BLUE; break;
|
||||||
|
case CardColor::Yellow: game_card_color = GAME::CardColor::YELLOW; break;
|
||||||
|
}
|
||||||
|
CLIENT::PlayerPlayCardPayload payload = {static_cast<size_t>(card_id), game_card_color};
|
||||||
|
this->callback_({CLIENT::PlayerActionType::PLAY_CARD, payload});
|
||||||
|
});
|
||||||
|
window_->on_request_draw_card([this]() {
|
||||||
|
CLIENT::PlayerDrawCardPayload payload;
|
||||||
|
this->callback_({CLIENT::PlayerActionType::DRAW_CARD, payload});
|
||||||
|
});
|
||||||
|
|
||||||
|
for (auto color : GAME::AllColors) {
|
||||||
|
for (auto type : GAME::AllTypes) {
|
||||||
|
this->images_[{color, type}] =
|
||||||
|
slint::Image::load_from_path(std::format("../assets/cards/{}.svg", GAME::Card{color, type}.toString()).data());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameUI::doUpdateUI(const std::shared_ptr<const GAME::ClientGameState> &clientGameState)
|
void GameUI::doUpdateUI(const std::shared_ptr<const GAME::ClientGameState> &clientGameState)
|
||||||
@@ -39,6 +61,54 @@ namespace UNO::UI {
|
|||||||
if (clientGameState->getClientGameStage() == GAME::ClientGameStage::PRE_GAME) {
|
if (clientGameState->getClientGameStage() == GAME::ClientGameStage::PRE_GAME) {
|
||||||
window_->set_active_page(PageType::StartPage);
|
window_->set_active_page(PageType::StartPage);
|
||||||
}
|
}
|
||||||
|
if (clientGameState->getClientGameStage() == GAME::ClientGameStage::ACTIVE
|
||||||
|
|| clientGameState->getClientGameStage() == GAME::ClientGameStage::IDLE) {
|
||||||
|
window_->set_active_page(PageType::GamePage);
|
||||||
|
|
||||||
|
auto players = clientGameState->getPlayers();
|
||||||
|
std::vector<OtherPlayer> other_player;
|
||||||
|
for (auto it = players.begin(); it != players.end(); it++) {
|
||||||
|
other_player.push_back({it->getName().data(),
|
||||||
|
static_cast<int>(it->getRemainingCardCount()),
|
||||||
|
it->getIsUno(),
|
||||||
|
it - players.begin() == clientGameState->getCurrentPlayerId()});
|
||||||
|
}
|
||||||
|
|
||||||
|
auto self = other_player.begin() + static_cast<int>(clientGameState->getSelfId());
|
||||||
|
std::ranges::rotate(other_player, self);
|
||||||
|
other_player.pop_back();
|
||||||
|
|
||||||
|
auto other_player_slint = std::make_shared<slint::VectorModel<OtherPlayer>>();
|
||||||
|
other_player_slint->set_vector(other_player);
|
||||||
|
|
||||||
|
auto cards = clientGameState->getCards();
|
||||||
|
|
||||||
|
window_->set_other_players(other_player_slint);
|
||||||
|
window_->set_current_player_name(clientGameState->getPlayerName().data());
|
||||||
|
window_->set_current_player_card_count(static_cast<int>(cards.size()));
|
||||||
|
window_->set_current_player_has_uno(false);
|
||||||
|
window_->set_is_current_player_turn(clientGameState->getClientGameStage() == GAME::ClientGameStage::ACTIVE);
|
||||||
|
|
||||||
|
auto hand_cards = std::make_shared<slint::VectorModel<HandCard>>();
|
||||||
|
size_t id = 0;
|
||||||
|
for (auto it = cards.begin(); it != cards.end(); it++, id++) {
|
||||||
|
hand_cards->push_back({images_[*it],
|
||||||
|
false,
|
||||||
|
static_cast<int>(id),
|
||||||
|
it->getType() == GAME::CardType::WILD || it->getType() == GAME::CardType::WILDDRAWFOUR,
|
||||||
|
it->canBePlayedOn(clientGameState->getDiscardPile().getFront(), clientGameState->getDrawCount())});
|
||||||
|
}
|
||||||
|
window_->set_hand_cards(hand_cards);
|
||||||
|
window_->set_discard_top_card(images_[clientGameState->getDiscardPile().getFront()]);
|
||||||
|
window_->set_game_direction(clientGameState->getIsReversed() ? GameDirection::Clockwise : GameDirection::CounterClockwise);
|
||||||
|
|
||||||
|
switch (clientGameState->getDiscardPile().getFront().getColor()) {
|
||||||
|
case GAME::CardColor::RED: window_->set_current_color(CardColor::Red); break;
|
||||||
|
case GAME::CardColor::BLUE: window_->set_current_color(CardColor::Blue); break;
|
||||||
|
case GAME::CardColor::GREEN: window_->set_current_color(CardColor::Green); break;
|
||||||
|
case GAME::CardColor::YELLOW: window_->set_current_color(CardColor::Yellow); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameUI::updateUI(const std::shared_ptr<const GAME::ClientGameState> &clientGameState)
|
void GameUI::updateUI(const std::shared_ptr<const GAME::ClientGameState> &clientGameState)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include "MainWindow.h"
|
#include "MainWindow.h"
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace UNO::UI {
|
namespace UNO::UI {
|
||||||
@@ -18,6 +19,8 @@ namespace UNO::UI {
|
|||||||
std::function<void(CLIENT::PlayerAction)> callback_;
|
std::function<void(CLIENT::PlayerAction)> callback_;
|
||||||
slint::ComponentHandle<MainWindow> window_;
|
slint::ComponentHandle<MainWindow> window_;
|
||||||
|
|
||||||
|
std::map<GAME::Card, slint::Image> images_;
|
||||||
|
|
||||||
void doUpdateUI(const std::shared_ptr<const GAME::ClientGameState> &clientGameState);
|
void doUpdateUI(const std::shared_ptr<const GAME::ClientGameState> &clientGameState);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
Reference in New Issue
Block a user