-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from ettoreleandrotognoli/issue-27
avoid to despawn already removed entities
- Loading branch information
Showing
4 changed files
with
150 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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 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,124 @@ | ||
use bevy::prelude::*; | ||
use bevy_parallax::{ | ||
CreateParallaxEvent, LayerData, LayerSpeed, ParallaxCameraComponent, ParallaxMoveEvent, | ||
ParallaxPlugin, ParallaxSystems, | ||
}; | ||
|
||
fn main() { | ||
// Define window | ||
let primary_window = Window { | ||
title: "Cyberpunk".to_string(), | ||
resolution: (1280.0, 720.0).into(), | ||
resizable: false, | ||
..Default::default() | ||
}; | ||
|
||
App::new() | ||
.add_plugins( | ||
DefaultPlugins | ||
.set(WindowPlugin { | ||
primary_window: Some(primary_window), | ||
..default() | ||
}) | ||
// Use nearest filtering so our pixel art renders clear | ||
.set(ImagePlugin::default_nearest()), | ||
) | ||
.add_plugins(ParallaxPlugin) | ||
.add_systems(Startup, initialize_camera_system) | ||
.add_systems(Update, move_camera_system.before(ParallaxSystems)) | ||
.add_systems(Update, (reload_system, despawn_all)) | ||
.run(); | ||
} | ||
|
||
pub fn new_create_parallax_event(camera: Entity) -> CreateParallaxEvent { | ||
CreateParallaxEvent { | ||
layers_data: vec![ | ||
LayerData { | ||
speed: LayerSpeed::Horizontal(0.9), | ||
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() | ||
}, | ||
LayerData { | ||
speed: LayerSpeed::Horizontal(0.6), | ||
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() | ||
}, | ||
LayerData { | ||
speed: LayerSpeed::Horizontal(0.1), | ||
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() | ||
}, | ||
], | ||
camera: camera, | ||
} | ||
} | ||
|
||
// Put a ParallaxCameraComponent on the camera used for parallax | ||
pub fn initialize_camera_system( | ||
mut commands: Commands, | ||
mut create_parallax: EventWriter<CreateParallaxEvent>, | ||
) { | ||
let camera = commands | ||
.spawn(Camera2dBundle::default()) | ||
.insert(ParallaxCameraComponent::default()) | ||
.id(); | ||
create_parallax.send(new_create_parallax_event(camera)); | ||
} | ||
|
||
pub fn reload_system( | ||
keyboard_input: Res<Input<KeyCode>>, | ||
camera_query: Query<Entity, With<Camera>>, | ||
mut create_parallax: EventWriter<CreateParallaxEvent>, | ||
) { | ||
let camera = camera_query.get_single().unwrap(); | ||
if keyboard_input.just_released(KeyCode::R) { | ||
create_parallax.send(new_create_parallax_event(camera)) | ||
} | ||
} | ||
|
||
pub fn despawn_all( | ||
mut commands: Commands, | ||
keyboard_input: Res<Input<KeyCode>>, | ||
all_query: Query<Entity, (Without<Camera>, Without<Window>)>, | ||
) { | ||
if keyboard_input.just_released(KeyCode::Q) { | ||
for entity in all_query.iter() { | ||
commands.entity(entity).despawn(); | ||
} | ||
} | ||
} | ||
|
||
// Send a ParallaxMoveEvent with the desired camera movement speed | ||
pub fn move_camera_system( | ||
keyboard_input: Res<Input<KeyCode>>, | ||
mut move_event_writer: EventWriter<ParallaxMoveEvent>, | ||
camera_query: Query<Entity, With<Camera>>, | ||
) { | ||
let camera = camera_query.get_single().unwrap(); | ||
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, | ||
}); | ||
} | ||
} |
This file contains 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 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