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

sprite sheet generators and example #7

Merged
merged 2 commits into from
Nov 30, 2023
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ cargo run --example <example> --features=bevy/default --release
- [gems](https://opengameart.org/content/gems-set-01)
- [parallax](https://ansimuz.itch.io/mountain-dusk-parallax-background)
- tilemap - [Cute Forest](https://aamatniekss.itch.io/free-pixelart-tileset-cute-forest) and [Ocean Background](https://opengameart.org/content/ocean-background)

- sprite_sheet - generated with https://old-flick.itch.io/sprite-randomizer

---
### License
Expand Down
Binary file added assets/sprite_sheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions examples/sprite_sheet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::collections::VecDeque;

use bevy::{prelude::*, window::PrimaryWindow};
use bevy_scroller::{
Scroller, ScrollerBundle, ScrollerPlugin, ScrollerSize, SequenceSpriteSheetGenerator,
};
fn main() {
let mut app = App::new();
app
.add_plugins((DefaultPlugins, ScrollerPlugin))
.add_systems(Startup, startup);
#[cfg(feature = "dev")]
{
use bevy_editor_pls::EditorPlugin;
app.add_plugins(EditorPlugin::default());
}
app.run();
}

fn startup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
windows: Query<&Window, With<PrimaryWindow>>,
) {
let primary_window = windows.get_single().expect("no primary window");
commands.spawn(Camera2dBundle::default());

let texture_handle = asset_server.load("sprite_sheet.png");
let sprite_size = Vec2::new(64., 64.);
let texture_atlas = TextureAtlas::from_grid(texture_handle, sprite_size, 10, 10, None, None);
let texture_atlas_handle = texture_atlases.add(texture_atlas);

commands.spawn((
ScrollerSize {
size: Vec2::new(primary_window.width(), sprite_size.y * 2.),
},
ScrollerBundle {
scroller: Scroller {
speed: 1.,
render_layer: Some(1),
..default()
},
generator: SequenceSpriteSheetGenerator {
sprites: VecDeque::from_iter(0..100),
texture_atlas: texture_atlas_handle,
},
..default()
},
));
}
2 changes: 1 addition & 1 deletion src/generators/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy::prelude::*;

use crate::Scroller;

pub trait GeneratedItem {
pub trait GeneratedItem: Debug {
fn size(&self) -> Vec2;
}
pub trait ScrollerGenerator: Default {
Expand Down
2 changes: 2 additions & 0 deletions src/generators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod generator;
#[cfg(feature = "poisson")]
mod poisson;
mod sprite;
mod sprite_sheet;

pub use generator::{pre_generator, GeneratedItem, ScrollerGenerator, SpawnerInput};
#[cfg(feature = "poisson")]
Expand All @@ -10,3 +11,4 @@ pub use sprite::{
sprite_spawner, RandomSequenceSpriteGenerator, SequenceSpriteGenerator, SingleSpriteGenerator,
SpriteScrollerItem,
};
pub use sprite_sheet::*;
55 changes: 55 additions & 0 deletions src/generators/sprite_sheet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::collections::VecDeque;

use bevy::prelude::*;

use crate::{GeneratedItem, Scroller, ScrollerGenerator, ScrollerItem};

#[derive(Debug)]
pub struct SpriteSheetScrollerItem {
sprite: u32,
}

impl GeneratedItem for SpriteSheetScrollerItem {
fn size(&self) -> Vec2 {
Vec2::new(64., 64.) * 2.
}
}

#[derive(Default, Component, Clone)]
pub struct SequenceSpriteSheetGenerator {
pub texture_atlas: Handle<TextureAtlas>,
pub sprites: VecDeque<u32>,
}

impl ScrollerGenerator for SequenceSpriteSheetGenerator {
type I = SpriteSheetScrollerItem;
fn gen_item(&mut self) -> Self::I {
self.sprites.rotate_left(1);

SpriteSheetScrollerItem {
sprite: *self.sprites.front().unwrap(),
}
}
}
pub fn spritesheet_spawner(
In(input): In<Vec<(Entity, Scroller, Box<SpriteSheetScrollerItem>)>>,
q_gen: Query<&SequenceSpriteSheetGenerator>,
mut commands: Commands,
) {
input.into_iter().for_each(|(entity, _, item)| {
let generator = q_gen.get(entity).unwrap();
commands.spawn((
ScrollerItem {
size: item.size(),
parent: entity,
},
SpriteSheetBundle {
visibility: Visibility::Hidden,
texture_atlas: generator.texture_atlas.clone(),
sprite: TextureAtlasSprite::new(item.sprite as usize),
transform: Transform::from_scale(Vec3::splat(2.0)),
..default()
},
));
})
}
5 changes: 3 additions & 2 deletions src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub struct ScrollerPlugin;

use crate::{
scroller::*, sprite_spawner, RandomSequenceSpriteGenerator, ScrollerApp, SequenceSpriteGenerator,
SingleSpriteGenerator,
scroller::*, sprite_spawner, spritesheet_spawner, RandomSequenceSpriteGenerator, ScrollerApp,
SequenceSpriteGenerator, SequenceSpriteSheetGenerator, SingleSpriteGenerator,
};
use bevy::prelude::*;

Expand All @@ -20,6 +20,7 @@ impl Plugin for ScrollerPlugin {
.register_type::<Vec<Entity>>()
.add_scroller_generator::<SingleSpriteGenerator, _, _>(sprite_spawner)
.add_scroller_generator::<SequenceSpriteGenerator, _, _>(sprite_spawner)
.add_scroller_generator::<SequenceSpriteSheetGenerator, _, _>(spritesheet_spawner)
.add_scroller_generator::<RandomSequenceSpriteGenerator, _, _>(sprite_spawner)
.add_systems(PreUpdate, (init, on_items_added).chain())
.add_systems(
Expand Down