Skip to content

Commit

Permalink
basic droid takeover mechanic
Browse files Browse the repository at this point in the history
  • Loading branch information
sim82 committed Nov 22, 2023
1 parent d42f0a8 commit fefa01f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 28 deletions.
75 changes: 56 additions & 19 deletions src/collision.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
use crate::{particle::ColorGenerator, prelude::*, weapon::Projectile};
use crate::{
droid::DroidOverloadMarker,
particle::ColorGenerator,
player::{PlayerMarker, PlayerState},
prelude::*,
ship::ShipMarker,
weapon::Projectile,
};
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
use rand_distr::Normal;
Expand All @@ -7,11 +14,11 @@ use rand_distr::Normal;
pub enum CollisionFxType {
Spark,
}
fn display_events_system(mut collision_events: EventReader<CollisionEvent>) {
for collision_event in collision_events.read() {
info!("Received collision event: {:?}", collision_event);
}
}
// fn display_events_system(mut collision_events: EventReader<CollisionEvent>) {
// for collision_event in collision_events.read() {
// info!("Received collision event: {:?}", collision_event);
// }
// }

fn projectile_collision_system(
mut commands: Commands,
Expand Down Expand Up @@ -49,27 +56,57 @@ fn projectile_collision_system(
}
}

fn collision_fx_system(
_commands: Commands,
fn collision_droid_takeover_system(
mut collision_events: EventReader<CollisionEvent>,
collision_fx_query: Query<&CollisionFxType>,
mut player_state: ResMut<NextState<PlayerState>>,
ship_query: Query<Entity, (With<ShipMarker>, With<PlayerMarker>)>,
droid_query: Query<Entity, With<DroidOverloadMarker>>,
) {
for collision_event in collision_events.read() {
info!("collision event: {collision_event:?}");
match collision_event {
CollisionEvent::Started(a, b, _) => {
if collision_fx_query.contains(*a) && collision_fx_query.contains(*b) {
info!("spark {a:?} {b:?}");
}
}
CollisionEvent::Stopped(_, _, _) => (),
if let CollisionEvent::Started(a, b, _) = collision_event {
//
let Ok(_ship) = ship_query.get(*a).or_else(|_| ship_query.get(*b)) else {
continue;
};
let Ok(_droid) = droid_query.get(*a).or_else(|_| droid_query.get(*b)) else {
continue;
};

info!("take over droid");
player_state.set(PlayerState::Droid);
}
}
}

// fn collision_fx_system(
// _commands: Commands,
// mut collision_events: EventReader<CollisionEvent>,
// collision_fx_query: Query<&CollisionFxType>,
// ) {
// for collision_event in collision_events.read() {
// info!("collision event: {collision_event:?}");
// match collision_event {
// CollisionEvent::Started(a, b, _) => {
// if collision_fx_query.contains(*a) && collision_fx_query.contains(*b) {
// info!("spark {a:?} {b:?}");
// }
// }
// CollisionEvent::Stopped(_, _, _) => (),
// }
// }
// }

pub struct CollisionPlugin;
impl Plugin for CollisionPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, display_events_system)
.add_systems(Update, (projectile_collision_system, collision_fx_system));
app
// .add_systems(Update, display_events_system)
.add_systems(
Update,
(
projectile_collision_system,
collision_droid_takeover_system.run_if(in_state(PlayerState::Ship)), /*collision_fx_system*/
),
);
}
}
14 changes: 11 additions & 3 deletions src/droid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
use bevy::prelude::*;
use bevy_prototype_lyon::prelude::Stroke;
use bevy_rapier2d::prelude::*;
use rand::Rng;
use std::{borrow::Cow, time::Duration};

pub mod ai;
Expand Down Expand Up @@ -129,17 +130,24 @@ fn droid_attack_system(
fn droid_overload_system(
mut commands: Commands,
query: Query<(Entity, &DroidHealth), Without<DroidOverloadMarker>>,
mut query_overload: Query<(Entity, &mut DroidHealth), With<DroidOverloadMarker>>,
mut query_overload: Query<
(Entity, &mut DroidHealth, &mut Transform),
With<DroidOverloadMarker>,
>,
) {
//
for (entity, health) in &query {
if health.emp_load >= 1.0 {
commands.entity(entity).insert(DroidOverloadMarker);
}
}
for (entity, mut health) in &mut query_overload {
if health.emp_load <= 0.0 {
for (entity, mut health, mut transform) in &mut query_overload {
if health.emp_load <= 0.5 {
commands.entity(entity).remove::<DroidOverloadMarker>();
transform.scale = Vec3::ONE;
} else {
let mut rng = rand::thread_rng();
transform.scale = Vec3::splat(rng.gen_range(0.95..1.05));
}
health.emp_load = (health.emp_load - 0.005).max(0.0);
}
Expand Down
2 changes: 0 additions & 2 deletions src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ fn main_menu_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
color: TEXT_COLOR,
// color: colors::COLORS[1],
font: font.clone(),
..default()
};

commands
Expand Down Expand Up @@ -116,7 +115,6 @@ fn main_menu_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
font_size: 80.0,
color: TEXT_COLOR,
font,
..default()
},
)
.with_style(Style {
Expand Down
13 changes: 9 additions & 4 deletions src/player.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
camera::CameraTarget,
droid::{ai::new_shooting_droid_ai, DroidMarker},
droid::{ai::new_shooting_droid_ai, DroidHealth, DroidMarker},
input::InputTarget,
prelude::*,
ship::ShipMarker,
ship::{ShipInput, ShipMarker},
};
use bevy::prelude::*;
use big_brain::thinker::ThinkerBuilder;
Expand Down Expand Up @@ -54,6 +54,7 @@ fn enter_droid(
.entity(entity)
.insert(PrimaryPlayerBundle::default())
.insert(PlayerMarker)
.insert(DroidHealth::default()) // crappy way to instantly heal the taken over droid. should clear emp load only
.remove::<ThinkerBuilder>();
}
}
Expand Down Expand Up @@ -87,10 +88,14 @@ fn enter_ship(
#[allow(clippy::type_complexity)]
fn exit_ship(
mut commands: Commands,
query: Query<Entity, (With<PlayerMarker>, With<ShipMarker>, With<InputTarget>)>,
mut query: Query<
(Entity, &mut ShipInput),
(With<PlayerMarker>, With<ShipMarker>, With<InputTarget>),
>,
) {
for entity in &query {
for (entity, mut ship_input) in &mut query {
commands.entity(entity).remove::<PrimaryPlayerBundle>();
*ship_input = ShipInput::default();
}
}
fn enter_hexton() {}
Expand Down

0 comments on commit fefa01f

Please sign in to comment.