Game.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Copyright (c) 2021, Jamie Mansfield <jmansfield@cadixdev.org>
  3. * Copyright (c) 2022, Jonas Höpner <me@jonashoepner.de>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "Game.h"
  9. #include <AK/Random.h>
  10. #include <LibGUI/Event.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGfx/Palette.h>
  13. REGISTER_WIDGET(Spider, Game);
  14. namespace Spider {
  15. static constexpr uint8_t new_game_animation_delay = 2;
  16. static constexpr uint8_t draw_animation_delay = 2;
  17. static constexpr int s_timer_interval_ms = 1000 / 60;
  18. Game::Game()
  19. {
  20. m_stacks.append(adopt_ref(*new CardStack({ 10, Game::height - Card::height - 10 }, CardStack::Type::Waste)));
  21. m_stacks.append(adopt_ref(*new CardStack({ Game::width - Card::width - 10, Game::height - Card::height - 10 }, CardStack::Type::Stock)));
  22. for (int i = 0; i < 10; i++) {
  23. m_stacks.append(adopt_ref(*new CardStack({ 10 + i * (Card::width + 10), 10 }, CardStack::Type::Normal)));
  24. }
  25. }
  26. void Game::setup(Mode mode)
  27. {
  28. if (m_new_game_animation)
  29. stop_timer();
  30. m_mode = mode;
  31. if (on_game_end)
  32. on_game_end(GameOverReason::NewGame, m_score);
  33. for (auto& stack : m_stacks)
  34. stack.clear();
  35. m_new_game_animation_pile = 0;
  36. m_score = 500;
  37. update_score(0);
  38. NonnullRefPtrVector<Card> deck;
  39. deck.ensure_capacity(Card::card_count * 2);
  40. for (int i = 0; i < Card::card_count; ++i) {
  41. switch (m_mode) {
  42. case Mode::SingleSuit:
  43. for (int j = 0; j < 8; j++) {
  44. deck.append(Card::construct(Card::Suit::Spades, i));
  45. }
  46. break;
  47. case Mode::TwoSuit:
  48. for (int j = 0; j < 4; j++) {
  49. deck.append(Card::construct(Card::Suit::Spades, i));
  50. deck.append(Card::construct(Card::Suit::Hearts, i));
  51. }
  52. break;
  53. default:
  54. VERIFY_NOT_REACHED();
  55. break;
  56. }
  57. }
  58. m_new_deck.clear_with_capacity();
  59. m_new_deck.ensure_capacity(deck.size());
  60. while (!deck.is_empty())
  61. m_new_deck.append(deck.take(get_random_uniform(deck.size())));
  62. m_focused_stack = nullptr;
  63. m_focused_cards.clear();
  64. m_new_game_animation = true;
  65. start_timer(s_timer_interval_ms);
  66. update();
  67. }
  68. void Game::start_timer_if_necessary()
  69. {
  70. if (on_game_start && m_waiting_for_new_game) {
  71. on_game_start();
  72. m_waiting_for_new_game = false;
  73. }
  74. }
  75. void Game::update_score(int delta)
  76. {
  77. m_score = max(static_cast<int>(m_score) + delta, 0);
  78. if (on_score_update)
  79. on_score_update(m_score);
  80. }
  81. void Game::draw_cards()
  82. {
  83. // draw a single card from the stock for each pile
  84. auto& stock_pile = stack(Stock);
  85. if (stock_pile.is_empty())
  86. return;
  87. update_score(-1);
  88. m_draw_animation = true;
  89. m_original_stock_rect = stock_pile.bounding_box();
  90. start_timer(s_timer_interval_ms);
  91. }
  92. void Game::mark_intersecting_stacks_dirty(Card& intersecting_card)
  93. {
  94. for (auto& stack : m_stacks) {
  95. if (intersecting_card.rect().intersects(stack.bounding_box()))
  96. update(stack.bounding_box());
  97. }
  98. update(intersecting_card.rect());
  99. }
  100. void Game::ensure_top_card_is_visible(NonnullRefPtr<CardStack> stack)
  101. {
  102. if (stack->is_empty())
  103. return;
  104. auto& top_card = stack->peek();
  105. if (top_card.is_upside_down()) {
  106. top_card.set_upside_down(false);
  107. update(top_card.rect());
  108. }
  109. }
  110. void Game::detect_full_stacks()
  111. {
  112. auto& completed_stack = stack(Completed);
  113. for (auto pile : piles) {
  114. auto& current_pile = stack(pile);
  115. bool started = false;
  116. uint8_t last_value;
  117. Color color;
  118. for (size_t i = current_pile.stack().size(); i > 0; i--) {
  119. auto& card = current_pile.stack().at(i - 1);
  120. if (card.is_upside_down())
  121. break;
  122. if (!started) {
  123. if (card.value() != 0) {
  124. break;
  125. }
  126. started = true;
  127. color = card.color();
  128. } else if (card.value() != last_value + 1 || card.color() != color) {
  129. break;
  130. } else if (card.value() == Card::card_count - 1) {
  131. // we have a full set
  132. auto original_current_rect = current_pile.bounding_box();
  133. for (size_t j = 0; j < Card::card_count; j++) {
  134. completed_stack.push(current_pile.pop());
  135. }
  136. update(original_current_rect);
  137. update(completed_stack.bounding_box());
  138. ensure_top_card_is_visible(current_pile);
  139. update_score(101);
  140. }
  141. last_value = card.value();
  142. }
  143. }
  144. detect_victory();
  145. }
  146. void Game::detect_victory()
  147. {
  148. for (auto pile : piles) {
  149. auto& current_pile = stack(pile);
  150. if (!current_pile.is_empty())
  151. return;
  152. }
  153. if (on_game_end)
  154. on_game_end(GameOverReason::Victory, m_score);
  155. }
  156. void Game::paint_event(GUI::PaintEvent& event)
  157. {
  158. Gfx::Color background_color = this->background_color();
  159. GUI::Frame::paint_event(event);
  160. GUI::Painter painter(*this);
  161. painter.add_clip_rect(frame_inner_rect());
  162. painter.add_clip_rect(event.rect());
  163. if (!m_focused_cards.is_empty()) {
  164. for (auto& focused_card : m_focused_cards)
  165. focused_card.clear(painter, background_color);
  166. }
  167. for (auto& stack : m_stacks) {
  168. stack.draw(painter, background_color);
  169. }
  170. if (!m_focused_cards.is_empty()) {
  171. for (auto& focused_card : m_focused_cards) {
  172. focused_card.draw(painter);
  173. focused_card.save_old_position();
  174. }
  175. }
  176. if (!m_mouse_down) {
  177. if (!m_focused_cards.is_empty()) {
  178. for (auto& card : m_focused_cards)
  179. card.set_moving(false);
  180. m_focused_cards.clear();
  181. }
  182. if (m_focused_stack) {
  183. m_focused_stack->set_focused(false);
  184. m_focused_stack = nullptr;
  185. }
  186. }
  187. }
  188. void Game::mousedown_event(GUI::MouseEvent& event)
  189. {
  190. GUI::Frame::mousedown_event(event);
  191. if (m_new_game_animation || m_draw_animation)
  192. return;
  193. auto click_location = event.position();
  194. for (auto& to_check : m_stacks) {
  195. if (to_check.type() == CardStack::Type::Waste)
  196. continue;
  197. if (to_check.bounding_box().contains(click_location)) {
  198. if (to_check.type() == CardStack::Type::Stock) {
  199. start_timer_if_necessary();
  200. draw_cards();
  201. } else if (!to_check.is_empty()) {
  202. auto& top_card = to_check.peek();
  203. if (top_card.is_upside_down()) {
  204. if (top_card.rect().contains(click_location)) {
  205. top_card.set_upside_down(false);
  206. start_timer_if_necessary();
  207. update(top_card.rect());
  208. }
  209. } else if (m_focused_cards.is_empty()) {
  210. to_check.add_all_grabbed_cards(click_location, m_focused_cards, Cards::CardStack::MovementRule::Same);
  211. m_mouse_down_location = click_location;
  212. if (m_focused_stack)
  213. m_focused_stack->set_focused(false);
  214. to_check.set_focused(true);
  215. m_focused_stack = &to_check;
  216. // When the user wants to automatically move cards, do not go into the drag mode.
  217. if (event.button() != GUI::MouseButton::Secondary)
  218. m_mouse_down = true;
  219. start_timer_if_necessary();
  220. }
  221. }
  222. break;
  223. }
  224. }
  225. }
  226. void Game::move_focused_cards(CardStack& stack)
  227. {
  228. for (auto& to_intersect : m_focused_cards) {
  229. mark_intersecting_stacks_dirty(to_intersect);
  230. stack.push(to_intersect);
  231. (void)m_focused_stack->pop();
  232. }
  233. update_score(-1);
  234. update(m_focused_stack->bounding_box());
  235. update(stack.bounding_box());
  236. detect_full_stacks();
  237. ensure_top_card_is_visible(*m_focused_stack);
  238. }
  239. void Game::mouseup_event(GUI::MouseEvent& event)
  240. {
  241. GUI::Frame::mouseup_event(event);
  242. if (!m_focused_stack || m_focused_cards.is_empty() || m_new_game_animation || m_draw_animation)
  243. return;
  244. bool rebound = true;
  245. if (event.button() == GUI::MouseButton::Secondary) {
  246. // This enables the game to move the focused cards to the first possible stack excluding empty stacks.
  247. // NOTE: This ignores empty stacks, as the game has no undo button, and a card, which has been moved to an empty stack without any other possibilities is not reversible.
  248. for (auto& stack : m_stacks) {
  249. if (stack.is_focused())
  250. continue;
  251. if (stack.is_allowed_to_push(m_focused_cards.at(0), m_focused_cards.size(), Cards::CardStack::MovementRule::Any) && !stack.is_empty()) {
  252. move_focused_cards(stack);
  253. rebound = false;
  254. break;
  255. }
  256. }
  257. } else {
  258. for (auto& stack : m_stacks) {
  259. if (stack.is_focused())
  260. continue;
  261. for (auto& focused_card : m_focused_cards) {
  262. if (stack.bounding_box().intersects(focused_card.rect())) {
  263. if (stack.is_allowed_to_push(m_focused_cards.at(0), m_focused_cards.size(), Cards::CardStack::MovementRule::Any)) {
  264. move_focused_cards(stack);
  265. rebound = false;
  266. break;
  267. }
  268. }
  269. }
  270. }
  271. }
  272. if (rebound) {
  273. for (auto& to_intersect : m_focused_cards)
  274. mark_intersecting_stacks_dirty(to_intersect);
  275. m_focused_stack->rebound_cards();
  276. update(m_focused_stack->bounding_box());
  277. }
  278. m_mouse_down = false;
  279. }
  280. void Game::mousemove_event(GUI::MouseEvent& event)
  281. {
  282. GUI::Frame::mousemove_event(event);
  283. if (!m_mouse_down || m_new_game_animation || m_draw_animation)
  284. return;
  285. auto click_location = event.position();
  286. int dx = click_location.dx_relative_to(m_mouse_down_location);
  287. int dy = click_location.dy_relative_to(m_mouse_down_location);
  288. for (auto& to_intersect : m_focused_cards) {
  289. mark_intersecting_stacks_dirty(to_intersect);
  290. to_intersect.rect().translate_by(dx, dy);
  291. update(to_intersect.rect());
  292. }
  293. m_mouse_down_location = click_location;
  294. }
  295. void Game::timer_event(Core::TimerEvent&)
  296. {
  297. if (m_new_game_animation) {
  298. if (m_new_game_animation_delay < new_game_animation_delay) {
  299. ++m_new_game_animation_delay;
  300. } else {
  301. m_new_game_animation_delay = 0;
  302. auto& current_pile = stack(piles.at(m_new_game_animation_pile));
  303. // for first 4 piles, draw 6 cards
  304. // for last 6 piles, draw 5 cards
  305. size_t cards_to_draw = m_new_game_animation_pile < 4 ? 6 : 5;
  306. if (current_pile.count() < (cards_to_draw - 1)) {
  307. auto card = m_new_deck.take_last();
  308. card->set_upside_down(true);
  309. current_pile.push(card);
  310. } else {
  311. current_pile.push(m_new_deck.take_last());
  312. ++m_new_game_animation_pile;
  313. }
  314. update(current_pile.bounding_box());
  315. if (m_new_game_animation_pile == piles.size()) {
  316. VERIFY(m_new_deck.size() == 50);
  317. auto& stock_pile = stack(Stock);
  318. while (!m_new_deck.is_empty())
  319. stock_pile.push(m_new_deck.take_last());
  320. update(stock_pile.bounding_box());
  321. m_new_game_animation = false;
  322. m_waiting_for_new_game = true;
  323. stop_timer();
  324. }
  325. }
  326. } else if (m_draw_animation) {
  327. if (m_draw_animation_delay < draw_animation_delay) {
  328. ++m_draw_animation_delay;
  329. } else {
  330. auto& stock_pile = stack(Stock);
  331. auto& current_pile = stack(piles.at(m_draw_animation_pile));
  332. auto card = stock_pile.pop();
  333. card->set_upside_down(false);
  334. current_pile.push(card);
  335. update(current_pile.bounding_box());
  336. ++m_draw_animation_pile;
  337. if (m_draw_animation_pile == piles.size()) {
  338. update(m_original_stock_rect);
  339. detect_full_stacks();
  340. m_draw_animation = false;
  341. m_draw_animation_delay = 0;
  342. m_draw_animation_pile = 0;
  343. stop_timer();
  344. }
  345. }
  346. }
  347. }
  348. }