docs: document grid system and game screen environment (#65)

This commit is contained in:
demenik
2025-12-10 18:12:27 +01:00
parent 49302948d2
commit 040b2742ca
6 changed files with 21 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
use crate::prelude::*; use crate::prelude::*;
/// Plugin for the main game screen, managing the game loop and environment.
pub struct GameScreenPlugin; pub struct GameScreenPlugin;
impl Plugin for GameScreenPlugin { impl Plugin for GameScreenPlugin {
@@ -9,10 +10,12 @@ impl Plugin for GameScreenPlugin {
} }
} }
/// Sets up the game screen environment (e.g., background color).
fn setup(mut clear_color: ResMut<ClearColor>) { fn setup(mut clear_color: ResMut<ClearColor>) {
*clear_color = ClearColor(Color::srgb(0.294, 0.412, 0.184)); *clear_color = ClearColor(Color::srgb(0.294, 0.412, 0.184));
} }
/// Cleans up resources when exiting the game screen.
fn cleanup(mut clear_color: ResMut<ClearColor>) { fn cleanup(mut clear_color: ResMut<ClearColor>) {
*clear_color = ClearColor(Color::srgb(0.2, 0.2, 0.2)); *clear_color = ClearColor(Color::srgb(0.2, 0.2, 0.2));
} }

View File

@@ -1,18 +1,22 @@
use super::errors::GridError; use super::errors::GridError;
use crate::prelude::*; use crate::prelude::*;
/// Component representing a single tile on the grid.
#[derive(Component)] #[derive(Component)]
pub struct Tile { pub struct Tile {
pub x: u32, pub x: u32,
pub y: u32, pub y: u32,
} }
/// Visual marker component for the crop on a tile.
#[derive(Component)] #[derive(Component)]
pub struct CropVisual; pub struct CropVisual;
/// Visual marker component for the water on a tile.
#[derive(Component)] #[derive(Component)]
pub struct WaterVisual; pub struct WaterVisual;
/// The logical state of a tile.
#[derive(Component, Default, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] #[derive(Component, Default, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub enum TileState { pub enum TileState {
#[default] #[default]
@@ -38,6 +42,7 @@ impl TileState {
} }
} }
/// Resource containing grid dimensions and tile entities.
#[derive(Resource)] #[derive(Resource)]
pub struct Grid { pub struct Grid {
pub width: u32, pub width: u32,
@@ -46,6 +51,7 @@ pub struct Grid {
} }
impl Grid { impl Grid {
/// Returns the entity of the tile at the given position.
pub fn get_tile(&self, pos: (u32, u32)) -> Result<Entity, GridError> { pub fn get_tile(&self, pos: (u32, u32)) -> Result<Entity, GridError> {
if pos.0 >= self.width || pos.1 >= self.height { if pos.0 >= self.width || pos.1 >= self.height {
return Err(GridError::OutOfBounds { return Err(GridError::OutOfBounds {
@@ -56,6 +62,7 @@ impl Grid {
Ok(self.tiles[pos.0 as usize][pos.1 as usize]) Ok(self.tiles[pos.0 as usize][pos.1 as usize])
} }
/// Modifies the state of a tile using a mapping function.
pub fn map_tile_state<F>( pub fn map_tile_state<F>(
&self, &self,
pos: (u32, u32), pos: (u32, u32),
@@ -75,6 +82,7 @@ impl Grid {
Ok(()) Ok(())
} }
/// Counts the number of tiles that are not unclaimed.
pub fn count_claimed_tiles(&self, tile_query: &Query<&TileState>) -> u32 { pub fn count_claimed_tiles(&self, tile_query: &Query<&TileState>) -> u32 {
self.tiles self.tiles
.iter() .iter()

View File

@@ -1 +1,2 @@
/// The pixel size of a tile (width and height).
pub const TILE_SIZE: f32 = 32.0; pub const TILE_SIZE: f32 = 32.0;

View File

@@ -1,5 +1,6 @@
use std::{error::Error, fmt}; use std::{error::Error, fmt};
/// Errors related to grid operations.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum GridError { pub enum GridError {
OutOfBounds { x: i32, y: i32 }, OutOfBounds { x: i32, y: i32 },

View File

@@ -7,6 +7,7 @@ pub mod consts;
pub mod errors; pub mod errors;
pub mod utils; pub mod utils;
/// Manages the game grid, including tiles, visuals, and updates.
pub struct GridPlugin; pub struct GridPlugin;
impl Plugin for GridPlugin { impl Plugin for GridPlugin {
@@ -18,6 +19,7 @@ impl Plugin for GridPlugin {
} }
} }
/// Initializes the grid and spawns tile entities.
fn setup(mut commands: Commands, asset_server: Res<AssetServer>, config: Res<GameConfig>) { fn setup(mut commands: Commands, asset_server: Res<AssetServer>, config: Res<GameConfig>) {
let grid_width = config.grid_width; let grid_width = config.grid_width;
let grid_height = config.grid_height; let grid_height = config.grid_height;
@@ -96,6 +98,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, config: Res<Gam
}); });
} }
/// Despawns all grid entities and removes resources.
fn cleanup(mut commands: Commands, tile_query: Query<Entity, With<Tile>>) { fn cleanup(mut commands: Commands, tile_query: Query<Entity, With<Tile>>) {
for tile_entity in tile_query.iter() { for tile_entity in tile_query.iter() {
commands.entity(tile_entity).despawn(); commands.entity(tile_entity).despawn();
@@ -103,6 +106,7 @@ fn cleanup(mut commands: Commands, tile_query: Query<Entity, With<Tile>>) {
commands.remove_resource::<Grid>(); commands.remove_resource::<Grid>();
} }
/// Updates tile visuals based on their state (e.g., crop growth, highlighting).
fn update_tiles( fn update_tiles(
mut query: Query< mut query: Query<
(&TileState, &mut AseSlice, &Children, &Tile), (&TileState, &mut AseSlice, &Children, &Tile),

View File

@@ -1,14 +1,17 @@
use super::errors::GridError; use super::errors::GridError;
use crate::prelude::*; use crate::prelude::*;
/// Calculates the starting X coordinate for centering the grid.
pub fn grid_start_x(grid_width: u32) -> f32 { pub fn grid_start_x(grid_width: u32) -> f32 {
-(grid_width as f32 * TILE_SIZE) / 2.0 + TILE_SIZE / 2.0 -(grid_width as f32 * TILE_SIZE) / 2.0 + TILE_SIZE / 2.0
} }
/// Calculates the starting Y coordinate for centering the grid.
pub fn grid_start_y(grid_height: u32) -> f32 { pub fn grid_start_y(grid_height: u32) -> f32 {
-(grid_height as f32 * TILE_SIZE) / 2.0 + TILE_SIZE / 2.0 -(grid_height as f32 * TILE_SIZE) / 2.0 + TILE_SIZE / 2.0
} }
/// Converts world coordinates to grid coordinates.
pub fn world_to_grid_coords( pub fn world_to_grid_coords(
world_pos: Vec3, world_pos: Vec3,
grid_width: u32, grid_width: u32,
@@ -30,6 +33,7 @@ pub fn world_to_grid_coords(
Ok((x as u32, y as u32)) Ok((x as u32, y as u32))
} }
/// Converts grid coordinates to world coordinates.
pub fn grid_to_world_coords( pub fn grid_to_world_coords(
grid_x: u32, grid_x: u32,
grid_y: u32, grid_y: u32,