90 lines
2.7 KiB
Rust
90 lines
2.7 KiB
Rust
use crate::{
|
|
components::tile::{Grid, Tile, TileState},
|
|
states::AppState,
|
|
};
|
|
use bevy::prelude::*;
|
|
use bevy_aseprite_ultra::prelude::AseSlice;
|
|
|
|
const TILE_SIZE: f32 = 32.0;
|
|
const GRID_WIDTH: u32 = 10;
|
|
const GRID_HEIGHT: u32 = 10;
|
|
|
|
const GRID_START_X: f32 = -(GRID_WIDTH as f32 * TILE_SIZE) / 2.0 + TILE_SIZE / 2.0;
|
|
const GRID_START_Y: f32 = -(GRID_HEIGHT as f32 * TILE_SIZE) / 2.0 + TILE_SIZE / 2.0;
|
|
|
|
pub struct GridPlugin;
|
|
|
|
impl Plugin for GridPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.add_systems(OnEnter(AppState::GameScreen), setup);
|
|
app.add_systems(OnExit(AppState::GameScreen), cleanup);
|
|
|
|
app.add_systems(
|
|
Update,
|
|
update_tile_colors.run_if(in_state(AppState::GameScreen)),
|
|
);
|
|
}
|
|
}
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
let mut tiles = Vec::with_capacity(GRID_WIDTH as usize);
|
|
|
|
for x in 0..GRID_WIDTH {
|
|
let mut column = Vec::with_capacity(GRID_HEIGHT as usize);
|
|
|
|
for y in 0..GRID_HEIGHT {
|
|
let tile_entity = commands
|
|
.spawn((
|
|
Tile { x, y },
|
|
TileState::Unclaimed,
|
|
AseSlice {
|
|
name: "Unclaimed".into(),
|
|
aseprite: asset_server.load("tiles/tile-unclaimed.aseprite"),
|
|
},
|
|
Sprite::default(),
|
|
Transform::from_xyz(
|
|
GRID_START_X + x as f32 * TILE_SIZE,
|
|
GRID_START_Y + y as f32 * TILE_SIZE,
|
|
0.0,
|
|
),
|
|
))
|
|
.id();
|
|
column.push(tile_entity);
|
|
}
|
|
tiles.push(column);
|
|
}
|
|
|
|
commands.insert_resource(Grid {
|
|
width: GRID_WIDTH,
|
|
height: GRID_HEIGHT,
|
|
tiles,
|
|
});
|
|
}
|
|
|
|
fn cleanup(mut commands: Commands, tile_query: Query<Entity, With<Tile>>) {
|
|
for tile_entity in tile_query.iter() {
|
|
commands.entity(tile_entity).despawn();
|
|
}
|
|
commands.remove_resource::<Grid>();
|
|
}
|
|
|
|
fn update_tile_colors(
|
|
mut query: Query<(&TileState, &mut AseSlice)>,
|
|
asset_server: Res<AssetServer>,
|
|
) {
|
|
for (state, mut slice) in &mut query {
|
|
slice.name = match state {
|
|
TileState::Unclaimed => "Unclaimed",
|
|
TileState::Empty => "Empty",
|
|
TileState::Occupied => "Occupied",
|
|
}
|
|
.into();
|
|
|
|
slice.aseprite = match state {
|
|
TileState::Unclaimed => asset_server.load("tiles/tile-unclaimed.aseprite"),
|
|
TileState::Empty => asset_server.load("tiles/tile-empty.aseprite"),
|
|
TileState::Occupied => asset_server.load("tiles/tile-occupied.aseprite"),
|
|
};
|
|
}
|
|
}
|