Merge branch '6-2d-farming-grid' into 'dev'
Resolve "2D Farming Grid" See merge request softwaregrundprojekt/2025-2026/einzelprojekt/tutorium-moritz/bernroider-dominik/bernroider-dominik!2
This commit is contained in:
BIN
assets/tiles/tile-empty.aseprite
Normal file
BIN
assets/tiles/tile-empty.aseprite
Normal file
Binary file not shown.
BIN
assets/tiles/tile-occupied.aseprite
Normal file
BIN
assets/tiles/tile-occupied.aseprite
Normal file
Binary file not shown.
BIN
assets/tiles/tile-unclaimed.aseprite
Normal file
BIN
assets/tiles/tile-unclaimed.aseprite
Normal file
Binary file not shown.
2
src/components/mod.rs
Normal file
2
src/components/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod pom;
|
||||
pub mod tile;
|
||||
23
src/components/tile.rs
Normal file
23
src/components/tile.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use bevy::prelude::*;
|
||||
use bevy_aseprite_ultra::prelude::AseSlice;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Tile {
|
||||
pub x: u32,
|
||||
pub y: u32,
|
||||
}
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub enum TileState {
|
||||
#[default]
|
||||
Unclaimed,
|
||||
Empty,
|
||||
Occupied,
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct Grid {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub tiles: Vec<Vec<Entity>>,
|
||||
}
|
||||
@@ -25,6 +25,7 @@ fn main() {
|
||||
plugins::CorePlugin,
|
||||
plugins::StartScreenPlugin,
|
||||
plugins::GameScreenPlugin,
|
||||
plugins::GridPlugin,
|
||||
))
|
||||
.run();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use crate::components::*;
|
||||
use crate::states::*;
|
||||
use bevy::color::palettes::css::GREEN;
|
||||
use bevy::prelude::*;
|
||||
use bevy_aseprite_ultra::prelude::*;
|
||||
|
||||
@@ -15,15 +14,16 @@ impl Plugin for GameScreenPlugin {
|
||||
|
||||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
commands.spawn((
|
||||
Pom,
|
||||
pom::Pom,
|
||||
AseAnimation {
|
||||
aseprite: asset_server.load("pom-sleep.aseprite"),
|
||||
aseprite: asset_server.load("pom/pom-sleep.aseprite"),
|
||||
animation: Animation::tag("sleep-sit-start").with_repeat(AnimationRepeat::Loop),
|
||||
},
|
||||
Sprite::default(),
|
||||
Transform::from_xyz(0.0, 0.0, 1.0),
|
||||
));
|
||||
|
||||
commands.insert_resource(ClearColor(Color::from(GREEN)));
|
||||
commands.insert_resource(ClearColor(Color::srgb(0.294, 0.412, 0.184)));
|
||||
}
|
||||
|
||||
fn cleanup(mut commands: Commands) {
|
||||
|
||||
89
src/plugins/grid.rs
Normal file
89
src/plugins/grid.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
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"),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
pub mod core;
|
||||
pub mod game_screen;
|
||||
pub mod grid;
|
||||
pub mod start_screen;
|
||||
|
||||
pub use core::CorePlugin;
|
||||
pub use game_screen::GameScreenPlugin;
|
||||
pub use grid::GridPlugin;
|
||||
pub use start_screen::StartScreenPlugin;
|
||||
|
||||
Reference in New Issue
Block a user