Created
August 19, 2020 03:21
-
-
Save DGriffin91/b592d4ce3ca8ea99204c9ba9a057b74c to your computer and use it in GitHub Desktop.
Despawn panic test
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
use bevy::prelude::*; | |
pub struct Game; | |
struct Box(u8); | |
struct AddBoxEventTriggerState { | |
event_timer: Timer, | |
} | |
impl Default for AddBoxEventTriggerState { | |
fn default() -> Self { | |
AddBoxEventTriggerState { | |
event_timer: Timer::from_seconds(1.0), | |
} | |
} | |
} | |
struct AddBoxEvent(); | |
#[derive(Default)] | |
struct AddBoxEventListenerState { | |
event_reader: EventReader<AddBoxEvent>, | |
} | |
// sends MyEvent every second | |
fn add_box_event_trigger( | |
time: Res<Time>, | |
mut state: ResMut<AddBoxEventTriggerState>, | |
mut my_events: ResMut<Events<AddBoxEvent>>, | |
) { | |
state.event_timer.tick(time.delta_seconds); | |
if state.event_timer.finished { | |
my_events.send(AddBoxEvent()); | |
state.event_timer.reset(); | |
} | |
} | |
fn setup(mut commands: Commands) { | |
commands | |
.spawn(LightComponents { | |
translation: Translation::new(4.0, 8.0, 4.0), | |
..Default::default() | |
}) | |
.spawn(Camera3dComponents { | |
transform: Transform::new_sync_disabled(Mat4::face_toward( | |
Vec3::new(0.0, 20.0, 5.0), | |
Vec3::new(0.0, 10.0, 0.0), | |
Vec3::new(0.0, 1.0, 0.0), | |
)), | |
..Default::default() | |
}); | |
} | |
fn add_box( | |
mut commands: Commands, | |
mut meshes: ResMut<Assets<Mesh>>, | |
mut materials: ResMut<Assets<StandardMaterial>>, | |
mut state: ResMut<AddBoxEventListenerState>, | |
events: Res<Events<AddBoxEvent>>, | |
) { | |
for _event in state.event_reader.iter(&events) { | |
commands | |
.spawn(PbrComponents { | |
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), | |
material: materials.add(Color::rgb(0.9, 0.4, 0.3).into()), | |
translation: Translation(Vec3::new(0.0, 0.0, -20.0)), | |
..Default::default() | |
}) | |
.with(Box(0)); | |
println!("Spawn Box"); | |
} | |
} | |
fn box_movement( | |
mut commands: Commands, | |
time: Res<Time>, | |
mut query: Query<(Entity, &Box, &mut Translation)>, | |
) { | |
for (entity, _box, mut translation) in &mut query.iter() { | |
if *translation.0.z_mut() > 1.0 { | |
println!("Despawn Box"); | |
commands.despawn(entity); | |
} else { | |
*translation.0.z_mut() += time.delta_seconds * 10.0; | |
} | |
} | |
} | |
impl Plugin for Game { | |
fn build(&self, app: &mut AppBuilder) { | |
app.add_startup_system(setup.system()) | |
.add_system(box_movement.system()) | |
.add_event::<AddBoxEvent>() | |
.init_resource::<AddBoxEventTriggerState>() | |
.init_resource::<AddBoxEventListenerState>() | |
.add_system(add_box_event_trigger.system()) | |
.add_system(add_box.system()); | |
} | |
} | |
fn main() { | |
App::build() | |
.add_resource(Msaa { samples: 4 }) | |
.add_default_plugins() | |
.add_plugin(Game) | |
.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment