-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
66 additions
and
3 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
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 |
---|---|---|
@@ -1,9 +1,48 @@ | ||
use crate::prelude::*; | ||
use bevy::prelude::*; | ||
|
||
#[derive(Component)] | ||
pub struct PlayerMarker; | ||
|
||
#[derive(Event)] | ||
pub struct PlayerTakeover { | ||
pub player: Entity, | ||
pub target: Entity, | ||
} | ||
|
||
fn player_takeover_system( | ||
mut commands: Commands, | ||
mut events: EventReader<PlayerTakeover>, | ||
ai_query: Query<Entity, With<AiMarker>>, | ||
child_query: Query<&Children>, | ||
mut ship_query: Query<(&mut ShipInput, &mut AttackRequest)>, | ||
parent_query: Query<&Parent>, | ||
) { | ||
for PlayerTakeover { player, target } in events.read() { | ||
if let Ok(parent) = parent_query.get(*player) { | ||
if let Ok((mut ship_input, mut attack_request)) = ship_query.get_mut(parent.get()) { | ||
*ship_input = default(); | ||
*attack_request = default(); | ||
} | ||
} | ||
let Ok(children) = child_query.get(*target) else { | ||
continue; | ||
}; | ||
|
||
for child in children.iter() { | ||
if !ai_query.contains(*child) { | ||
continue; | ||
} | ||
commands.entity(*child).despawn_recursive(); | ||
} | ||
commands.entity(*target).clear_children().add_child(*player); | ||
} | ||
} | ||
|
||
pub struct PlayerPlugin; | ||
impl Plugin for PlayerPlugin { | ||
fn build(&self, app: &mut App) {} | ||
fn build(&self, app: &mut App) { | ||
app.add_event::<PlayerTakeover>() | ||
.add_systems(PostUpdate, player_takeover_system); | ||
} | ||
} |