From 4c548cdb457adcd44309bf5b0fb04074878f4dcb Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Fri, 1 Dec 2023 20:17:15 -0800 Subject: [PATCH] Ayyy, got 2d sprites loading! With tweaks! Now to get it to reload... --- assets/martian.tweak.toml | 21 +++++++++++++++++++++ src/display2d.rs | 18 ++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/assets/martian.tweak.toml b/assets/martian.tweak.toml index 8dac82a..e86e691 100644 --- a/assets/martian.tweak.toml +++ b/assets/martian.tweak.toml @@ -92,3 +92,24 @@ post_saturation = 1.0 [display3d.ssao] # Options: Off, Low, Medium, High, Ultra quality_level = "Off" + +### +# Display 2D tweaks +### +[display2d.sprites] +# Sprite file path inside the `assets` folder +file = "images/sprites.png" +# Size of each tile [x,y] (they have to be the same size) +tile_size = [16, 16] +columns = 8 +rows = 1 +# The order of each sprite left-to-right, top-to-bottom +sprite_order = [ "LightTile" + , "DarkTile" + , "RedQueen" + , "RedDrone" + , "RedPawn" + , "BlueQueen" + , "BlueDrone" + , "BluePawn" + ] diff --git a/src/display2d.rs b/src/display2d.rs index 5226eae..b3d9b2b 100644 --- a/src/display2d.rs +++ b/src/display2d.rs @@ -17,7 +17,12 @@ pub(crate) struct Display2dPlugin; impl Plugin for Display2dPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, (initialize_camera, set_background)) - .add_systems(OnEnter(GameState::Loading), load_spritesheet) + .add_systems( + Update, + load_spritesheet + .run_if(on_event::>()) + .run_if(in_state(GameState::Loading)), + ) .add_systems(OnExit(GameState::Loading), initialize_board) .add_systems( Update, @@ -65,6 +70,7 @@ pub(crate) struct Display2d; struct BackgroundImage; /// All possible sprites +/// Necessary because individual components of Piece, Side, and Light/Dark are not homogeneous #[derive(Debug, Deserialize, Component, Clone, PartialEq)] pub(crate) enum GameSprite { RedQueen, @@ -123,19 +129,23 @@ fn load_spritesheet( tweaks: Res>, mut commands: Commands, ) { + info!("Loading spritesheet"); let handle: Handle = server.load("martian.tweak.toml"); + info!("Handle: {:?}", handle); let tweak = tweaks.get(&handle).expect("Load tweakfiles"); + info!("Tweak: {:?}", tweak); let atlas = TextureAtlas::from_grid( server.load(tweak.display2d.sprites.file.clone()), Vec2::new( - tweak.display2d.sprites.tile_size.x, - tweak.display2d.sprites.tile_size.y, + tweak.display2d.sprites.tile_size[0] as f32, + tweak.display2d.sprites.tile_size[1] as f32, ), tweak.display2d.sprites.columns, tweak.display2d.sprites.rows, None, None, ); + info!("Atlas: {:?}", atlas); commands.insert_resource(SpriteSheet { handle: texture_atlases.add(atlas), }); @@ -414,7 +424,7 @@ pub(crate) mod tweaks { #[serde(default)] pub file: String, #[serde(default)] - pub tile_size: Vec2, + pub tile_size: [u8; 2], #[serde(default)] pub columns: usize, #[serde(default)]