diff --git a/visualizer/assets/pot/sprite.png b/visualizer/assets/pot/sprite.png new file mode 100644 index 0000000..73b3a22 Binary files /dev/null and b/visualizer/assets/pot/sprite.png differ diff --git a/visualizer/src/main.rs b/visualizer/src/main.rs index d6a073c..fc51b4c 100644 --- a/visualizer/src/main.rs +++ b/visualizer/src/main.rs @@ -7,12 +7,13 @@ use tracing::Level; mod api; mod asteroid; mod map; +mod pot; mod ships; #[derive(Component)] struct MyCameraMarker; -fn setup_camera(mut commands: Commands) { +fn setup(mut commands: Commands) { commands.spawn(Camera2dBundle::default()).insert(PanCam { min_scale: 1., max_scale: Some(3.), @@ -20,20 +21,6 @@ fn setup_camera(mut commands: Commands) { }); } -fn setup_ui(mut commands: Commands) { - commands.spawn(NodeBundle { - background_color: Color::RED.into(), - ..Default::default() - }); -} - -// fn setup_environment(mut commands: Commands, asset_server: Res) { -// commands.spawn(SpriteBundle { -// texture: asset_server.load("background.gif"), -// ..default() -// }); -// } - fn main() { tracing_subscriber::fmt().with_max_level(Level::INFO).init(); @@ -42,9 +29,12 @@ fn main() { .add_plugins(PanCamPlugin) .add_plugins(EntropyPlugin::::default()) .add_plugins(DefaultPickingPlugins) - .add_systems(Startup, setup_camera) - .add_systems(Startup, setup_ui) - // .add_systems(Startup, setup_environment) - .add_plugins((map::MapPlugin, ships::ShipsPlugin, asteroid::AsteroidPlugin)) + .add_systems(Startup, setup) + .add_plugins(( + map::MapPlugin, + ships::ShipsPlugin, + asteroid::AsteroidPlugin, + pot::PotPlugin, + )) .run(); } diff --git a/visualizer/src/map/mod.rs b/visualizer/src/map/mod.rs index 96c9054..494f9cb 100644 --- a/visualizer/src/map/mod.rs +++ b/visualizer/src/map/mod.rs @@ -118,12 +118,16 @@ fn spawn_assets( } } +fn spawn_pot(mut commands: Commands, pot_material: Res) { + commands.spawn(crate::pot::Pot::new(Position::new(0, 0), &pot_material)); +} + pub struct MapPlugin; impl Plugin for MapPlugin { fn build(&self, app: &mut App) { app.add_event::() - .add_systems(Startup, load_assets_map) + .add_systems(Startup, (load_assets_map, spawn_pot)) .add_systems(Update, (read_load_assets_stream, spawn_assets)) .insert_state(MapState(Vec::new())); } diff --git a/visualizer/src/pot/mod.rs b/visualizer/src/pot/mod.rs new file mode 100644 index 0000000..587bb9d --- /dev/null +++ b/visualizer/src/pot/mod.rs @@ -0,0 +1,77 @@ +use bevy::prelude::*; + +use crate::map::Position; + +const TILE_SIZE: u32 = 64; + +#[derive(Resource)] +pub struct Material { + texture: Handle, + layout: Handle, +} + +impl FromWorld for Material { + fn from_world(world: &mut World) -> Self { + let texture: Handle = world + .get_resource::() + .unwrap() + .load("pot/sprite.png"); + + let layout = world + .get_resource_mut::>() + .unwrap() + .add(TextureAtlasLayout::from_grid( + Vec2::new(TILE_SIZE as f32, TILE_SIZE as f32), + 7, + 1, + None, + None, + )); + + Self { texture, layout } + } +} + +#[derive(Bundle)] +pub struct Pot { + sprite_sheet: SpriteSheetBundle, + position: Position, +} + +impl Pot { + pub fn new(position: Position, material: &Material) -> Self { + Self { + sprite_sheet: SpriteSheetBundle { + atlas: TextureAtlas { + layout: material.layout.clone(), + index: 0, + }, + transform: Transform { + scale: Vec3::new(0.8, 0.8, 1.0), + ..Default::default() + }, + texture: material.texture.clone(), + ..Default::default() + }, + position, + } + } +} + +fn render(mut query: Query<(&mut Transform, &Position)>) { + for (mut t, p) in query.iter_mut() { + t.translation = Vec3::new( + (p.x * TILE_SIZE as i32) as f32, + (p.y * TILE_SIZE as i32) as f32, + 1.0, + ); + } +} + +pub struct PotPlugin; + +impl Plugin for PotPlugin { + fn build(&self, app: &mut App) { + app.init_resource::().add_systems(Update, render); + } +}