Skip to main content

rsnaker/game_logic/
playing_thread_manager.rs

1pub use crate::controls::direction::Direction;
2use crate::controls::main_menu::controls_main_switch_menu;
3use crate::controls::playing_input::playing_input_loop;
4pub use crate::controls::speed::Speed;
5pub use crate::game_logic::fruits_manager::FruitsManager;
6use crate::game_logic::game_options::GameOptions;
7use crate::game_logic::playing_logic::playing_logic_loop;
8pub use crate::game_logic::state::{GameState, GameStatus};
9use crate::graphics::playing_render::playing_render_loop;
10use crate::graphics::sprites::map::Map;
11use crate::graphics::sprites::snake_body::SnakeBody;
12use ratatui::text::Span;
13use ratatui::DefaultTerminal;
14use std::cmp::max;
15use std::sync::{Arc, RwLock};
16use std::thread;
17use tracing::{debug, info, trace};
18
19/// our game engine
20/// NB: 'c must outlive 'b as, 'c (fruits manager) uses in intern the map with lock on it.
21/// NB: 't the terminal life must outlive all the other lifetimes
22pub struct Game<'a, 'b, 'c: 'b, 't: 'a + 'b + 'c> {
23    /// Game main parameters
24    options: &'t GameOptions,
25    /// The game logic speed, linked to the snake movements
26    speed: Speed,
27    /// Represents the snake moving around
28    serpent: Arc<RwLock<SnakeBody<'a>>>,
29    /// The direction chosen by the player for the snake
30    direction: Arc<RwLock<Direction>>,
31    /// The game logic map where items/snake are displayed
32    /// NB: As we want a resizable map, ``RwLock``, otherwise use only Arc<Map> (immuable)
33    carte: Arc<RwLock<Map<'b>>>,
34    /// Game states and metrics (life etc.)
35    state: Arc<RwLock<GameState>>,
36    /// Manage fruits (popping, eaten etc.)
37    fruits_manager: Arc<RwLock<FruitsManager<'c, 'b>>>,
38    /// The current terminal
39    terminal: &'t mut DefaultTerminal,
40}
41impl<'a, 'b, 'c, 't> Game<'a, 'b, 'c, 't> {
42    #[must_use]
43    fn new(
44        options: &'t GameOptions,
45        serpent: SnakeBody<'a>,
46        carte: Map<'b>,
47        terminal: &'t mut DefaultTerminal,
48    ) -> Game<'a, 'b, 'c, 't> {
49        let arc_carte = Arc::new(RwLock::new(carte));
50        let life = options.life;
51        let fruits_nb = options.nb_of_fruits;
52        let speed = options.speed;
53        Game {
54            options,
55            speed,
56            serpent: Arc::new(RwLock::new(serpent)),
57            direction: Arc::new(RwLock::new(Direction::Right)),
58            carte: arc_carte.clone(),
59            state: Arc::new(RwLock::new(GameState::new(life))),
60            fruits_manager: Arc::new(RwLock::new(FruitsManager::new(
61                fruits_nb,
62                arc_carte.clone(),
63            ))),
64            terminal,
65        }
66    }
67    /// Displays the game menu and handles user navigation
68    ///
69    /// # Panics
70    ///
71    /// This function will panic if the internal `state` lock is poisoned
72    /// and cannot be read.
73    pub fn menu(mut options: GameOptions, mut terminal: DefaultTerminal) {
74        info!("Welcome dear player ! Make your choice on Main menu !");
75        //one loop means one game, hard reset of the game from the menu
76        // (as parameters can change in the parameter menu)
77        loop {
78            //Display the menu and get the user choice: play or not
79            // (as well as others menu options of course)
80            if controls_main_switch_menu(&mut terminal, &mut options) {
81                info!("Let's play! 🐍 (Run has been entered in the menu, starting the game...)");
82                // if the player wants to play, we need to initiate some game values
83                //  to get the correct case size for display
84                let case_size = u16::try_from(max(
85                    Span::raw(&options.body_symbol).width(),
86                    Span::raw(&options.head_symbol).width(),
87                    //ratatui using UnicodeWidthStr crates as dep
88                ))
89                .expect("Bad symbol size, use a real character");
90                let carte: Map = Map::new(case_size, terminal.get_frame().area());
91                let serpent: SnakeBody = SnakeBody::new(
92                    &options.body_symbol,
93                    &options.head_symbol,
94                    options.snake_length,
95                    GameOptions::initial_position(),
96                    case_size,
97                );
98                info!(Snake_lenght = ?options.snake_length, Snake_head = ?options.head_symbol, Snake_body = ?options.body_symbol, "Snake info for starting");
99                trace!(?serpent);
100                debug!(?options);
101                let mut game = Game::new(&options, serpent, carte, &mut terminal);
102                game.start();
103                if game
104                    .state
105                    .read()
106                    .expect("Panic in a previous thread, check previous error")
107                    .status
108                    == GameStatus::ByeBye
109                {
110                    break;
111                }
112            } else {
113                break;
114            }
115        }
116        info!("Good bye, come back soon, snaker is waiting for you 🐍");
117    }
118    /// Start the main Game threads: input, rendering, logic
119    pub fn start(&mut self) {
120        debug!("Starting game threads");
121        // Be careful: not all threads on the same structure and do not keep them too much
122        // => performance issue otherwise
123        // Prepare thread use of variable
124
125        //For logical thread
126        let logic_snake = Arc::clone(&self.serpent);
127        let logic_gs = Arc::clone(&self.state);
128        let logic_dir = Arc::clone(&self.direction);
129        let carte = Arc::clone(&self.carte);
130        let fruits_manager = Arc::clone(&self.fruits_manager);
131
132        // For input management thread
133        let input_gs = Arc::clone(&self.state);
134        let input_dir = Arc::clone(&self.direction);
135
136        //if we want to have a variable speed put it under an Arc<Rw>, constant can directly be put under an Arc
137        // or share as a normal variable by copy
138        //Game speed is a constant by game, so we can clone it normally
139        let current_game_speed = self.speed;
140        let classic = self.options.classic_mode;
141        let snake_symbols = format!("{}{}", self.options.head_symbol, self.options.body_symbol);
142        //In a scope to have auto cleaning by auto join at the end of the main thread
143        thread::scope(|s| {
144            // Game logic thread
145            thread::Builder::new()
146                //For better log name the thread, otherwise just s.spawn()
147                .name("t_game_logic".to_string())
148                .spawn_scoped(s, move || {
149                    playing_logic_loop(
150                        &logic_dir,
151                        &logic_snake,
152                        &logic_gs,
153                        &carte,
154                        &fruits_manager,
155                        (current_game_speed, snake_symbols, classic),
156                    );
157                })
158                .expect("Unable to create the thread");
159            // input logic thread
160            thread::Builder::new()
161                //For better log name the thread, otherwise just s.spawn()
162                .name("t_input".to_string())
163                .spawn_scoped(s, move || {
164                    playing_input_loop(&input_dir, &input_gs);
165                })
166                .expect("Unable to create the thread");
167
168            // Graphical thread (last one, reusing the main thread)
169            playing_render_loop(
170                &Arc::clone(&self.carte),
171                &Arc::clone(&self.fruits_manager),
172                &Arc::clone(&self.state),
173                &Arc::clone(&self.serpent),
174                self.options.caps_fps,
175                (self.speed.score_modifier(), self.speed.symbol()),
176                self.terminal,
177            );
178        });
179    }
180}