Skip to content

Commit

Permalink
Merge branch 'grapple'
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinmorrow committed Jul 19, 2023
2 parents e8664c5 + ed1b372 commit a043ab4
Show file tree
Hide file tree
Showing 3 changed files with 480 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub fn start_app() {
setup_default_plugins(),
PhysicsPlugins::default(),
InputManagerPlugin::<player::Action>::default(),
player::grapple::GrapplePlugin::default(),
))
.insert_resource(level::Level::new(0))
.insert_resource(Gravity(Vec2::new(0.0, -9.81 * 100.0)))
Expand Down
50 changes: 46 additions & 4 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ use bevy::{prelude::*, window::PrimaryWindow};
use bevy_xpbd_2d::prelude::*;
use leafwing_input_manager::prelude::*;

pub mod grapple;

const SIZE: f32 = 64.0;
const SIZE_VEC2: Vec2 = Vec2::new(SIZE, SIZE);

const TEXTURE_PATH: &str = "player.png";

#[derive(Component)]
#[derive(Component, Default)]
pub struct Player;

#[derive(Bundle)]
#[derive(Bundle, Default)]
pub struct PlayerBundle {
sprite_bundle: SpriteBundle,
rigid_body: RigidBody,
Expand All @@ -19,6 +21,8 @@ pub struct PlayerBundle {
angular_damping: AngularDamping,
linear_damping: LinearDamping,
input_manager: InputManagerBundle<Action>,
external_force: ExternalForce,
gravity_scale: GravityScale,
}

impl PlayerBundle {
Expand All @@ -31,8 +35,11 @@ impl PlayerBundle {
.insert(QwertyScanCode::A, Action::Left)
.insert(KeyCode::Right, Action::Right)
.insert(QwertyScanCode::D, Action::Right)
.insert(KeyCode::W, Action::Jump)
.insert(QwertyScanCode::Up, Action::Jump)
.insert(KeyCode::Space, Action::Jump)
.insert(KeyCode::Up, Action::Jump);
.insert(QwertyScanCode::E, Action::Grapple)
.insert(QwertyScanCode::Slash, Action::Grapple);

Self {
sprite_bundle: SpriteBundle {
Expand All @@ -56,6 +63,8 @@ impl PlayerBundle {
action_state: ActionState::default(),
input_map,
},
external_force: ExternalForce::default(),
gravity_scale: GravityScale(1.0),
}
}
}
Expand Down Expand Up @@ -98,7 +107,40 @@ pub fn r#move(
Action::Left => player.x = -300.0,
Action::Right => player.x = 300.0,
Action::Jump => player.y = 300.0,
Action::Grapple => todo!(),
Action::Grapple => { /* Do nothing, this is handled elsewhere. */ }
}
}
}

/// Add a force to the player in the given direction (to be used for grappling).
fn add_grapple_force(
mut player_query: Query<(&mut ExternalForce, &mut GravityScale), With<Player>>,
direction: Vec2,
) {
let (mut external_force, mut gravity) = player_query.single_mut();

// Add force to player
let force = direction * grapple::FORCE_MULT;
trace!("Setting external force on player to: {:?}", force);
external_force.set_force(force);

// Remove player gravity
gravity.0 = 0.0;

debug!("Added grapple force to player.");
}

/// Remove the force from the player (to be used for stopping grappling).
fn remove_grapple_force(
mut player_query: Query<(&mut ExternalForce, &mut GravityScale), With<Player>>,
) {
let (mut external_force, mut gravity) = player_query.single_mut();

// Remove player external force
external_force.set_force(Vec2::ZERO);

// Add player gravity
gravity.0 = 1.0;

debug!("Removed grapple force from player.");
}
Loading

0 comments on commit a043ab4

Please sign in to comment.