Skip to content

Commit

Permalink
Improve trajectory prediction system
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Dec 8, 2023
1 parent d04bae2 commit db33998
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 43 deletions.
5 changes: 4 additions & 1 deletion src/debris.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::block::{Block, DestroyBlockOnContact, BLOCK_COLLISION_GROUP, BLOCK_SIZE};
use crate::floor::Floor;
use crate::level::LevelLifecycle;
use crate::level::{LevelLifecycle, UpdateLevelStats};
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
use rand::random;
Expand Down Expand Up @@ -33,6 +33,7 @@ pub fn block_to_debris_system(
mut collision_events: EventReader<CollisionEvent>,
mut floor_query: Query<&mut DestroyBlockOnContact>,
mut block_query: Query<(Entity, &Block, &Transform, &Velocity)>,
mut update_level_stats_events: EventWriter<UpdateLevelStats>,
) {
for event in collision_events.read() {
match event {
Expand Down Expand Up @@ -73,6 +74,8 @@ pub fn block_to_debris_system(
},
));
}

update_level_stats_events.send(UpdateLevelStats::BlockDestroyed);
}
})
}
Expand Down
30 changes: 23 additions & 7 deletions src/effect/magnetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,34 @@ impl Default for MagneticEffect {
/// Should apply a force to all blocks in range, pulling them towards the center of the block
pub fn magnetic_effect_system(
mut magnets_query: Query<
(&Transform, &mut ExternalImpulse, &MagneticEffect),
(Without<Falling>, Without<Aiming>, Without<TargetIndicator>),
(Entity, &Transform, &mut ExternalImpulse, &MagneticEffect),
(Without<Aiming>, Without<TargetIndicator>, With<Block>),
>,
mut blocks_query: Query<
(&Transform, &mut ExternalImpulse),
(Without<MagneticEffect>, With<Block>),
(Entity, &Transform, &mut ExternalImpulse),
(
With<Block>,
Without<MagneticEffect>,
Without<Aiming>,
Without<TargetIndicator>,
),
>,
) {
for ((magnet_transform, mut magnet_self_impulse, effect)) in magnets_query.iter_mut() {
for (block_transform, mut external_impulse) in blocks_query.iter_mut() {
for ((mag_entity, magnet_transform, mut magnet_self_impulse, effect)) in
magnets_query.iter_mut()
{
for (block_entity, block_transform, mut external_impulse) in blocks_query.iter_mut() {
let impulse = calculate_magnetic_impulse(magnet_transform, block_transform, effect);

dbg!(
&mag_entity,
&block_entity,
&impulse,
&magnet_transform,
&block_transform,
&effect
);

if let Some(impulse) = impulse {
external_impulse.impulse = impulse;
magnet_self_impulse.impulse -= impulse;
Expand All @@ -51,7 +67,7 @@ pub fn magnetic_effect_system(

let mut combinations = magnets_query.iter_combinations_mut();
while let Some(
[(transform_a, mut impulse_a, effect_a), (transform_b, mut impulse_b, effect_b)],
[(entity_a, transform_a, mut impulse_a, effect_a), (entity_b, transform_b, mut impulse_b, effect_b)],
) = combinations.fetch_next()
{
let impulse = calculate_magnetic_impulse(transform_a, transform_b, effect_a);
Expand Down
46 changes: 37 additions & 9 deletions src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ impl Plugin for LevelPlugin {
Update,
(
load_level_event,
(check_current_block_stats, check_win_condition)
(
check_current_block_stats,
check_win_loose_condition,
update_level_stats_events,
)
.run_if(in_state(LevelState::Playing)),
),
)
.add_systems(OnEnter(LevelState::Playing), reset_level_stats)
.init_resource::<LevelStats>()
.insert_resource(LEVELS[0].clone())
.add_event::<NextLevel>();
.add_event::<NextLevel>()
.add_event::<UpdateLevelStats>();
}
}

Expand All @@ -30,11 +35,17 @@ pub struct LevelStats {
pub current_height: f32,
pub blocks_thrown: usize,
pub current_block_count: usize,
pub blocks_missed: usize,
pub blocks_dropped: usize,

pub deaths: usize,
}

#[derive(Event, Debug, Clone)]
pub enum UpdateLevelStats {
BlockThrown,
BlockDestroyed,
}

#[derive(Resource, Debug, Clone)]
pub struct Level {
pub level: usize,
Expand Down Expand Up @@ -68,22 +79,22 @@ pub static LEVELS: [Level; 4] = [
level: 1,
goal: LevelGoal::ReachBlockCount(10),
time_limit: Some(Duration::from_secs(60)),
max_blocks: None,
max_blocks: Some(13),
base_width: 80.0,
},
Level {
level: 2,
goal: LevelGoal::ReachHeight(200.0),
time_limit: Some(Duration::from_secs(60)),
max_blocks: None,
max_blocks: Some(30),
base_width: 120.0,
},
Level {
level: 3,
goal: LevelGoal::ReachBlockCount(10),
goal: LevelGoal::ReachBlockCount(20),
time_limit: Some(Duration::from_secs(60)),
max_blocks: None,
base_width: 160.0,
max_blocks: Some(25),
base_width: 100.0,
},
];

Expand Down Expand Up @@ -130,7 +141,7 @@ pub fn check_current_block_stats(
level_stats.current_block_count = block_count;
}

pub fn check_win_condition(
pub fn check_win_loose_condition(
mut commands: Commands,
level_stats: Res<LevelStats>,
level: Res<Level>,
Expand All @@ -153,3 +164,20 @@ pub fn check_win_condition(
fn reset_level_stats(mut level_stats: ResMut<LevelStats>) {
*level_stats = LevelStats::default();
}

fn update_level_stats_events(
mut commands: Commands,
mut level_stats: ResMut<LevelStats>,
mut evr: EventReader<UpdateLevelStats>,
) {
for event in evr.read() {
match event {
UpdateLevelStats::BlockThrown => {
level_stats.blocks_thrown += 1;
}
UpdateLevelStats::BlockDestroyed => {
level_stats.blocks_dropped += 1;
}
}
}
}
7 changes: 7 additions & 0 deletions src/level_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub fn egui_level_ui(
ui.heading("Stats:");
ui.label(format!("Tower Height: {:.2}", stats.current_height));
ui.label(format!("Blocks Stacked: {}", stats.current_block_count));
ui.label(format!("Blocks Thrown: {}", stats.blocks_thrown));
ui.label(format!("Blocks Dropped: {}", stats.blocks_dropped));

ui.heading("Winning Condition:");
match current_level.goal {
Expand All @@ -81,6 +83,11 @@ pub fn egui_level_ui(
}
}

ui.heading("Loosing Condition:");
if let Some(max_blocks) = current_level.max_blocks {
ui.label(format!("Max Blocks: {}", max_blocks));
}

if ui.button("Next Level").clicked() {
next_level.send(NextLevel(None));
}
Expand Down
Loading

0 comments on commit db33998

Please sign in to comment.