Skip to content

Commit

Permalink
Merge pull request #24 from AnthonyTornetta/23-add-braking-to-ships
Browse files Browse the repository at this point in the history
Added ship braking
  • Loading branch information
AnthonyTornetta authored Jan 27, 2023
2 parents 4090643 + 25af634 commit 73ec16a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
9 changes: 6 additions & 3 deletions cosmos_client/src/input/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use bevy::{prelude::*, utils::HashMap};
pub enum CosmosInputs {
MoveForward,
MoveBackward,
MoveUpOrJump,
Jump,
SlowDown,
MoveLeft,
MoveRight,
// For use in ships
MoveDown,
MoveUp,
Sprint,

StopPiloting,
Expand Down Expand Up @@ -50,8 +52,9 @@ fn init_input(mut input_handler: ResMut<CosmosInputHandler>) {
input_handler.set_keycode(CosmosInputs::MoveBackward, KeyCode::S);
input_handler.set_keycode(CosmosInputs::MoveRight, KeyCode::D);
input_handler.set_keycode(CosmosInputs::SlowDown, KeyCode::LShift);
input_handler.set_keycode(CosmosInputs::MoveUpOrJump, KeyCode::Space);
input_handler.set_keycode(CosmosInputs::MoveDown, KeyCode::LShift);
input_handler.set_keycode(CosmosInputs::Jump, KeyCode::Space);
input_handler.set_keycode(CosmosInputs::MoveDown, KeyCode::Q);
input_handler.set_keycode(CosmosInputs::MoveUp, KeyCode::E);
input_handler.set_keycode(CosmosInputs::Sprint, KeyCode::LControl);

input_handler.set_mouse_button(CosmosInputs::BreakBlock, MouseButton::Left);
Expand Down
6 changes: 4 additions & 2 deletions cosmos_client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn process_ship_movement(
if input_handler.check_pressed(CosmosInputs::MoveBackward, &keys, &mouse) {
movement.movement.z -= 1.0;
}
if input_handler.check_pressed(CosmosInputs::MoveUpOrJump, &keys, &mouse) {
if input_handler.check_pressed(CosmosInputs::MoveUp, &keys, &mouse) {
movement.movement.y += 1.0;
}
if input_handler.check_pressed(CosmosInputs::MoveDown, &keys, &mouse) {
Expand All @@ -76,6 +76,8 @@ fn process_ship_movement(
movement.movement.x += 1.0;
}

movement.breaking = input_handler.check_pressed(CosmosInputs::SlowDown, &keys, &mouse);

if input_handler.check_just_pressed(CosmosInputs::StopPiloting, &keys, &mouse) {
client.send_message(
NettyChannel::Reliable.id(),
Expand Down Expand Up @@ -170,7 +172,7 @@ fn process_player_movement(
if input_handler.check_pressed(CosmosInputs::MoveBackward, &keys, &mouse) {
velocity.linvel -= forward * time;
}
if input_handler.check_just_pressed(CosmosInputs::MoveUpOrJump, &keys, &mouse) {
if input_handler.check_just_pressed(CosmosInputs::Jump, &keys, &mouse) {
velocity.linvel += up * 5.0;
}
if input_handler.check_pressed(CosmosInputs::MoveLeft, &keys, &mouse) {
Expand Down
4 changes: 4 additions & 0 deletions cosmos_core/src/structure/ship/ship_movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::pilot::Pilot;

#[derive(Component, Default, Serialize, Deserialize, Debug, Clone, FromReflect, Reflect)]
pub struct ShipMovement {
pub breaking: bool,
pub movement: Vec3,
pub torque: Vec3,
}
Expand All @@ -22,6 +23,7 @@ impl ShipMovement {
pub fn set(&mut self, other: &Self) {
self.movement = other.movement;
self.torque = other.torque;
self.breaking = other.breaking;
}
}

Expand All @@ -40,6 +42,8 @@ fn clear_movement_when_no_pilot(mut query: Query<&mut ShipMovement, Without<Pilo
movement.torque.x = 0.0;
movement.torque.y = 0.0;
movement.torque.z = 0.0;

movement.breaking = false;
}
}

Expand Down
10 changes: 8 additions & 2 deletions cosmos_core/src/structure/systems/thruster_system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use crate::{

use super::{StructureSystem, Systems};

const MAX_SHIP_SPEED: f32 = 256.0;

pub struct ThrusterProperty {
pub strength: f32,
pub energy_consupmtion: f32,
Expand Down Expand Up @@ -139,9 +141,9 @@ fn update_movement(
.angvel
.clamp_length(0.0, movement.torque.length() * 4.0);

velocity.linvel = velocity.linvel.clamp_length(0.0, 256.0);
velocity.linvel = velocity.linvel.clamp_length(0.0, MAX_SHIP_SPEED);

let movement_vector = if normal.x == 0.0 && normal.y == 0.0 && normal.z == 0.0 {
let mut movement_vector = if normal.x == 0.0 && normal.y == 0.0 && normal.z == 0.0 {
Vec3::ZERO
} else {
let mut movement_vector = transform.forward() * normal.z;
Expand Down Expand Up @@ -172,6 +174,10 @@ fn update_movement(
}
};

if movement.breaking {
movement_vector += -velocity.linvel * 0.1;
}

external_impulse.impulse += movement_vector;
}
}
Expand Down

0 comments on commit 73ec16a

Please sign in to comment.