From ea7596edf57149b3c3f84378276c9d0558aed050 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 16 Jul 2022 15:47:20 +0200 Subject: better stopping detection --- rust/src/basic_die.rs | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) (limited to 'rust') 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