Skip to content

Commit

Permalink
Merge pull request #31 from ettoreleandrotognoli/issue-26
Browse files Browse the repository at this point in the history
add repeat option to layer
  • Loading branch information
ettoreleandrotognoli authored Sep 26, 2023
2 parents 3364c9a + 76e7761 commit 12725f6
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 107 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ features = [
]

[dev-dependencies]
# Temporarily removed until it updates to Bevy 0.8
# bevy-inspector-egui = "0.11.0"
bevy-inspector-egui = "0.19.0"
ron = "0.8.0"

[dev-dependencies.bevy]
Expand Down
6 changes: 0 additions & 6 deletions data/fishy_layer_data.ron
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
rows: 1,
scale: 1.25,
z: 0.0,
transition_factor: 1.2,
color: Rgba(
red: 0.5,
green: 0.8,
Expand All @@ -23,7 +22,6 @@
rows: 1,
scale: 1.25,
z: 1.0,
transition_factor: 1.2,
color: Rgba(
red: 0.5,
green: 0.8,
Expand All @@ -39,7 +37,6 @@
rows: 1,
scale: 1.25,
z: 2.0,
transition_factor: 1.2,
color: Rgba(
red: 0.7,
green: 0.6,
Expand All @@ -55,7 +52,6 @@
rows: 1,
scale: 1.25,
z: 3.0,
transition_factor: 1.2,
color: Rgba(
red: 0.7,
green: 0.6,
Expand All @@ -71,7 +67,6 @@
rows: 1,
scale: 1.25,
z: 4.0,
transition_factor: 1.2,
color: Rgba(
red: 0.7,
green: 0.6,
Expand All @@ -87,7 +82,6 @@
rows: 1,
scale: 1.25,
z: 5.0,
transition_factor: 1.2,
color: Rgba(
red: 0.7,
green: 0.6,
Expand Down
57 changes: 38 additions & 19 deletions examples/cyberpunk.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use bevy::prelude::*;
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use bevy_parallax::{
CreateParallaxEvent, LayerData, LayerSpeed, ParallaxCameraComponent, ParallaxMoveEvent,
ParallaxPlugin, ParallaxSystems,
CreateParallaxEvent, LayerData, LayerRepeat, LayerSpeed, ParallaxCameraComponent,
ParallaxMoveEvent, ParallaxPlugin, ParallaxSystems, RepeatStrategy,
};

fn main() {
Expand All @@ -24,8 +25,10 @@ fn main() {
.set(ImagePlugin::default_nearest()),
)
.add_plugins(ParallaxPlugin)
.add_plugins(WorldInspectorPlugin::new())
.add_systems(Startup, initialize_camera_system)
.add_systems(Update, move_camera_system.before(ParallaxSystems))
.insert_resource(ClearColor(Color::rgb_u8(42, 0, 63)))
.run();
}

Expand All @@ -41,34 +44,37 @@ pub fn initialize_camera_system(
create_parallax.send(CreateParallaxEvent {
layers_data: vec![
LayerData {
speed: LayerSpeed::Horizontal(0.9),
speed: LayerSpeed::Bidirectional(0.9, 0.9),
repeat: LayerRepeat::horizontally(RepeatStrategy::Same),
path: "cyberpunk_back.png".to_string(),
tile_size: Vec2::new(96.0, 160.0),
cols: 1,
rows: 1,
scale: 4.5,
z: 0.0,
..Default::default()
..default()
},
LayerData {
speed: LayerSpeed::Horizontal(0.6),
speed: LayerSpeed::Bidirectional(0.6, 0.8),
repeat: LayerRepeat::horizontally(RepeatStrategy::Same),
path: "cyberpunk_middle.png".to_string(),
tile_size: Vec2::new(144.0, 160.0),
cols: 1,
rows: 1,
scale: 4.5,
z: 1.0,
..Default::default()
..default()
},
LayerData {
speed: LayerSpeed::Horizontal(0.1),
speed: LayerSpeed::Bidirectional(0.1, 0.3),
repeat: LayerRepeat::both(RepeatStrategy::Mirror),
path: "cyberpunk_front.png".to_string(),
tile_size: Vec2::new(272.0, 160.0),
cols: 1,
rows: 1,
scale: 4.5,
z: 2.0,
..Default::default()
..default()
},
],
camera: camera,
Expand All @@ -79,18 +85,31 @@ pub fn initialize_camera_system(
pub fn move_camera_system(
keyboard_input: Res<Input<KeyCode>>,
mut move_event_writer: EventWriter<ParallaxMoveEvent>,
camera_query: Query<Entity, With<Camera>>,
mut camera_query: Query<(Entity, &mut Transform), With<Camera>>,
) {
let camera = camera_query.get_single().unwrap();
let (camera, mut camera_transform) = camera_query.get_single_mut().unwrap();
let speed = 9.;
let mut direction = Vec2::ZERO;
if keyboard_input.pressed(KeyCode::D) || keyboard_input.pressed(KeyCode::Right) {
move_event_writer.send(ParallaxMoveEvent {
camera_move_speed: Vec2::new(3.0, 0.0),
camera: camera,
});
} else if keyboard_input.pressed(KeyCode::A) || keyboard_input.pressed(KeyCode::Left) {
move_event_writer.send(ParallaxMoveEvent {
camera_move_speed: Vec2::new(-3.0, 0.0),
camera: camera,
});
direction += Vec2::new(1.0, 0.0);
}
if keyboard_input.pressed(KeyCode::A) || keyboard_input.pressed(KeyCode::Left) {
direction += Vec2::new(-1.0, 0.0);
}
if keyboard_input.pressed(KeyCode::W) || keyboard_input.pressed(KeyCode::Up) {
direction += Vec2::new(0.0, 1.0);
}
if keyboard_input.pressed(KeyCode::S) || keyboard_input.pressed(KeyCode::Down) {
direction += Vec2::new(0.0, -1.0);
}
if keyboard_input.pressed(KeyCode::E) {
camera_transform.rotate_z(0.1);
}
if keyboard_input.pressed(KeyCode::Q) {
camera_transform.rotate_z(-0.1);
}
move_event_writer.send(ParallaxMoveEvent {
camera_move_speed: direction.normalize_or_zero() * speed,
camera: camera,
});
}
7 changes: 5 additions & 2 deletions examples/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use bevy::{
render::{camera::Viewport, view::RenderLayers},
};
use bevy_parallax::{
CreateParallaxEvent, LayerData, LayerSpeed, ParallaxCameraComponent, ParallaxMoveEvent,
ParallaxPlugin, ParallaxSystems,
CreateParallaxEvent, LayerData, LayerSpeed, LayerRepeat, ParallaxCameraComponent, ParallaxMoveEvent,
ParallaxPlugin, ParallaxSystems, RepeatStrategy,
};

fn main() {
Expand Down Expand Up @@ -82,6 +82,7 @@ pub fn initialize_camera_system(
layers_data: vec![
LayerData {
speed: LayerSpeed::Horizontal(0.9),
repeat: LayerRepeat::horizontally(RepeatStrategy::Same),
path: "cyberpunk_back.png".to_string(),
tile_size: Vec2::new(96.0, 160.0),
cols: 1,
Expand All @@ -92,6 +93,7 @@ pub fn initialize_camera_system(
},
LayerData {
speed: LayerSpeed::Horizontal(0.6),
repeat: LayerRepeat::horizontally(RepeatStrategy::Same),
path: "cyberpunk_middle.png".to_string(),
tile_size: Vec2::new(144.0, 160.0),
cols: 1,
Expand All @@ -102,6 +104,7 @@ pub fn initialize_camera_system(
},
LayerData {
speed: LayerSpeed::Horizontal(0.1),
repeat: LayerRepeat::horizontally(RepeatStrategy::Same),
path: "cyberpunk_front.png".to_string(),
tile_size: Vec2::new(272.0, 160.0),
cols: 1,
Expand Down
88 changes: 82 additions & 6 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,91 @@ pub enum LayerSpeed {
Bidirectional(f32, f32),
}

#[derive(Debug, Deserialize, Clone)]
pub enum RepeatStrategy {
Same,
Mirror,
}

impl RepeatStrategy {
pub fn transform(
&self,
mut spritesheet_bundle: SpriteSheetBundle,
pos: (i32, i32),
) -> SpriteSheetBundle {
match self {
Self::Same => spritesheet_bundle,
Self::Mirror => {
let (x, y) = pos;
spritesheet_bundle.sprite.flip_x = x % 2 != 0;
spritesheet_bundle.sprite.flip_y = y % 2 != 0;
spritesheet_bundle
}
}
}
}

#[derive(Debug, Deserialize, Clone)]
pub enum LayerRepeat {
Horizontal(RepeatStrategy),
Vertical(RepeatStrategy),
Bidirectional(RepeatStrategy, RepeatStrategy),
}

impl LayerRepeat {
pub fn both(strategy: RepeatStrategy) -> Self {
Self::Bidirectional(strategy.clone(), strategy)
}

pub fn horizontally(strategy: RepeatStrategy) -> Self {
Self::Horizontal(strategy)
}

pub fn vertically(strategy: RepeatStrategy) -> Self {
Self::Vertical(strategy)
}

pub fn has_vertical(&self) -> bool {
match self {
Self::Horizontal(_) => false,
_ => true,
}
}

pub fn has_horizontal(&self) -> bool {
match self {
Self::Vertical(_) => false,
_ => true,
}
}

pub fn get_horizontal_strategy(&self) -> RepeatStrategy {
match self {
Self::Horizontal(strategy) => strategy.clone(),
Self::Bidirectional(strategy, _) => strategy.clone(),
_ => RepeatStrategy::Same,
}
}

pub fn get_vertical_strategy(&self) -> RepeatStrategy {
match self {
Self::Vertical(strategy) => strategy.clone(),
Self::Bidirectional(_, strategy) => strategy.clone(),
_ => RepeatStrategy::Same,
}
}
}

/// Layer initialization data
#[derive(Debug, Deserialize, Resource)]
#[serde(default)]
pub struct LayerData {
/// Relative speed of layer to the camera movement.
/// If the speed value is set to 1.0, the layer won't move in that direction.
pub speed: LayerSpeed,

pub repeat: LayerRepeat,

/// Path to layer texture file
pub path: String,
/// Size of a tile of the texture
Expand All @@ -32,8 +110,6 @@ pub struct LayerData {
pub z: f32,
/// Default initial position of the Entity container
pub position: Vec2,
/// Number used to determine when textures are moved to opposite side of camera
pub transition_factor: f32,

pub color: Color,
}
Expand All @@ -42,14 +118,14 @@ impl Default for LayerData {
fn default() -> Self {
Self {
speed: LayerSpeed::Horizontal(1.0),
repeat: LayerRepeat::Bidirectional(RepeatStrategy::Same, RepeatStrategy::Same),
path: "".to_string(),
tile_size: Vec2::ZERO,
cols: 1,
rows: 1,
scale: 1.0,
z: 0.0,
position: Vec2::ZERO,
transition_factor: 1.2,
color: Color::WHITE,
}
}
Expand All @@ -60,12 +136,12 @@ impl Default for LayerData {
pub struct LayerComponent {
/// Relative speed of layer to the camera movement
pub speed: Vec2,
///
pub repeat: LayerRepeat,
/// Number of rows (x) and columns (y) with the textures in the layer
pub texture_count: Vec2,
/// Number used to determine when textures are moved to opposite side of camera
pub transition_factor: f32,

pub camera: Entity
pub camera: Entity,
}

/// Core component for layer texture
Expand Down
Loading

0 comments on commit 12725f6

Please sign in to comment.