Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static pot asset example #50

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added visualizer/assets/pot/sprite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 9 additions & 19 deletions visualizer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,20 @@ 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.),
..default()
});
}

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<AssetServer>) {
// commands.spawn(SpriteBundle {
// texture: asset_server.load("background.gif"),
// ..default()
// });
// }

fn main() {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();

Expand All @@ -42,9 +29,12 @@ fn main() {
.add_plugins(PanCamPlugin)
.add_plugins(EntropyPlugin::<WyRand>::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();
}
6 changes: 5 additions & 1 deletion visualizer/src/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,16 @@ fn spawn_assets(
}
}

fn spawn_pot(mut commands: Commands, pot_material: Res<crate::pot::Material>) {
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::<StreamEvent>()
.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()));
}
Expand Down
77 changes: 77 additions & 0 deletions visualizer/src/pot/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use bevy::prelude::*;

use crate::map::Position;

const TILE_SIZE: u32 = 64;

#[derive(Resource)]
pub struct Material {
texture: Handle<Image>,
layout: Handle<TextureAtlasLayout>,
}

impl FromWorld for Material {
fn from_world(world: &mut World) -> Self {
let texture: Handle<Image> = world
.get_resource::<AssetServer>()
.unwrap()
.load("pot/sprite.png");

let layout = world
.get_resource_mut::<Assets<TextureAtlasLayout>>()
.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::<Material>().add_systems(Update, render);
}
}
Loading