Skip to content

Commit

Permalink
guidelines (finally!)
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinmorrow committed Jul 18, 2023
1 parent 6537751 commit 6b5759b
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/player/grapple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ impl Plugin for GrapplePlugin {
debug!("Building GrapplePlugin");

app.add_state::<GrappleState>()
.insert_resource(Guideline::default())
.add_systems(OnExit(GrappleState::Grappling), end_grapple)
.add_systems(OnExit(GrappleState::Aiming), remove_guideline)
.add_systems(
Update,
(
idle.run_if(state_exists_and_equals(GrappleState::Idle)),
aim.run_if(state_exists_and_equals(GrappleState::Aiming)),
aim_marker.run_if(state_exists_and_equals(GrappleState::Aiming)),
aim_guideline.run_if(state_exists_and_equals(GrappleState::Aiming)),
grapple.run_if(state_exists_and_equals(GrappleState::Grappling)),
manage_grapple.run_if(state_exists_and_equals(GrappleState::Grappling)),
should_grapple_end.run_if(state_exists_and_equals(GrappleState::Grappling)),
Expand Down Expand Up @@ -64,6 +67,9 @@ struct TargetPos(
Entity,
);

#[derive(Resource, Default)]
struct Guideline(Vec<Entity>);

fn idle(
action_state_query: Query<&ActionState<Action>, With<Player>>,
mut next_grapple_state: ResMut<NextState<GrappleState>>,
Expand Down Expand Up @@ -120,6 +126,53 @@ fn aim_marker(
commands.insert_resource(target_pos);
}

fn aim_guideline(
target_pos: Option<Res<TargetPos>>,
player_query: Query<&GlobalTransform, With<Player>>,
mut guideline: ResMut<Guideline>,
mut commands: Commands,
) {
// Clear old guidelines
for guideline in guideline.0.iter() {
commands.entity(*guideline).despawn();
}
guideline.0.clear();

let Some(target_pos) = target_pos else {
warn!("No target pos for grapple guidelines");
return;
};
let player = player_query.single();

// Get direction from player to target
let player_pos = player.translation().truncate();
let target_pos = target_pos.0;
let direction = target_pos - player_pos;
let distance = direction.normalize() * 50.0;

// Add guidelines
let mut current_pos = player_pos;
while current_pos.distance(target_pos) >= 50.0 {
let sprite = SpriteBundle {
sprite: Sprite {
color: Color::BLUE,
custom_size: Some(Vec2::new(10.0, 10.0)),
..default()
},
transform: Transform {
translation: current_pos.extend(2.0),
..default()
},
..default()
};

let entity = commands.spawn(sprite).id();

guideline.0.push(entity);
current_pos += distance;
}
}

/// Cleanly removes the `TargetPos` resource.
///
/// This despawns the marker entity and removes the resource.
Expand Down Expand Up @@ -323,6 +376,17 @@ fn end_grapple(
super::remove_grapple_force(mut_player_query);
}

fn remove_guideline(mut guideline: ResMut<Guideline>, mut commands: Commands) {
debug!("Removing guideline");

// Clear old guidelines
for guideline in guideline.0.iter() {
commands.entity(*guideline).despawn();
}

guideline.0.clear();
}

fn get_distance_to_window_edge(player: &Transform, window: &Window, direction: Vec2) -> f32 {
let window_size = Vec2::new(window.width(), window.height());
let player_pos = player.translation.truncate();
Expand Down

0 comments on commit 6b5759b

Please sign in to comment.