Skip to content

Commit

Permalink
WIP: droid takeover works mostly
Browse files Browse the repository at this point in the history
  • Loading branch information
sim82 committed Dec 26, 2023
1 parent 6782ffe commit b17cdcb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/collision.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::{
droid::DroidOverloadMarker, particle::ColorGenerator, player::PlayerMarker, prelude::*,
ship::ShipMarker, weapon::Projectile,
droid::DroidOverloadMarker,
particle::ColorGenerator,
player::{PlayerMarker, PlayerTakeover},
prelude::*,
ship::ShipMarker,
weapon::Projectile,
};
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
Expand Down Expand Up @@ -57,6 +61,7 @@ fn collision_droid_takeover_system(
player_query: Query<&Parent, With<PlayerMarker>>,
ship_query: Query<(Entity, &Children), With<ShipMarker>>,
droid_query: Query<Entity, With<DroidOverloadMarker>>,
mut event_writer: EventWriter<PlayerTakeover>,
) {
for collision_event in collision_events.read() {
if let CollisionEvent::Started(a, b, _) = collision_event {
Expand All @@ -70,11 +75,23 @@ fn collision_droid_takeover_system(
if !children.iter().any(|child| player_query.contains(*child)) {
continue;
}
// let Ok(player) = player_query.get()
let Some(player) = children
.iter()
.cloned()
.find(|child| player_query.contains(*child))
else {
continue;
};
let Ok(droid) = droid_query.get(*a).or_else(|_| droid_query.get(*b)) else {
continue;
};

info!("take over droid {:?} {:?}", ship, droid);
event_writer.send(PlayerTakeover {
player,
target: droid,
})
// TODO
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/droid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub mod ai;
// const FORCE_MULTIPLIER: f32 = 4000.0;
pub const IMPULSE_MULTIPLIER: f32 = 8.0;
pub const RELOAD_TIMEOUT: f32 = 1.0;
#[derive(Component, Default)]
pub struct AiMarker;

#[derive(Component, Default)]
pub struct DroidMarker;
Expand Down Expand Up @@ -246,6 +248,7 @@ pub struct AiDroidBundle {
enemy_evaluation: EnemyEvaluation,
primary_enemy: PrimaryEnemy,
spatial_bundle: SpatialBundle,
ai_marker: AiMarker,
}

impl AiDroidBundle {
Expand All @@ -255,6 +258,7 @@ impl AiDroidBundle {
enemy_evaluation: EnemyEvaluation::default(),
primary_enemy: PrimaryEnemy { enemy },
spatial_bundle: default(),
ai_marker: default(),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ pub fn toggle_on_esc_system(
pub mod prelude {
pub use crate::collision::CollisionFxType;
pub use crate::colors::*;
pub use crate::droid::AiMarker;
pub use crate::droid::AttackRequest;
pub use crate::particle::{ParticleDirection, ParticleSource};
pub use crate::ship::ShipInput;
pub use crate::state::{GameDespawn, GameState};
pub use crate::tiles::{TileCache, TilePos, TileType, TilesState};
pub use crate::tunables::default_stroke;
Expand Down
41 changes: 40 additions & 1 deletion src/player.rs
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);
}
}

0 comments on commit b17cdcb

Please sign in to comment.