From ea7596edf57149b3c3f84378276c9d0558aed050 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 16 Jul 2022 15:47:20 +0200 Subject: better stopping detection --- godot/scenes/objects/Player.tscn | 4 +- .../libcode_with_your_friends2022.so | Bin 28904944 -> 29349872 bytes rust/src/basic_die.rs | 43 +++++++++++++++------ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/godot/scenes/objects/Player.tscn b/godot/scenes/objects/Player.tscn index f8a3fba..9730147 100644 --- a/godot/scenes/objects/Player.tscn +++ b/godot/scenes/objects/Player.tscn @@ -6,12 +6,12 @@ [node name="PlayerRoot" type="Spatial"] script = ExtResource( 3 ) -camera/camera_clamp = Vector2( 0, -2 ) +camera/camera_clamp = Vector2( 0, -1.3 ) shooting/max_force = 30.0 shooting/up_angle = 1.0 shooting/stopping_velocity = 0.003 input/camera_mouse_sensitivity = Vector2( 0.003, 0.002 ) -input/shoot_sensitivity = 0.3 +input/shoot_sensitivity = 0.1 [node name="W8" parent="." instance=ExtResource( 1 )] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.1, 0 ) diff --git a/lib/x86_64-unknown-linux-gnu/libcode_with_your_friends2022.so b/lib/x86_64-unknown-linux-gnu/libcode_with_your_friends2022.so index 4b82952..97b6350 100755 Binary files a/lib/x86_64-unknown-linux-gnu/libcode_with_your_friends2022.so and b/lib/x86_64-unknown-linux-gnu/libcode_with_your_friends2022.so differ diff --git a/rust/src/basic_die.rs b/rust/src/basic_die.rs index ffd3bff..ab53720 100644 --- a/rust/src/basic_die.rs +++ b/rust/src/basic_die.rs @@ -25,6 +25,8 @@ pub struct BasicDie { up_angle: f32, #[property(path="shooting/stopping_velocity")] stopping_velocity: f32, + #[property(path="shooting/stopping_min_milliseconds")] + stopping_min_ms: i64, #[property(path="input/camera_mouse_sensitivity")] mouse_sensitivity: Vector2, #[property(path="input/shoot_sensitivity")] @@ -32,6 +34,7 @@ pub struct BasicDie { input_state: InputState, current_force: f32, + last_shot_time: i64, action_shooting: String, @@ -56,11 +59,13 @@ impl BasicDie { max_force: 0.0, up_angle: 5.0, stopping_velocity: 0.0, + stopping_min_ms: 1000, mouse_sensitivity: Vector2 { x: 1.0, y: 1.0 }, shoot_sensitivity: 1.0, input_state: InputState::Default, current_force: 0.0, + last_shot_time: 0, action_shooting: String::from("mouse_btn_left"), @@ -75,10 +80,12 @@ impl BasicDie { #[export] unsafe fn _ready(&mut self, owner: &Spatial) { - Input::godot_singleton().set_mouse_mode(Input::MOUSE_MODE_CAPTURED); + Input::godot_singleton().set_mouse_mode(Input::MOUSE_MODE_CAPTURED); owner.set_physics_process(true); + self.last_shot_time = OS::godot_singleton().get_ticks_msec(); + let search_node = | parent: &SpatialRef, name: String | { let parent_node = match parent { Some(node) => node.assume_safe().as_ref(), @@ -114,23 +121,33 @@ impl BasicDie { #[export] unsafe fn _physics_process(&mut self, _owner: &Spatial, _delta: f64) { + + let die = match self.node_die { + None => { godot_warn!("No W8 assigned."); return; }, + Some(node) => match node.assume_safe().cast::() { + None => { godot_warn!("W8 is not a RigidBody."); return; }, + Some(rb) => rb + } + }; + + // let the cam follow the die + match self.node_camera_root { + None => { godot_warn!("No W8 assigned."); }, + Some(cam) => { + cam.assume_safe().set_translation(die.translation()); + } + } // detect if the die stops moving - if matches!(self.input_state, InputState::Moving) { - // get the current velocity - let current_vel = match self.node_die { - None => { godot_warn!("No W8 assigned."); 0.0 }, - Some(node) => - match node.assume_safe().cast::() { - None => { godot_warn!("W8 is not a RigidBody."); 0.0 }, - Some(rb) => rb.linear_velocity().length() - } - }; + let delta_ms = OS::godot_singleton().get_ticks_msec() - self.last_shot_time; + if matches!(self.input_state, InputState::Moving) + && (delta_ms > self.stopping_min_ms) { // check if the velocity is less than the threshold and change input state in that case + let current_vel = die.linear_velocity().length(); if current_vel <= self.stopping_velocity { self.input_state = InputState::Default; - godot_print!("Die stopped moving"); + godot_print!("Die stopped moving at velocity {} after {} ms", current_vel, delta_ms); } }; } @@ -297,5 +314,7 @@ impl BasicDie { // actually add the force die.apply_impulse(die.transform().origin, impulse_dir); + + self.last_shot_time = OS::godot_singleton().get_ticks_msec(); } } -- cgit