Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
0.6.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Leandro Schmidt committed Jan 10, 2024
1 parent 2fa4537 commit 4986ea3
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 77 deletions.
Binary file modified data/scenes/scenario.rgs
Binary file not shown.
Empty file.
1 change: 0 additions & 1 deletion game/src/bn_engine/mod.rs

This file was deleted.

3 changes: 0 additions & 3 deletions game/src/bn_engine/player_engine.rs

This file was deleted.

3 changes: 2 additions & 1 deletion game/src/bn_scripts/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod player_scripts;
pub mod player_scripts;
pub mod objects_scripts;
1 change: 1 addition & 0 deletions game/src/bn_scripts/objects_scripts/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod slider;
54 changes: 54 additions & 0 deletions game/src/bn_scripts/objects_scripts/slider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use fyrox::{
core::{
reflect::prelude::*,
uuid::{uuid, Uuid},
visitor::prelude::*,
TypeUuidProvider,
},
event::Event,
impl_component_provider,
script::{ScriptContext, ScriptDeinitContext, ScriptTrait},
};

#[derive(Visit, Reflect, Default, Debug, Clone)]
pub struct Slider {
// Add fields here.
}
impl Slider {
pub fn change_player_sliding() {}
}

impl_component_provider!(Slider);

impl TypeUuidProvider for Slider {
fn type_uuid() -> Uuid {
uuid!("af230ca1-43ea-4024-a25e-93a9a567c6d7")
}
}

impl ScriptTrait for Slider {
fn on_init(&mut self, _context: &mut ScriptContext) {
// Put initialization logic here.
}

fn on_start(&mut self, _context: &mut ScriptContext) {
// There should be a logic that depends on other scripts in scene.
// It is called right after **all** scripts were initialized.
}

fn on_deinit(&mut self, _context: &mut ScriptDeinitContext) {
// Put de-initialization logic here.
}

fn on_os_event(&mut self, _event: &Event<()>, _context: &mut ScriptContext) {
// Respond to OS events here.
}

fn on_update(&mut self, _context: &mut ScriptContext) {
// Put object logic here.
}

fn id(&self) -> Uuid {
Self::type_uuid()
}
}
30 changes: 15 additions & 15 deletions game/src/bn_scripts/player_scripts/camera_moviment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use fyrox::{
},
event::{DeviceEvent, Event},
impl_component_provider,
scene::transform::Transform,
script::{ScriptContext, ScriptTrait},
};

Expand All @@ -17,17 +18,26 @@ pub struct CameraMoviment {
pub yaw: f32,
}
impl CameraMoviment {
//Mouse Detect Function
pub fn process_input_event(&mut self, event: &Event<()>) {
//Mouse Detect Function Pitch
pub fn process_camera_moviment_pitch(&mut self, event: &Event<()>, camera_node: &mut Transform) {
match event {
Event::DeviceEvent { event, .. } => {
if let DeviceEvent::MouseMotion { delta } = event {
self.yaw -= delta.0 as f32;
self.pitch = (self.pitch + delta.1 as f32).clamp(-90.0, 90.0);
camera_node.set_rotation(UnitQuaternion::from_axis_angle(
&Vector3::x_axis(),
//The 3 is mouse sensitivy
self.pitch.to_radians() / 3.,
));
}
}
_ => (),
}
}
//Mouse Detect Function Yaw
pub fn process_camera_moviment_yaw(camera_node: &mut Transform) {

}
pub fn get_pitch(&mut self) -> f32 {
return self.pitch;
Expand All @@ -52,20 +62,10 @@ impl ScriptTrait for CameraMoviment {
self.yaw = 0.0; //Horizontal View
}

fn on_os_event(&mut self, event: &Event<()>, _context: &mut ScriptContext) {
fn on_os_event(&mut self, event: &Event<()>, context: &mut ScriptContext) {
// Enable mouse detection
self.process_input_event(event);
}

fn on_update(&mut self, context: &mut ScriptContext) {
//Mouse Vertical View
context.scene.graph[context.handle]
.local_transform_mut()
.set_rotation(UnitQuaternion::from_axis_angle(
&Vector3::x_axis(),
//The 3 is mouse sensitivy
self.pitch.to_radians() / 3.,
));
let camera_node = context.scene.graph[context.handle].local_transform_mut();
self.process_camera_moviment_pitch(event, camera_node);
}

fn id(&self) -> Uuid {
Expand Down
40 changes: 36 additions & 4 deletions game/src/bn_scripts/player_scripts/foot_collider.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use fyrox::{
core::{
core::{
pool::Handle,
reflect::prelude::*,
uuid::{uuid, Uuid},
visitor::prelude::*,
TypeUuidProvider,
},
impl_component_provider,
scene::{collider::Collider, graph::Graph, node::Node},
scene::{collider::Collider, graph::Graph, node::Node, rigidbody::RigidBody},
script::{ScriptContext, ScriptTrait},
};

#[derive(Visit, Reflect, Default, Debug, Clone)]
pub struct FootCollider {
pub is_on_air: bool,
pub is_on_slider: bool,
}
impl FootCollider {
pub fn has_ground_contact(&mut self, handle: Handle<Node>, graph: &Graph) -> bool {
Expand All @@ -28,8 +29,38 @@ impl FootCollider {
}
false
}
pub fn is_in_air(&mut self) -> bool {
return self.is_on_air;
pub fn has_slider_contact(&mut self, handle: Handle<Node>, graph: &Graph) -> bool {
if let Some(collider) = graph.try_get(handle).and_then(|n| n.cast::<Collider>()) {
for contact in collider.contacts(&graph.physics) {
for manifold in contact.manifolds.iter() {
// Checking the actual foot collision
if let Some(actual_collider) = graph
.try_get(manifold.rigid_body2)
.and_then(|n| n.cast::<RigidBody>())
{
// If is a slider then
if actual_collider.tag() == "Slider" {
if manifold.local_n1.y.abs() > 0.7 || manifold.local_n2.y.abs() > 0.7 {
return true;
}
}
}
// // This is the player collision so doesnt need it
// if let Some(actual_collider) = graph
// .try_get(manifold.rigid_body1)
// .and_then(|n| n.cast::<RigidBody>())
// {
// if actual_collider.tag() == "Slider" {
// if manifold.local_n1.y.abs() > 0.7 || manifold.local_n2.y.abs() > 0.7 {
// return true;
// }
// }
// }
return false;
}
}
}
return false;
}
}
impl_component_provider!(FootCollider);
Expand All @@ -49,6 +80,7 @@ impl ScriptTrait for FootCollider {
fn on_update(&mut self, context: &mut ScriptContext) {
// Updating the variable
self.is_on_air = !self.has_ground_contact(context.handle, &context.scene.graph);
self.is_on_slider = self.has_slider_contact(context.handle, &context.scene.graph);
}

fn id(&self) -> Uuid {
Expand Down
63 changes: 43 additions & 20 deletions game/src/bn_scripts/player_scripts/player_moviment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use fyrox::{
core::{
algebra::{ArrayStorage, Const, Matrix, UnitQuaternion, Vector3},
log::Log,
pool::Handle,
reflect::prelude::*,
uuid::{uuid, Uuid},
Expand Down Expand Up @@ -35,7 +36,8 @@ pub struct PlayerMoviment {
ticks_reset_cooldown: i32,
acceleration: f32,
straffing: bool,
old_mouse_position: f32,
old_camera_yaw: f32,
old_camera_pitch: f32,
}
impl PlayerMoviment {
/// Process all keyboard for player moviment
Expand Down Expand Up @@ -107,15 +109,17 @@ impl PlayerMoviment {
&mut self,
velocity: Matrix<f32, Const<3>, Const<1>, ArrayStorage<f32, 3, 1>>,
is_on_air: bool,
is_on_slide: bool,
is_frontal_collide: bool,
is_pressing_s: bool,
acceleration_mouse: f32,
) -> Matrix<f32, Const<3>, Const<1>, ArrayStorage<f32, 3, 1>> {
let mut base_velocity = velocity;
let calculate_acceleration: bool = is_on_air || is_on_slide;
for i in 0..3 {
//If is in the air and moving the camera
if i != 1
&& is_on_air
&& calculate_acceleration
&& acceleration_mouse != 0.
&& !is_pressing_s
&& !is_frontal_collide
Expand Down Expand Up @@ -145,7 +149,7 @@ impl PlayerMoviment {
self.acceleration += acceleration;
base_velocity[i] *= self.acceleration;
//If is only in the air
} else if i != 1 && is_on_air && !is_pressing_s && !is_frontal_collide {
} else if i != 1 && calculate_acceleration && !is_pressing_s && !is_frontal_collide {
self.ticks_dessaceleration = 0;
base_velocity[i] *= self.acceleration;

Expand All @@ -164,22 +168,28 @@ impl PlayerMoviment {
}
base_velocity[i] *= self.acceleration;
//If is fronta colission
} else if is_frontal_collide {
}
// If is frontal collide
else if is_frontal_collide {
self.acceleration /= 3.;
if self.acceleration <= 1. {
self.acceleration = 1.;
self.straffing = false;
}
base_velocity[i] *= self.acceleration;
//If pressing S
} else if is_pressing_s {
}
// If pressed the S
else if is_pressing_s {
self.acceleration /= 2.;
if self.acceleration <= 1. {
self.acceleration = 1.;
self.straffing = false;
}
base_velocity[i] *= self.acceleration;
} else {
}
// In others cases the velocity multiplys by acceleration
else {
base_velocity[i] *= self.acceleration;
}
}
Expand All @@ -191,8 +201,10 @@ impl PlayerMoviment {
/// to change the player velocity and then change the position of the player
pub fn calculate_acceleration(&mut self, context: &mut ScriptContext) {
let mut is_on_air: bool = false;
let mut is_on_slide: bool = false;
let mut is_frontal_collide: bool = false;
let mut camera_yaw: f32 = 0.0;
let mut camera_pitch: f32 = 0.0;
//Getting variables from others scripts
{
// Receiving the cameras
Expand All @@ -211,6 +223,7 @@ impl PlayerMoviment {
.try_get_script_of::<FootCollider>(self.foot_collider_node)
{
is_on_air = foot_collider_node_script_ref.is_on_air;
is_on_slide = foot_collider_node_script_ref.is_on_slider;
}

// Receiving the frontal collider
Expand All @@ -235,7 +248,8 @@ impl PlayerMoviment {
// Keep only vertical velocity, and drop horizontal.
let mut velocity = Vector3::new(0.0, body.lin_vel().y, 0.0);
let mut dessacelerate: bool = false;
let mut mouse_accelerate: f32 = 0.;
let mut mouse_accelerate_yaw: f32 = 0.;
let mut mouse_accelerate_pitch: f32 = 0.;

// Change the velocity depending on the keys pressed.
if self.position_z || self.straffing {
Expand All @@ -248,16 +262,16 @@ impl PlayerMoviment {
velocity -= body.look_vector() * 2.;
dessacelerate = true;
}
if self.position_x {
if self.position_x && !is_on_slide {
// If we moving left then add "side" vector of the body.
velocity += body.side_vector() * 2.;
}
if self.position_x_negative {
if self.position_x_negative && !is_on_slide {
// If we moving right then subtract "side" vector of the body.
velocity -= body.side_vector() * 2.;
}
// Jump System
if self.jump && !is_on_air && self.ticks_jump_cooldown <= 3 {
if self.jump && !is_on_slide && !is_on_air && self.ticks_jump_cooldown <= 3 {
//Check if is the first tick
if self.ticks_jump_cooldown == -1 {
self.ticks_jump_cooldown = 0;
Expand All @@ -272,32 +286,41 @@ impl PlayerMoviment {
self.ticks_jump_cooldown = -1;
}
// Calculation the acceleration by mouse movement
if self.old_mouse_position != camera_yaw.to_radians() {
if self.old_camera_yaw != camera_yaw.to_radians() {
//Calculates the mouse velocity
let mut _player_mouse_position: f32 = 0.;
let mut mouse_position_yaw: f32 = 0.;
//Negative to Positive
if camera_yaw.to_radians() < 0. {
_player_mouse_position = camera_yaw.to_radians().abs();
mouse_position_yaw = camera_yaw.to_radians().abs();
} else {
_player_mouse_position = camera_yaw.to_radians();
mouse_position_yaw = camera_yaw.to_radians();
}
//Difference between
if _player_mouse_position != self.old_mouse_position {
if _player_mouse_position < self.old_mouse_position {
mouse_accelerate = self.old_mouse_position - _player_mouse_position;
if mouse_position_yaw != self.old_camera_yaw {
if mouse_position_yaw < self.old_camera_yaw {
mouse_accelerate_yaw = self.old_camera_yaw - mouse_position_yaw;
} else {
mouse_accelerate = _player_mouse_position - self.old_mouse_position;
mouse_accelerate_yaw = mouse_position_yaw - self.old_camera_yaw;
}
}
self.old_mouse_position = _player_mouse_position
self.old_camera_yaw = mouse_position_yaw
}
// Calculation the acceleration by slider moviment
if self.old_camera_pitch != camera_pitch.to_radians() {
let mut mouse_position_pitch: f32 = 0.;
mouse_position_pitch = camera_pitch.to_radians();
Log::info(mouse_position_pitch.to_string());

self.old_camera_pitch = mouse_position_pitch;
}
// Change the velocity of the player
body.set_lin_vel(self.velocity(
velocity,
is_on_air,
is_on_slide,
is_frontal_collide,
dessacelerate,
mouse_accelerate,
mouse_accelerate_yaw,
));
//Reset Tick Cooldown
if self.ticks_reset_cooldown <= 30 {
Expand Down
Loading

0 comments on commit 4986ea3

Please sign in to comment.