From e1492e9a62bf0d4ff1dce5b933ba05cad8de270d Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 5 May 2021 09:42:59 -0400 Subject: Solitaire: Place files in Solitaire namespace and rename main widget The purpose is to allow the Solitaire widget to be used in GML. The macro to register a widget requires a namespace, so this moves all files in the application to the Solitaire namespace. This also renames the SolitaireWidget class to Game - this is to avoid the redundancy / verbosity of typing "Solitaire::SolitaireWidget", and matches many other games in Serenity (Breakout, 2048, etc.). --- Userland/Games/Solitaire/CMakeLists.txt | 2 +- Userland/Games/Solitaire/Card.cpp | 4 + Userland/Games/Solitaire/Card.h | 4 + Userland/Games/Solitaire/CardStack.cpp | 4 + Userland/Games/Solitaire/CardStack.h | 4 + Userland/Games/Solitaire/Game.cpp | 418 +++++++++++++++++++++++++++ Userland/Games/Solitaire/Game.h | 125 ++++++++ Userland/Games/Solitaire/SolitaireWidget.cpp | 414 -------------------------- Userland/Games/Solitaire/SolitaireWidget.h | 121 -------- Userland/Games/Solitaire/main.cpp | 6 +- 10 files changed, 563 insertions(+), 539 deletions(-) create mode 100644 Userland/Games/Solitaire/Game.cpp create mode 100644 Userland/Games/Solitaire/Game.h delete mode 100644 Userland/Games/Solitaire/SolitaireWidget.cpp delete mode 100644 Userland/Games/Solitaire/SolitaireWidget.h (limited to 'Userland/Games/Solitaire') diff --git a/Userland/Games/Solitaire/CMakeLists.txt b/Userland/Games/Solitaire/CMakeLists.txt index 4a9d4e6c71..09ef04f528 100644 --- a/Userland/Games/Solitaire/CMakeLists.txt +++ b/Userland/Games/Solitaire/CMakeLists.txt @@ -1,8 +1,8 @@ set(SOURCES Card.cpp CardStack.cpp + Game.cpp main.cpp - SolitaireWidget.cpp ) serenity_app(Solitaire ICON app-solitaire) diff --git a/Userland/Games/Solitaire/Card.cpp b/Userland/Games/Solitaire/Card.cpp index b1a98ba6d7..40c4d4d072 100644 --- a/Userland/Games/Solitaire/Card.cpp +++ b/Userland/Games/Solitaire/Card.cpp @@ -9,6 +9,8 @@ #include #include +namespace Solitaire { + static const NonnullRefPtr s_diamond = Gfx::CharacterBitmap::create_from_ascii( " # " " ### " @@ -155,3 +157,5 @@ void Card::clear_and_draw(GUI::Painter& painter, const Color& background_color) draw(painter); save_old_position(); } + +} diff --git a/Userland/Games/Solitaire/Card.h b/Userland/Games/Solitaire/Card.h index 3cdd15513d..8a3c97cc47 100644 --- a/Userland/Games/Solitaire/Card.h +++ b/Userland/Games/Solitaire/Card.h @@ -13,6 +13,8 @@ #include #include +namespace Solitaire { + class Card final : public Core::Object { C_OBJECT(Card) public: @@ -63,3 +65,5 @@ private: bool m_moving { false }; bool m_upside_down { false }; }; + +} diff --git a/Userland/Games/Solitaire/CardStack.cpp b/Userland/Games/Solitaire/CardStack.cpp index d859678507..8d77476db0 100644 --- a/Userland/Games/Solitaire/CardStack.cpp +++ b/Userland/Games/Solitaire/CardStack.cpp @@ -6,6 +6,8 @@ #include "CardStack.h" +namespace Solitaire { + CardStack::CardStack() : m_position({ 0, 0 }) , m_type(Invalid) @@ -216,3 +218,5 @@ void CardStack::calculate_bounding_box() m_bounding_box.set_size(Card::width + width, Card::height + height); } + +} diff --git a/Userland/Games/Solitaire/CardStack.h b/Userland/Games/Solitaire/CardStack.h index 589da87443..d42fc5af18 100644 --- a/Userland/Games/Solitaire/CardStack.h +++ b/Userland/Games/Solitaire/CardStack.h @@ -9,6 +9,8 @@ #include "Card.h" #include +namespace Solitaire { + class CardStack final { public: enum Type { @@ -78,3 +80,5 @@ private: bool m_dirty { false }; Gfx::IntRect m_base; }; + +} diff --git a/Userland/Games/Solitaire/Game.cpp b/Userland/Games/Solitaire/Game.cpp new file mode 100644 index 0000000000..0a22602087 --- /dev/null +++ b/Userland/Games/Solitaire/Game.cpp @@ -0,0 +1,418 @@ +/* + * Copyright (c) 2020, Till Mayer + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "Game.h" +#include +#include + +namespace Solitaire { + +static const Color s_background_color { Color::from_rgb(0x008000) }; +static constexpr uint8_t new_game_animation_delay = 5; +static constexpr int s_timer_interval_ms = 1000 / 60; + +Game::Game(Function&& on_score_update) + : m_on_score_update(move(on_score_update)) +{ + set_fill_with_background_color(false); + + m_stacks[Stock] = CardStack({ 10, 10 }, CardStack::Type::Stock); + m_stacks[Waste] = CardStack({ 10 + Card::width + 10, 10 }, CardStack::Type::Waste); + m_stacks[Foundation4] = CardStack({ Game::width - Card::width - 10, 10 }, CardStack::Type::Foundation); + m_stacks[Foundation3] = CardStack({ Game::width - 2 * Card::width - 20, 10 }, CardStack::Type::Foundation); + m_stacks[Foundation2] = CardStack({ Game::width - 3 * Card::width - 30, 10 }, CardStack::Type::Foundation); + m_stacks[Foundation1] = CardStack({ Game::width - 4 * Card::width - 40, 10 }, CardStack::Type::Foundation); + m_stacks[Pile1] = CardStack({ 10, 10 + Card::height + 10 }, CardStack::Type::Normal); + m_stacks[Pile2] = CardStack({ 10 + Card::width + 10, 10 + Card::height + 10 }, CardStack::Type::Normal); + m_stacks[Pile3] = CardStack({ 10 + 2 * Card::width + 20, 10 + Card::height + 10 }, CardStack::Type::Normal); + m_stacks[Pile4] = CardStack({ 10 + 3 * Card::width + 30, 10 + Card::height + 10 }, CardStack::Type::Normal); + m_stacks[Pile5] = CardStack({ 10 + 4 * Card::width + 40, 10 + Card::height + 10 }, CardStack::Type::Normal); + m_stacks[Pile6] = CardStack({ 10 + 5 * Card::width + 50, 10 + Card::height + 10 }, CardStack::Type::Normal); + m_stacks[Pile7] = CardStack({ 10 + 6 * Card::width + 60, 10 + Card::height + 10 }, CardStack::Type::Normal); +} + +Game::~Game() +{ +} + +static float rand_float() +{ + return rand() / static_cast(RAND_MAX); +} + +void Game::timer_event(Core::TimerEvent&) +{ + if (m_game_over_animation) { + VERIFY(!m_animation.card().is_null()); + if (m_animation.card()->position().x() > Game::width || m_animation.card()->rect().right() < 0) + create_new_animation_card(); + + m_animation.tick(); + } + + if (m_has_to_repaint || m_game_over_animation || m_new_game_animation) { + m_repaint_all = false; + update(); + } +} + +void Game::create_new_animation_card() +{ + srand(time(nullptr)); + + auto card = Card::construct(static_cast(rand() % Card::Type::__Count), rand() % Card::card_count); + card->set_position({ rand() % (Game::width - Card::width), rand() % (Game::height / 8) }); + + int x_sgn = card->position().x() > (Game::width / 2) ? -1 : 1; + m_animation = Animation(card, rand_float() + .4f, x_sgn * ((rand() % 3) + 2), .6f + rand_float() * .4f); +} + +void Game::start_game_over_animation() +{ + if (m_game_over_animation) + return; + + create_new_animation_card(); + m_game_over_animation = true; +} + +void Game::stop_game_over_animation() +{ + if (!m_game_over_animation) + return; + + m_game_over_animation = false; + update(); +} + +void Game::setup() +{ + stop_game_over_animation(); + stop_timer(); + + for (auto& stack : m_stacks) + stack.clear(); + + m_new_deck.clear(); + m_new_game_animation_pile = 0; + m_score = 0; + update_score(0); + + for (int i = 0; i < Card::card_count; ++i) { + m_new_deck.append(Card::construct(Card::Type::Clubs, i)); + m_new_deck.append(Card::construct(Card::Type::Spades, i)); + m_new_deck.append(Card::construct(Card::Type::Hearts, i)); + m_new_deck.append(Card::construct(Card::Type::Diamonds, i)); + } + + srand(time(nullptr)); + for (uint8_t i = 0; i < 200; ++i) + m_new_deck.append(m_new_deck.take(rand() % m_new_deck.size())); + + m_new_game_animation = true; + start_timer(s_timer_interval_ms); + update(); +} + +void Game::update_score(int to_add) +{ + m_score = max(static_cast(m_score) + to_add, 0); + m_on_score_update(m_score); +} + +void Game::keydown_event(GUI::KeyEvent& event) +{ + if (m_new_game_animation || m_game_over_animation) + return; + + if (event.key() == KeyCode::Key_F12) + start_game_over_animation(); +} + +void Game::mousedown_event(GUI::MouseEvent& event) +{ + GUI::Widget::mousedown_event(event); + + if (m_new_game_animation || m_game_over_animation) + return; + + auto click_location = event.position(); + for (auto& to_check : m_stacks) { + if (to_check.bounding_box().contains(click_location)) { + if (to_check.type() == CardStack::Type::Stock) { + auto& waste = stack(Waste); + auto& stock = stack(Stock); + + if (stock.is_empty()) { + if (waste.is_empty()) + return; + + while (!waste.is_empty()) { + auto card = waste.pop(); + stock.push(card); + } + + stock.set_dirty(); + waste.set_dirty(); + m_has_to_repaint = true; + update_score(-100); + } else { + move_card(stock, waste); + } + } else if (!to_check.is_empty()) { + auto& top_card = to_check.peek(); + + if (top_card.is_upside_down()) { + if (top_card.rect().contains(click_location)) { + top_card.set_upside_down(false); + to_check.set_dirty(); + update_score(5); + m_has_to_repaint = true; + } + } else if (m_focused_cards.is_empty()) { + to_check.add_all_grabbed_cards(click_location, m_focused_cards); + m_mouse_down_location = click_location; + to_check.set_focused(true); + m_focused_stack = &to_check; + m_mouse_down = true; + } + } + break; + } + } +} + +void Game::mouseup_event(GUI::MouseEvent& event) +{ + GUI::Widget::mouseup_event(event); + + if (!m_focused_stack || m_focused_cards.is_empty() || m_game_over_animation || m_new_game_animation) + return; + + bool rebound = true; + for (auto& stack : m_stacks) { + if (stack.is_focused()) + continue; + + for (auto& focused_card : m_focused_cards) { + if (stack.bounding_box().intersects(focused_card.rect())) { + if (stack.is_allowed_to_push(m_focused_cards.at(0))) { + for (auto& to_intersect : m_focused_cards) { + mark_intersecting_stacks_dirty(to_intersect); + stack.push(to_intersect); + m_focused_stack->pop(); + } + + m_focused_stack->set_dirty(); + stack.set_dirty(); + + if (m_focused_stack->type() == CardStack::Type::Waste && stack.type() == CardStack::Type::Normal) { + update_score(5); + } else if (m_focused_stack->type() == CardStack::Type::Waste && stack.type() == CardStack::Type::Foundation) { + update_score(10); + } else if (m_focused_stack->type() == CardStack::Type::Normal && stack.type() == CardStack::Type::Foundation) { + update_score(10); + } else if (m_focused_stack->type() == CardStack::Type::Foundation && stack.type() == CardStack::Type::Normal) { + update_score(-15); + } + + rebound = false; + break; + } + } + } + } + + if (rebound) { + for (auto& to_intersect : m_focused_cards) + mark_intersecting_stacks_dirty(to_intersect); + + m_focused_stack->rebound_cards(); + m_focused_stack->set_dirty(); + } + + m_mouse_down = false; + m_has_to_repaint = true; +} + +void Game::mousemove_event(GUI::MouseEvent& event) +{ + GUI::Widget::mousemove_event(event); + + if (!m_mouse_down || m_game_over_animation || m_new_game_animation) + return; + + auto click_location = event.position(); + int dx = click_location.dx_relative_to(m_mouse_down_location); + int dy = click_location.dy_relative_to(m_mouse_down_location); + + for (auto& to_intersect : m_focused_cards) { + mark_intersecting_stacks_dirty(to_intersect); + to_intersect.rect().translate_by(dx, dy); + } + + m_mouse_down_location = click_location; + m_has_to_repaint = true; +} + +void Game::doubleclick_event(GUI::MouseEvent& event) +{ + GUI::Widget::doubleclick_event(event); + + if (m_game_over_animation) { + start_game_over_animation(); + setup(); + return; + } + + if (m_new_game_animation) + return; + + auto click_location = event.position(); + for (auto& to_check : m_stacks) { + if (to_check.type() == CardStack::Type::Foundation || to_check.type() == CardStack::Type::Stock) + continue; + + if (to_check.bounding_box().contains(click_location) && !to_check.is_empty()) { + auto& top_card = to_check.peek(); + if (!top_card.is_upside_down() && top_card.rect().contains(click_location)) { + if (stack(Foundation1).is_allowed_to_push(top_card)) + move_card(to_check, stack(Foundation1)); + else if (stack(Foundation2).is_allowed_to_push(top_card)) + move_card(to_check, stack(Foundation2)); + else if (stack(Foundation3).is_allowed_to_push(top_card)) + move_card(to_check, stack(Foundation3)); + else if (stack(Foundation4).is_allowed_to_push(top_card)) + move_card(to_check, stack(Foundation4)); + else + break; + + update_score(10); + } + break; + } + } + + m_has_to_repaint = true; +} + +void Game::check_for_game_over() +{ + for (auto& stack : m_stacks) { + if (stack.type() != CardStack::Type::Foundation) + continue; + if (stack.count() != Card::card_count) + return; + } + + start_game_over_animation(); +} + +void Game::move_card(CardStack& from, CardStack& to) +{ + auto card = from.pop(); + + card->set_moving(true); + m_focused_cards.clear(); + m_focused_cards.append(card); + mark_intersecting_stacks_dirty(card); + to.push(card); + + from.set_dirty(); + to.set_dirty(); + + m_has_to_repaint = true; +} + +void Game::mark_intersecting_stacks_dirty(Card& intersecting_card) +{ + for (auto& stack : m_stacks) { + if (intersecting_card.rect().intersects(stack.bounding_box())) { + stack.set_dirty(); + m_has_to_repaint = true; + } + } +} + +void Game::paint_event(GUI::PaintEvent& event) +{ + GUI::Widget::paint_event(event); + + m_has_to_repaint = false; + if (m_game_over_animation && m_repaint_all) + return; + + GUI::Painter painter(*this); + + if (m_repaint_all) { + painter.fill_rect(event.rect(), s_background_color); + + for (auto& stack : m_stacks) + stack.draw(painter, s_background_color); + } else if (m_game_over_animation && !m_animation.card().is_null()) { + m_animation.card()->draw(painter); + } else if (m_new_game_animation) { + if (m_new_game_animation_delay < new_game_animation_delay) { + ++m_new_game_animation_delay; + } else { + m_new_game_animation_delay = 0; + auto& current_pile = stack(piles.at(m_new_game_animation_pile)); + + if (current_pile.count() < m_new_game_animation_pile) { + auto card = m_new_deck.take_last(); + card->set_upside_down(true); + current_pile.push(card); + } else { + current_pile.push(m_new_deck.take_last()); + ++m_new_game_animation_pile; + } + current_pile.set_dirty(); + + if (m_new_game_animation_pile == piles.size()) { + while (!m_new_deck.is_empty()) + stack(Stock).push(m_new_deck.take_last()); + stack(Stock).set_dirty(); + m_new_game_animation = false; + } + } + } + + if (!m_game_over_animation && !m_repaint_all) { + if (!m_focused_cards.is_empty()) { + for (auto& focused_card : m_focused_cards) + focused_card.clear(painter, s_background_color); + } + + for (auto& stack : m_stacks) { + if (stack.is_dirty()) + stack.draw(painter, s_background_color); + } + + if (!m_focused_cards.is_empty()) { + for (auto& focused_card : m_focused_cards) { + focused_card.draw(painter); + focused_card.save_old_position(); + } + } + } + + m_repaint_all = true; + if (!m_mouse_down) { + if (!m_focused_cards.is_empty()) { + check_for_game_over(); + for (auto& card : m_focused_cards) + card.set_moving(false); + m_focused_cards.clear(); + } + + if (m_focused_stack) { + m_focused_stack->set_focused(false); + m_focused_stack = nullptr; + } + } +} + +} diff --git a/Userland/Games/Solitaire/Game.h b/Userland/Games/Solitaire/Game.h new file mode 100644 index 0000000000..18e6d3d875 --- /dev/null +++ b/Userland/Games/Solitaire/Game.h @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2020, Till Mayer + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include "CardStack.h" +#include +#include + +namespace Solitaire { + +class Game final : public GUI::Widget { + C_OBJECT(Game) +public: + static constexpr int width = 640; + static constexpr int height = 480; + + virtual ~Game() override; + void setup(); + +private: + Game(Function&& on_score_update); + + class Animation { + public: + Animation() + { + } + + Animation(RefPtr animation_card, float gravity, int x_vel, float bouncyness) + : m_animation_card(animation_card) + , m_gravity(gravity) + , m_x_velocity(x_vel) + , m_bouncyness(bouncyness) + { + } + + RefPtr card() { return m_animation_card; } + + void tick() + { + VERIFY(!m_animation_card.is_null()); + m_y_velocity += m_gravity; + + if (m_animation_card->position().y() + Card::height + m_y_velocity > Game::height + 1 && m_y_velocity > 0) { + m_y_velocity = min((m_y_velocity * -m_bouncyness), -8.f); + m_animation_card->rect().set_y(Game::height - Card::height); + m_animation_card->rect().translate_by(m_x_velocity, 0); + } else { + m_animation_card->rect().translate_by(m_x_velocity, m_y_velocity); + } + } + + private: + RefPtr m_animation_card; + float m_gravity { 0 }; + int m_x_velocity { 0 }; + float m_y_velocity { 0 }; + float m_bouncyness { 0 }; + }; + + enum StackLocation { + Stock, + Waste, + Foundation1, + Foundation2, + Foundation3, + Foundation4, + Pile1, + Pile2, + Pile3, + Pile4, + Pile5, + Pile6, + Pile7, + __Count + }; + static constexpr Array piles = { Pile1, Pile2, Pile3, Pile4, Pile5, Pile6, Pile7 }; + + void mark_intersecting_stacks_dirty(Card& intersecting_card); + void update_score(int to_add); + void move_card(CardStack& from, CardStack& to); + void start_game_over_animation(); + void stop_game_over_animation(); + void create_new_animation_card(); + void check_for_game_over(); + + ALWAYS_INLINE CardStack& stack(StackLocation location) + { + return m_stacks[location]; + } + + virtual void paint_event(GUI::PaintEvent&) override; + virtual void mousedown_event(GUI::MouseEvent&) override; + virtual void mouseup_event(GUI::MouseEvent&) override; + virtual void mousemove_event(GUI::MouseEvent&) override; + virtual void doubleclick_event(GUI::MouseEvent&) override; + virtual void keydown_event(GUI::KeyEvent&) override; + virtual void timer_event(Core::TimerEvent&) override; + + NonnullRefPtrVector m_focused_cards; + NonnullRefPtrVector m_new_deck; + CardStack m_stacks[StackLocation::__Count]; + CardStack* m_focused_stack { nullptr }; + Gfx::IntPoint m_mouse_down_location; + + bool m_mouse_down { false }; + bool m_repaint_all { true }; + bool m_has_to_repaint { true }; + + Animation m_animation; + bool m_game_over_animation { false }; + + bool m_new_game_animation { false }; + uint8_t m_new_game_animation_pile { 0 }; + uint8_t m_new_game_animation_delay { 0 }; + + uint32_t m_score { 0 }; + Function m_on_score_update; +}; + +} diff --git a/Userland/Games/Solitaire/SolitaireWidget.cpp b/Userland/Games/Solitaire/SolitaireWidget.cpp deleted file mode 100644 index 19df773abf..0000000000 --- a/Userland/Games/Solitaire/SolitaireWidget.cpp +++ /dev/null @@ -1,414 +0,0 @@ -/* - * Copyright (c) 2020, Till Mayer - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include "SolitaireWidget.h" -#include -#include - -static const Color s_background_color { Color::from_rgb(0x008000) }; -static constexpr uint8_t new_game_animation_delay = 5; -static constexpr int s_timer_interval_ms = 1000 / 60; - -SolitaireWidget::SolitaireWidget(Function&& on_score_update) - : m_on_score_update(move(on_score_update)) -{ - set_fill_with_background_color(false); - - m_stacks[Stock] = CardStack({ 10, 10 }, CardStack::Type::Stock); - m_stacks[Waste] = CardStack({ 10 + Card::width + 10, 10 }, CardStack::Type::Waste); - m_stacks[Foundation4] = CardStack({ SolitaireWidget::width - Card::width - 10, 10 }, CardStack::Type::Foundation); - m_stacks[Foundation3] = CardStack({ SolitaireWidget::width - 2 * Card::width - 20, 10 }, CardStack::Type::Foundation); - m_stacks[Foundation2] = CardStack({ SolitaireWidget::width - 3 * Card::width - 30, 10 }, CardStack::Type::Foundation); - m_stacks[Foundation1] = CardStack({ SolitaireWidget::width - 4 * Card::width - 40, 10 }, CardStack::Type::Foundation); - m_stacks[Pile1] = CardStack({ 10, 10 + Card::height + 10 }, CardStack::Type::Normal); - m_stacks[Pile2] = CardStack({ 10 + Card::width + 10, 10 + Card::height + 10 }, CardStack::Type::Normal); - m_stacks[Pile3] = CardStack({ 10 + 2 * Card::width + 20, 10 + Card::height + 10 }, CardStack::Type::Normal); - m_stacks[Pile4] = CardStack({ 10 + 3 * Card::width + 30, 10 + Card::height + 10 }, CardStack::Type::Normal); - m_stacks[Pile5] = CardStack({ 10 + 4 * Card::width + 40, 10 + Card::height + 10 }, CardStack::Type::Normal); - m_stacks[Pile6] = CardStack({ 10 + 5 * Card::width + 50, 10 + Card::height + 10 }, CardStack::Type::Normal); - m_stacks[Pile7] = CardStack({ 10 + 6 * Card::width + 60, 10 + Card::height + 10 }, CardStack::Type::Normal); -} - -SolitaireWidget::~SolitaireWidget() -{ -} - -static float rand_float() -{ - return rand() / static_cast(RAND_MAX); -} - -void SolitaireWidget::timer_event(Core::TimerEvent&) -{ - if (m_game_over_animation) { - VERIFY(!m_animation.card().is_null()); - if (m_animation.card()->position().x() > SolitaireWidget::width || m_animation.card()->rect().right() < 0) - create_new_animation_card(); - - m_animation.tick(); - } - - if (m_has_to_repaint || m_game_over_animation || m_new_game_animation) { - m_repaint_all = false; - update(); - } -} - -void SolitaireWidget::create_new_animation_card() -{ - srand(time(nullptr)); - - auto card = Card::construct(static_cast(rand() % Card::Type::__Count), rand() % Card::card_count); - card->set_position({ rand() % (SolitaireWidget::width - Card::width), rand() % (SolitaireWidget::height / 8) }); - - int x_sgn = card->position().x() > (SolitaireWidget::width / 2) ? -1 : 1; - m_animation = Animation(card, rand_float() + .4f, x_sgn * ((rand() % 3) + 2), .6f + rand_float() * .4f); -} - -void SolitaireWidget::start_game_over_animation() -{ - if (m_game_over_animation) - return; - - create_new_animation_card(); - m_game_over_animation = true; -} - -void SolitaireWidget::stop_game_over_animation() -{ - if (!m_game_over_animation) - return; - - m_game_over_animation = false; - update(); -} - -void SolitaireWidget::setup() -{ - stop_game_over_animation(); - stop_timer(); - - for (auto& stack : m_stacks) - stack.clear(); - - m_new_deck.clear(); - m_new_game_animation_pile = 0; - m_score = 0; - update_score(0); - - for (int i = 0; i < Card::card_count; ++i) { - m_new_deck.append(Card::construct(Card::Type::Clubs, i)); - m_new_deck.append(Card::construct(Card::Type::Spades, i)); - m_new_deck.append(Card::construct(Card::Type::Hearts, i)); - m_new_deck.append(Card::construct(Card::Type::Diamonds, i)); - } - - srand(time(nullptr)); - for (uint8_t i = 0; i < 200; ++i) - m_new_deck.append(m_new_deck.take(rand() % m_new_deck.size())); - - m_new_game_animation = true; - start_timer(s_timer_interval_ms); - update(); -} - -void SolitaireWidget::update_score(int to_add) -{ - m_score = max(static_cast(m_score) + to_add, 0); - m_on_score_update(m_score); -} - -void SolitaireWidget::keydown_event(GUI::KeyEvent& event) -{ - if (m_new_game_animation || m_game_over_animation) - return; - - if (event.key() == KeyCode::Key_F12) - start_game_over_animation(); -} - -void SolitaireWidget::mousedown_event(GUI::MouseEvent& event) -{ - GUI::Widget::mousedown_event(event); - - if (m_new_game_animation || m_game_over_animation) - return; - - auto click_location = event.position(); - for (auto& to_check : m_stacks) { - if (to_check.bounding_box().contains(click_location)) { - if (to_check.type() == CardStack::Type::Stock) { - auto& waste = stack(Waste); - auto& stock = stack(Stock); - - if (stock.is_empty()) { - if (waste.is_empty()) - return; - - while (!waste.is_empty()) { - auto card = waste.pop(); - stock.push(card); - } - - stock.set_dirty(); - waste.set_dirty(); - m_has_to_repaint = true; - update_score(-100); - } else { - move_card(stock, waste); - } - } else if (!to_check.is_empty()) { - auto& top_card = to_check.peek(); - - if (top_card.is_upside_down()) { - if (top_card.rect().contains(click_location)) { - top_card.set_upside_down(false); - to_check.set_dirty(); - update_score(5); - m_has_to_repaint = true; - } - } else if (m_focused_cards.is_empty()) { - to_check.add_all_grabbed_cards(click_location, m_focused_cards); - m_mouse_down_location = click_location; - to_check.set_focused(true); - m_focused_stack = &to_check; - m_mouse_down = true; - } - } - break; - } - } -} - -void SolitaireWidget::mouseup_event(GUI::MouseEvent& event) -{ - GUI::Widget::mouseup_event(event); - - if (!m_focused_stack || m_focused_cards.is_empty() || m_game_over_animation || m_new_game_animation) - return; - - bool rebound = true; - for (auto& stack : m_stacks) { - if (stack.is_focused()) - continue; - - for (auto& focused_card : m_focused_cards) { - if (stack.bounding_box().intersects(focused_card.rect())) { - if (stack.is_allowed_to_push(m_focused_cards.at(0))) { - for (auto& to_intersect : m_focused_cards) { - mark_intersecting_stacks_dirty(to_intersect); - stack.push(to_intersect); - m_focused_stack->pop(); - } - - m_focused_stack->set_dirty(); - stack.set_dirty(); - - if (m_focused_stack->type() == CardStack::Type::Waste && stack.type() == CardStack::Type::Normal) { - update_score(5); - } else if (m_focused_stack->type() == CardStack::Type::Waste && stack.type() == CardStack::Type::Foundation) { - update_score(10); - } else if (m_focused_stack->type() == CardStack::Type::Normal && stack.type() == CardStack::Type::Foundation) { - update_score(10); - } else if (m_focused_stack->type() == CardStack::Type::Foundation && stack.type() == CardStack::Type::Normal) { - update_score(-15); - } - - rebound = false; - break; - } - } - } - } - - if (rebound) { - for (auto& to_intersect : m_focused_cards) - mark_intersecting_stacks_dirty(to_intersect); - - m_focused_stack->rebound_cards(); - m_focused_stack->set_dirty(); - } - - m_mouse_down = false; - m_has_to_repaint = true; -} - -void SolitaireWidget::mousemove_event(GUI::MouseEvent& event) -{ - GUI::Widget::mousemove_event(event); - - if (!m_mouse_down || m_game_over_animation || m_new_game_animation) - return; - - auto click_location = event.position(); - int dx = click_location.dx_relative_to(m_mouse_down_location); - int dy = click_location.dy_relative_to(m_mouse_down_location); - - for (auto& to_intersect : m_focused_cards) { - mark_intersecting_stacks_dirty(to_intersect); - to_intersect.rect().translate_by(dx, dy); - } - - m_mouse_down_location = click_location; - m_has_to_repaint = true; -} - -void SolitaireWidget::doubleclick_event(GUI::MouseEvent& event) -{ - GUI::Widget::doubleclick_event(event); - - if (m_game_over_animation) { - start_game_over_animation(); - setup(); - return; - } - - if (m_new_game_animation) - return; - - auto click_location = event.position(); - for (auto& to_check : m_stacks) { - if (to_check.type() == CardStack::Type::Foundation || to_check.type() == CardStack::Type::Stock) - continue; - - if (to_check.bounding_box().contains(click_location) && !to_check.is_empty()) { - auto& top_card = to_check.peek(); - if (!top_card.is_upside_down() && top_card.rect().contains(click_location)) { - if (stack(Foundation1).is_allowed_to_push(top_card)) - move_card(to_check, stack(Foundation1)); - else if (stack(Foundation2).is_allowed_to_push(top_card)) - move_card(to_check, stack(Foundation2)); - else if (stack(Foundation3).is_allowed_to_push(top_card)) - move_card(to_check, stack(Foundation3)); - else if (stack(Foundation4).is_allowed_to_push(top_card)) - move_card(to_check, stack(Foundation4)); - else - break; - - update_score(10); - } - break; - } - } - - m_has_to_repaint = true; -} - -void SolitaireWidget::check_for_game_over() -{ - for (auto& stack : m_stacks) { - if (stack.type() != CardStack::Type::Foundation) - continue; - if (stack.count() != Card::card_count) - return; - } - - start_game_over_animation(); -} - -void SolitaireWidget::move_card(CardStack& from, CardStack& to) -{ - auto card = from.pop(); - - card->set_moving(true); - m_focused_cards.clear(); - m_focused_cards.append(card); - mark_intersecting_stacks_dirty(card); - to.push(card); - - from.set_dirty(); - to.set_dirty(); - - m_has_to_repaint = true; -} - -void SolitaireWidget::mark_intersecting_stacks_dirty(Card& intersecting_card) -{ - for (auto& stack : m_stacks) { - if (intersecting_card.rect().intersects(stack.bounding_box())) { - stack.set_dirty(); - m_has_to_repaint = true; - } - } -} - -void SolitaireWidget::paint_event(GUI::PaintEvent& event) -{ - GUI::Widget::paint_event(event); - - m_has_to_repaint = false; - if (m_game_over_animation && m_repaint_all) - return; - - GUI::Painter painter(*this); - - if (m_repaint_all) { - painter.fill_rect(event.rect(), s_background_color); - - for (auto& stack : m_stacks) - stack.draw(painter, s_background_color); - } else if (m_game_over_animation && !m_animation.card().is_null()) { - m_animation.card()->draw(painter); - } else if (m_new_game_animation) { - if (m_new_game_animation_delay < new_game_animation_delay) { - ++m_new_game_animation_delay; - } else { - m_new_game_animation_delay = 0; - auto& current_pile = stack(piles.at(m_new_game_animation_pile)); - - if (current_pile.count() < m_new_game_animation_pile) { - auto card = m_new_deck.take_last(); - card->set_upside_down(true); - current_pile.push(card); - } else { - current_pile.push(m_new_deck.take_last()); - ++m_new_game_animation_pile; - } - current_pile.set_dirty(); - - if (m_new_game_animation_pile == piles.size()) { - while (!m_new_deck.is_empty()) - stack(Stock).push(m_new_deck.take_last()); - stack(Stock).set_dirty(); - m_new_game_animation = false; - } - } - } - - if (!m_game_over_animation && !m_repaint_all) { - if (!m_focused_cards.is_empty()) { - for (auto& focused_card : m_focused_cards) - focused_card.clear(painter, s_background_color); - } - - for (auto& stack : m_stacks) { - if (stack.is_dirty()) - stack.draw(painter, s_background_color); - } - - if (!m_focused_cards.is_empty()) { - for (auto& focused_card : m_focused_cards) { - focused_card.draw(painter); - focused_card.save_old_position(); - } - } - } - - m_repaint_all = true; - if (!m_mouse_down) { - if (!m_focused_cards.is_empty()) { - check_for_game_over(); - for (auto& card : m_focused_cards) - card.set_moving(false); - m_focused_cards.clear(); - } - - if (m_focused_stack) { - m_focused_stack->set_focused(false); - m_focused_stack = nullptr; - } - } -} diff --git a/Userland/Games/Solitaire/SolitaireWidget.h b/Userland/Games/Solitaire/SolitaireWidget.h deleted file mode 100644 index b7efe3cc31..0000000000 --- a/Userland/Games/Solitaire/SolitaireWidget.h +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (c) 2020, Till Mayer - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include "CardStack.h" -#include -#include - -class SolitaireWidget final : public GUI::Widget { - C_OBJECT(SolitaireWidget) -public: - static constexpr int width = 640; - static constexpr int height = 480; - - virtual ~SolitaireWidget() override; - void setup(); - -private: - SolitaireWidget(Function&& on_score_update); - - class Animation { - public: - Animation() - { - } - - Animation(RefPtr animation_card, float gravity, int x_vel, float bouncyness) - : m_animation_card(animation_card) - , m_gravity(gravity) - , m_x_velocity(x_vel) - , m_bouncyness(bouncyness) - { - } - - RefPtr card() { return m_animation_card; } - - void tick() - { - VERIFY(!m_animation_card.is_null()); - m_y_velocity += m_gravity; - - if (m_animation_card->position().y() + Card::height + m_y_velocity > SolitaireWidget::height + 1 && m_y_velocity > 0) { - m_y_velocity = min((m_y_velocity * -m_bouncyness), -8.f); - m_animation_card->rect().set_y(SolitaireWidget::height - Card::height); - m_animation_card->rect().translate_by(m_x_velocity, 0); - } else { - m_animation_card->rect().translate_by(m_x_velocity, m_y_velocity); - } - } - - private: - RefPtr m_animation_card; - float m_gravity { 0 }; - int m_x_velocity { 0 }; - float m_y_velocity { 0 }; - float m_bouncyness { 0 }; - }; - - enum StackLocation { - Stock, - Waste, - Foundation1, - Foundation2, - Foundation3, - Foundation4, - Pile1, - Pile2, - Pile3, - Pile4, - Pile5, - Pile6, - Pile7, - __Count - }; - static constexpr Array piles = { Pile1, Pile2, Pile3, Pile4, Pile5, Pile6, Pile7 }; - - void mark_intersecting_stacks_dirty(Card& intersecting_card); - void update_score(int to_add); - void move_card(CardStack& from, CardStack& to); - void start_game_over_animation(); - void stop_game_over_animation(); - void create_new_animation_card(); - void check_for_game_over(); - - ALWAYS_INLINE CardStack& stack(StackLocation location) - { - return m_stacks[location]; - } - - virtual void paint_event(GUI::PaintEvent&) override; - virtual void mousedown_event(GUI::MouseEvent&) override; - virtual void mouseup_event(GUI::MouseEvent&) override; - virtual void mousemove_event(GUI::MouseEvent&) override; - virtual void doubleclick_event(GUI::MouseEvent&) override; - virtual void keydown_event(GUI::KeyEvent&) override; - virtual void timer_event(Core::TimerEvent&) override; - - NonnullRefPtrVector m_focused_cards; - NonnullRefPtrVector m_new_deck; - CardStack m_stacks[StackLocation::__Count]; - CardStack* m_focused_stack { nullptr }; - Gfx::IntPoint m_mouse_down_location; - - bool m_mouse_down { false }; - bool m_repaint_all { true }; - bool m_has_to_repaint { true }; - - Animation m_animation; - bool m_game_over_animation { false }; - - bool m_new_game_animation { false }; - uint8_t m_new_game_animation_pile { 0 }; - uint8_t m_new_game_animation_delay { 0 }; - - uint32_t m_score { 0 }; - Function m_on_score_update; -}; diff --git a/Userland/Games/Solitaire/main.cpp b/Userland/Games/Solitaire/main.cpp index 3fcdf5152e..0b8423f30a 100644 --- a/Userland/Games/Solitaire/main.cpp +++ b/Userland/Games/Solitaire/main.cpp @@ -4,7 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include "SolitaireWidget.h" +#include "Game.h" #include #include #include @@ -37,9 +37,9 @@ int main(int argc, char** argv) auto window = GUI::Window::construct(); window->set_resizable(false); - window->resize(SolitaireWidget::width, SolitaireWidget::height); + window->resize(Solitaire::Game::width, Solitaire::Game::height); - auto widget = SolitaireWidget::construct([&](uint32_t score) { + auto widget = Solitaire::Game::construct([&](uint32_t score) { window->set_title(String::formatted("Score: {} - Solitaire", score)); }); -- cgit v1.2.3