rsnaker/game_logic/
state.rs

1//! # Game State Management
2//!
3//! This module defines a `GameState` struct that manages game-related flags
4//! such as life, score, and game status.
5//! It provides methods for initializing and resetting the game state.
6//!
7//! # Example
8//! ```rust
9//! use rsnaker::game_logic::state::{GameState, GameStatus};
10//!
11//! let mut state = GameState::new(3); // Initialize game_logic with 3 lives
12//! assert_eq!(state.life, 3);
13//! assert_eq!(state.score, 0);
14//! assert_eq!(state.status, GameStatus::Playing);
15//!
16//! state.reset(); // Reset game_logic state
17//! assert_eq!(state.life, 3);
18//! assert_eq!(state.score, 0);
19//! ```
20//!
21
22/// Represents the possible states of the game.
23#[derive(Debug, Clone, PartialEq)]
24pub enum GameStatus {
25    Paused,     // The game is currently paused
26    GameOver,   // The game has ended
27    ByeBye,     // The game has exited
28    Playing,    // The game is in progress
29    Restarting, // The game is restarting
30    Menu,       // The game is going or displaying greeting Menu
31}
32
33/// Manages the game state, including life count, score, and current status.
34#[derive(Clone, Debug, PartialEq)]
35pub struct GameState {
36    pub life: u16,          // Current player life count
37    pub score: i32,         // Current player score
38    pub status: GameStatus, // Current game status
39    life_ini: u16,          // Initial life count (used for reset)
40}
41
42impl GameState {
43    /// Creates a new `GameState` with the specified initial life count.
44    ///
45    /// # Arguments
46    /// * `life` - The initial number of lives the player starts with.
47    ///
48    /// # Returns
49    /// A new instance of `GameState` with default values.
50    #[must_use]
51    pub fn new(life: u16) -> Self {
52        Self {
53            life,
54            score: 0,
55            status: GameStatus::Playing,
56            life_ini: life,
57        }
58    }
59
60    /// Resets the game state to its initial values.
61    pub fn reset(&mut self) {
62        self.score = 0;
63        self.status = GameStatus::Playing;
64        self.life = self.life_ini;
65    }
66}