From 11a1a9b8f8c0ca32b706587c00f8efc53a87c6ef Mon Sep 17 00:00:00 2001 From: Bradley Wojcik Date: Thu, 18 Apr 2024 11:10:41 -0400 Subject: [PATCH] move game tick handling to game engine --- src/engine/game.rs | 39 ++++++++++++++++++++++-- src/interface/screens/game_screen.rs | 45 ++-------------------------- 2 files changed, 40 insertions(+), 44 deletions(-) diff --git a/src/engine/game.rs b/src/engine/game.rs index 2a5a61b..bd0acd1 100644 --- a/src/engine/game.rs +++ b/src/engine/game.rs @@ -105,7 +105,7 @@ impl Game { .expect("player not found") } - pub fn current_player_mut(&mut self) -> &mut Player { + fn current_player_mut(&mut self) -> &mut Player { self.players .get_mut(&self.current_player_seat) .expect("player not found") @@ -115,9 +115,44 @@ impl Game { self.players.get(&seat).expect("player not found") } - pub fn next_turn(&mut self) { + fn next_turn(&mut self) { self.current_player_seat = self.current_player_seat.next(); } + + pub fn handle_game_tick(&mut self, mut tick_count: u64) -> u64 { + match self.state { + GameState::PickingDealer => { + if self.hand_num == 0 { + // TODO: implement picking first dealer by first black jack, then recreating the deck + self.dealer_seat = rand::random(); + } else { + self.dealer_seat = self.dealer_seat.next(); + } + self.current_player_seat = self.dealer_seat.next(); + self.state = GameState::DealingHand; + } + GameState::DealingHand => { + // TODO: deal the "appropriate" way (2, 3, 2, 3, 3, 2, 3, 2) + if tick_count >= 5 { + if self.current_player().hand.is_empty() { + self.current_player_mut().hand = self.deck.deal(5); + self.next_turn(); + } else { + // TODO: display face up card and deck on the table + self.current_player_seat = self.dealer_seat.next(); + self.state = GameState::CallingPickup; + } + tick_count = 0 + } else { + tick_count += 1 + } + } + GameState::CallingPickup => {} + GameState::CallingHighSuit => {} + GameState::PlayingHand => {} + } + tick_count + } } impl Default for Game { diff --git a/src/interface/screens/game_screen.rs b/src/interface/screens/game_screen.rs index b88f3bb..2eb7dd7 100644 --- a/src/interface/screens/game_screen.rs +++ b/src/interface/screens/game_screen.rs @@ -5,7 +5,7 @@ use crate::interface::{ use crate::{ engine::{ card::Card, - game::{Game, GameState}, + game::Game, player::Player, table::{Seat, SEAT_VARIANTS}, }, @@ -157,47 +157,8 @@ impl Screen for GameScreen { } fn handle_tick_event(&mut self) -> Option { - match self.is_paused { - false => { - match self.game.state { - GameState::PickingDealer => { - if self.game.hand_num == 0 { - // TODO: implement picking first dealer by first black jack, then recreating the deck - self.game.dealer_seat = rand::random(); - } else { - self.game.dealer_seat = self.game.dealer_seat.next(); - } - self.game.current_player_seat = self.game.dealer_seat.next(); - self.game.state = GameState::DealingHand; - } - GameState::DealingHand => { - // TODO: deal the "appropriate" way (2, 3, 2, 3, 3, 2, 3, 2) - if self.tick_count >= 5 { - if self.game.current_player().hand.is_empty() { - self.game.current_player_mut().hand = self.game.deck.deal(5); - self.game.next_turn(); - } else { - // TODO: display face up card and deck on the table - self.game.current_player_seat = self.game.dealer_seat.next(); - self.game.state = GameState::CallingPickup; - } - self.tick_count = 0 - } else { - self.tick_count += 1 - } - } - GameState::CallingPickup => {} - GameState::CallingHighSuit => {} - GameState::PlayingHand => {} - } - // if self.tick_count >= 20 { - // self.game.next_turn(); - // self.tick_count = 0 - // } else { - // self.tick_count += 1 - // } - } - true => {} + if !self.is_paused { + self.tick_count = self.game.handle_game_tick(self.tick_count); } None }