-
Notifications
You must be signed in to change notification settings - Fork 4
Add PxFlip for sprite and tile. #8
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
Open
shanecelis
wants to merge
1
commit into
Seldom-SE:main
Choose a base branch
from
shanecelis:add-flip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // In this program, a sprite can be flipped with X and Y keys. | ||
|
|
||
| use bevy::prelude::*; | ||
| use seldom_pixel::prelude::*; | ||
|
|
||
| fn main() { | ||
| App::new() | ||
| .add_plugins(( | ||
| DefaultPlugins.set(WindowPlugin { | ||
| primary_window: Some(Window { | ||
| resolution: Vec2::splat(512.).into(), | ||
| ..default() | ||
| }), | ||
| ..default() | ||
| }), | ||
| PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png"), | ||
| )) | ||
| .insert_resource(ClearColor(Color::BLACK)) | ||
| .add_systems(Startup, init) | ||
| .add_systems(Update, on_key) | ||
| .run(); | ||
| } | ||
|
|
||
| fn init(assets: Res<AssetServer>, mut commands: Commands) { | ||
| commands.spawn(Camera2d); | ||
|
|
||
| // Spawn a sprite | ||
| commands.spawn(( | ||
| PxSprite(assets.load("sprite/mage.px_sprite.png")), | ||
| PxPosition(IVec2::splat(8)), | ||
| PxFlip::default(), | ||
| )); | ||
| } | ||
|
|
||
| fn on_key(input: Res<ButtonInput<KeyCode>>, mut query: Query<&mut PxFlip>) { | ||
| if input.just_pressed(KeyCode::KeyX) { | ||
| for mut flip in &mut query { | ||
| flip.x = !flip.x; | ||
| } | ||
| } | ||
|
|
||
| if input.just_pressed(KeyCode::KeyY) { | ||
| for mut flip in &mut query { | ||
| flip.y = !flip.y; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[px_layer] | ||
| struct Layer; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // In this program, a tilemap can be flipped with x and y keys. | ||
|
|
||
| use bevy::prelude::*; | ||
| use rand::{thread_rng, Rng}; | ||
| use seldom_pixel::prelude::*; | ||
|
|
||
| fn main() { | ||
| App::new() | ||
| .add_plugins(( | ||
| DefaultPlugins.set(WindowPlugin { | ||
| primary_window: Some(Window { | ||
| resolution: Vec2::splat(512.).into(), | ||
| ..default() | ||
| }), | ||
| ..default() | ||
| }), | ||
| PxPlugin::<Layer>::new(UVec2::splat(16), "palette/palette_1.palette.png"), | ||
| )) | ||
| .insert_resource(ClearColor(Color::BLACK)) | ||
| .add_systems(Startup, init) | ||
| .add_systems(Update, on_key) | ||
| .run(); | ||
| } | ||
|
|
||
| fn init(assets: Res<AssetServer>, mut commands: Commands) { | ||
| commands.spawn(Camera2d); | ||
|
|
||
| let mut tiles = PxTiles::new(UVec2::splat(4)); | ||
| let mut rng = thread_rng(); | ||
|
|
||
| for x in 0..4 { | ||
| for y in 0..4 { | ||
| tiles.set( | ||
| Some(commands.spawn((PxTile::from(rng.gen_range(0..4)), | ||
| PxFlip::default())).id()), | ||
| UVec2::new(x, y), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Spawn the map | ||
| commands.spawn(PxMap { | ||
| tiles, | ||
| tileset: assets.load("tileset/tileset.px_tileset.png"), | ||
| }); | ||
| } | ||
|
|
||
| fn on_key(input: Res<ButtonInput<KeyCode>>, mut query: Query<&mut PxFlip>) { | ||
| if input.just_pressed(KeyCode::KeyX) { | ||
| for mut flip in &mut query { | ||
| flip.x = !flip.x; | ||
| } | ||
| } | ||
|
|
||
| if input.just_pressed(KeyCode::KeyY) { | ||
| for mut flip in &mut query { | ||
| flip.y = !flip.y; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[px_layer] | ||
| struct Layer; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,31 +108,38 @@ impl RenderAsset for PxSpriteAsset { | |
| } | ||
|
|
||
| impl Animation for PxSpriteAsset { | ||
| type Param = (); | ||
| type Param = PxFlip; | ||
|
|
||
| fn frame_count(&self) -> usize { | ||
| self.data.area() / self.frame_size | ||
| } | ||
|
|
||
| fn draw( | ||
| &self, | ||
| _: (), | ||
| flip: PxFlip, | ||
| image: &mut PxImageSliceMut<impl Pixel>, | ||
| frame: impl Fn(UVec2) -> usize, | ||
| filter: impl Fn(u8) -> u8, | ||
| ) { | ||
| let width = self.data.width(); | ||
| let image_width = image.image_width(); | ||
| image.for_each_mut(|slice_i, image_i, pixel| { | ||
| if let Some(Some(value)) = self.data.get_pixel(IVec2::new( | ||
| let mut v = IVec2::new( | ||
| (slice_i % width) as i32, | ||
| ((frame(UVec2::new( | ||
| (image_i % image_width) as u32, | ||
| (image_i / image_width) as u32, | ||
| )) * self.frame_size | ||
| + slice_i) | ||
| / width) as i32, | ||
| )) { | ||
| ); | ||
| if flip.x { | ||
| v.x = width as i32 - v.x - 1; | ||
| } | ||
| if flip.y { | ||
| v.y = self.data.height() as i32 - v.y - 1; | ||
| } | ||
| if let Some(Some(value)) = self.data.get_pixel(v) { | ||
| pixel.set_value(filter(value)); | ||
| } | ||
| }); | ||
|
|
@@ -148,6 +155,15 @@ impl Spatial for PxSpriteAsset { | |
| } | ||
| } | ||
|
|
||
| /// Flips the sprite or tile on the x-axis, y-axis, or both. | ||
| #[derive(Component, Default, Clone, Copy, Debug)] | ||
| pub struct PxFlip { | ||
| /// If true, flips the x-axis. | ||
| pub x: bool, | ||
| /// If true, flips the y-axis. | ||
| pub y: bool, | ||
| } | ||
|
|
||
| /// A sprite | ||
| #[derive(Component, Deref, DerefMut, Default, Clone, Debug)] | ||
| #[require(PxPosition, PxAnchor, DefaultLayer, PxCanvas, Visibility)] | ||
|
|
@@ -400,14 +416,15 @@ pub(crate) type SpriteComponents<L> = ( | |
| &'static PxCanvas, | ||
| Option<&'static PxAnimation>, | ||
| Option<&'static PxFilter>, | ||
| Option<&'static PxFlip>, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as earlier comment, but for |
||
| ); | ||
|
|
||
| fn extract_sprites<L: PxLayer>( | ||
| // TODO Maybe calculate `ViewVisibility` | ||
| sprites: Extract<Query<(SpriteComponents<L>, &InheritedVisibility, RenderEntity)>>, | ||
| mut cmd: Commands, | ||
| ) { | ||
| for ((sprite, &position, &anchor, layer, &canvas, animation, filter), visibility, id) in | ||
| for ((sprite, &position, &anchor, layer, &canvas, animation, filter, flip), visibility, id) in | ||
| &sprites | ||
| { | ||
| if !visibility.get() { | ||
|
|
@@ -428,6 +445,12 @@ fn extract_sprites<L: PxLayer>( | |
| } else { | ||
| entity.remove::<PxFilter>(); | ||
| } | ||
|
|
||
| if let Some(flip) = flip { | ||
| entity.insert(flip.clone()); | ||
| } else { | ||
| entity.remove::<PxFlip>(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this non-optional and add
PxFlipas a required component onPxTileThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can do.