This commit is contained in:
yuni 2024-05-13 01:08:16 +02:00
parent 48f78468c9
commit 6ab89615b7

View file

@ -45,17 +45,16 @@ impl Plugin for WorldPlugin {
app.insert_resource(AsteroidUpdateTimer( app.insert_resource(AsteroidUpdateTimer(
Timer::from_seconds(ASTEROID_UPDATE_INTERVAL, TimerMode::Repeating))); Timer::from_seconds(ASTEROID_UPDATE_INTERVAL, TimerMode::Repeating)));
app.insert_resource(ActiveAsteroids(HashMap::new())); app.insert_resource(ActiveAsteroids(HashMap::new()));
app.add_event::<DespawnEvent>(); app.add_event::<DespawnAsteroidEvent>();
app.add_event::<RespawnEvent>(); app.add_event::<RespawnEvent>();
} }
} }
#[derive(Resource)] struct AsteroidUpdateTimer(Timer); #[derive(Resource)] struct AsteroidUpdateTimer(Timer);
#[derive(Resource)] pub struct ActiveAsteroids(pub HashMap<I64Vec3, AsteroidData>); #[derive(Resource)] pub struct ActiveAsteroids(pub HashMap<I64Vec3, AsteroidData>);
#[derive(Component)] struct Asteroid; #[derive(Component)] struct Asteroid;
#[derive(Component)] pub struct Star;
#[derive(Component)] pub struct DespawnOnPlayerDeath; #[derive(Component)] pub struct DespawnOnPlayerDeath;
#[derive(Event)] pub struct RespawnEvent; #[derive(Event)] pub struct RespawnEvent;
pub struct AsteroidData { pub struct AsteroidData {
@ -64,15 +63,12 @@ pub struct AsteroidData {
} }
#[derive(Event)] #[derive(Event)]
pub struct DespawnEvent { pub struct DespawnAsteroidEvent {
entity: Entity, entity: Entity,
sceneinstance: InstanceId, sceneinstance: InstanceId,
origin: I64Vec3, origin: I64Vec3,
} }
#[derive(Component)]
pub struct Star;
pub fn setup( pub fn setup(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
@ -173,7 +169,7 @@ fn spawn_despawn_asteroids(
mut timer: ResMut<AsteroidUpdateTimer>, mut timer: ResMut<AsteroidUpdateTimer>,
mut commands: Commands, mut commands: Commands,
q_player: Query<&Position, With<actor::PlayerCamera>>, q_player: Query<&Position, With<actor::PlayerCamera>>,
mut ew_despawn: EventWriter<DespawnEvent>, mut ew_despawn: EventWriter<DespawnAsteroidEvent>,
mut db: ResMut<ActiveAsteroids>, mut db: ResMut<ActiveAsteroids>,
q_asteroid: Query<(&Position, &SceneInstance), With<Asteroid>>, q_asteroid: Query<(&Position, &SceneInstance), With<Asteroid>>,
mut last_player_cell: Local<I64Vec3>, mut last_player_cell: Local<I64Vec3>,
@ -221,7 +217,7 @@ fn spawn_despawn_asteroids(
{ {
if let Ok((pos, sceneinstance)) = q_asteroid.get(asteroid.entity) { if let Ok((pos, sceneinstance)) = q_asteroid.get(asteroid.entity) {
if pos.0.distance(player.0) > 1000.0 { if pos.0.distance(player.0) > 1000.0 {
ew_despawn.send(DespawnEvent { ew_despawn.send(DespawnAsteroidEvent {
entity: asteroid.entity, entity: asteroid.entity,
sceneinstance: **sceneinstance, sceneinstance: **sceneinstance,
origin: origin.clone(), origin: origin.clone(),
@ -337,7 +333,7 @@ fn spawn_despawn_asteroids(
fn handle_despawn( fn handle_despawn(
mut commands: Commands, mut commands: Commands,
mut er_despawn: EventReader<DespawnEvent>, mut er_despawn: EventReader<DespawnAsteroidEvent>,
mut db: ResMut<ActiveAsteroids>, mut db: ResMut<ActiveAsteroids>,
mut scene_spawner: ResMut<SceneSpawner>, mut scene_spawner: ResMut<SceneSpawner>,
) { ) {