From 781d8da5de5efe5c29185e04cd8427a4553a7cc4 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Sat, 23 Dec 2023 16:50:08 -0600 Subject: [PATCH] Add statues to confirm camera is working --- src/assets.rs | 15 +++++++++++++++ src/camera.rs | 21 ++++++++++++++++++--- src/main.rs | 2 ++ src/player.rs | 1 - src/statue.rs | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 src/statue.rs diff --git a/src/assets.rs b/src/assets.rs index 0728ee4..1cb15f5 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -8,6 +8,7 @@ pub struct AssetLoader { #[derive(Resource, Debug, Default)] pub struct Sprites { pub player: Handle, + pub statue: Handle, } pub struct AssetPlugin; @@ -25,6 +26,8 @@ fn load_assets( mut texture_atlases: ResMut>, ) { let player_img = asset_server.load("img/Player.png"); + let props_img = asset_server.load("img/Props.png"); + let player_atlas = TextureAtlas::from_grid( player_img, Vec2::new(32.0, 64.0), @@ -33,10 +36,22 @@ fn load_assets( Some(Vec2 { x: 0., y: 0. }), Some(Vec2 { x: 0., y: 0. }), ); + let statue_atlas = TextureAtlas::from_grid( + props_img, + Vec2::new(40., 74.), + 1, + 1, + None, + Some(Vec2::new(443., 20.)), + ); + let player_atlas_handle = texture_atlases.add(player_atlas); + let statue_atlas_handle = texture_atlases.add(statue_atlas); + *assets = AssetLoader { sprites: Sprites { player: player_atlas_handle, + statue: statue_atlas_handle, }, }; } diff --git a/src/camera.rs b/src/camera.rs index deaf733..a947bf3 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,15 +1,30 @@ use bevy::prelude::*; +use crate::player; + pub struct Camera; impl Plugin for Camera { fn build(&self, app: &mut App) { - app.add_systems(Startup, spawn_camera); + app.add_systems(Startup, spawn) + .add_systems(PostUpdate, focus); } } -fn spawn_camera(mut commands: Commands) { +fn spawn(mut commands: Commands) { let mut bundle = Camera2dBundle::default(); - bundle.projection.scale = 1.; + bundle.projection.scale = 0.25; commands.spawn(bundle); } + +fn focus( + player: Query<&Transform, With>, + mut camera: Query< + &mut Transform, + (With, Without), + >, +) { + let ply = player.single(); + let mut camt = camera.single_mut(); + camt.translation = ply.translation +} diff --git a/src/main.rs b/src/main.rs index ac2c242..a00e750 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod assets; mod camera; mod movement; mod player; +mod statue; fn main() { let mut app = App::new(); @@ -20,6 +21,7 @@ fn main() { .set(ImagePlugin::default_nearest()), ) .add_plugins(( + statue::Statue, camera::Camera, assets::AssetPlugin, player::Player, diff --git a/src/player.rs b/src/player.rs index eaf23f8..87cec4a 100644 --- a/src/player.rs +++ b/src/player.rs @@ -23,7 +23,6 @@ fn spawn_player(mut commands: Commands, assets: Res) { SpriteSheetBundle { texture_atlas: assets.sprites.player.clone(), sprite: TextureAtlasSprite::new(0), - transform: Transform::from_scale(Vec3::splat(3.0)), ..Default::default() }, Mover { diff --git a/src/statue.rs b/src/statue.rs new file mode 100644 index 0000000..a4c6993 --- /dev/null +++ b/src/statue.rs @@ -0,0 +1,33 @@ +use bevy::prelude::*; + +use crate::assets::AssetLoader; + +#[derive(Component, Debug)] +pub struct Statue; + +impl Plugin for Statue { + fn build(&self, app: &mut App) { + app.add_systems(PostStartup, spawn_statue); + } +} + +fn spawn_statue(mut commands: Commands, assets: Res) { + commands.spawn(( + Statue, + SpriteSheetBundle { + texture_atlas: assets.sprites.statue.clone(), + sprite: TextureAtlasSprite::new(0), + transform: Transform::from_translation(Vec3::new(50., 50., 0.)), + ..Default::default() + }, + )); + commands.spawn(( + Statue, + SpriteSheetBundle { + texture_atlas: assets.sprites.statue.clone(), + sprite: TextureAtlasSprite::new(0), + transform: Transform::from_translation(Vec3::new(50., 100., 0.)), + ..Default::default() + }, + )); +}