From a8b66a60dca81a4e5d4ab995226b77ba29bc70c3 Mon Sep 17 00:00:00 2001 From: IcECreAm777 <31211782+IcECreAm777@users.noreply.github.com> Date: Sat, 16 Jul 2022 04:20:11 +0200 Subject: basic camera movement also events for other movements --- rust/src/BasicDie.rs | 239 +++++++++++++++++++++++++++++++++++++++++++++++++++ rust/src/game.rs | 4 +- rust/src/lib.rs | 2 + 3 files changed, 242 insertions(+), 3 deletions(-) create mode 100644 rust/src/BasicDie.rs (limited to 'rust/src') diff --git a/rust/src/BasicDie.rs b/rust/src/BasicDie.rs new file mode 100644 index 0000000..7bc6d65 --- /dev/null +++ b/rust/src/BasicDie.rs @@ -0,0 +1,239 @@ +use std::borrow::Borrow; +use gdnative::api::*; +use gdnative::prelude::*; + +/// the input state for the player +enum InputState { + Default, + Shooting, + Moving +} + +/// the basic die used by the player +#[derive(NativeClass)] +#[inherit(RigidBody)] +#[register_with(Self::register_builder)] +pub struct BasicDie { + #[property(path="base/camera_offset")] + camera_offset: f32, + #[property(path="base/camera_clamp")] + camera_clamp: Vector2, + #[property(path="shooting/max_force")] + max_force: f32, + #[property(path="shooting/up_angle")] + up_angle: f32, + #[property(path="input/mouse_sensitivity")] + mouse_sensitivity: Vector2, + + input_state: InputState, + current_force: f32, + + action_shooting: String, + + node_camera_arm_horizontal: Option>, + node_camera_arm_vertical: Option>, +} + +#[methods] +impl BasicDie { + // Register the builder for methods, properties and/or signals. + fn register_builder(_builder: &ClassBuilder) { + godot_print!("BasicDie builder is registered!"); + } + + fn new(_owner: &RigidBody) -> Self { + BasicDie { + camera_offset: 0.0, + camera_clamp: Vector2 { x: 0.0, y: 0.0 }, + max_force: 0.0, + up_angle: 5.0, + mouse_sensitivity: Vector2 { x: 1.0, y: 1.0 }, + + input_state: InputState::Default, + current_force: 0.0, + + action_shooting: String::from("mouse_btn_left"), + + node_camera_arm_horizontal: None, + node_camera_arm_vertical: None, + } + } + + #[export] + unsafe fn _ready(&mut self, owner: &RigidBody) { + owner.set_physics_process(true); + + // look for the horizontal camera arm + match owner.get_node(NodePath::from_str("CameraArmHorizontal")) { + Some(node) => { + let save_node = node.assume_safe(); + match save_node.cast::() { + Some(casted) => { + let save_casted = casted.claim(); + self.node_camera_arm_horizontal = Some(save_casted)}, + _ => godot_warn!("Camera Arm was not of type 'Spatial'"), + } + }, + _ => godot_warn!("No child node called 'Camera found'") + } + + // look for the vertical camera arm + match self.node_camera_arm_horizontal { + Some(arm) => { + let save_arm = arm.assume_safe(); + match save_arm.get_node(NodePath::from_str("CameraArmVertical")) { + Some(node) => { + let save_node = node.assume_safe(); + match save_node.cast::() { + Some(casted) => { + let save_casted = casted.claim(); + self.node_camera_arm_vertical = Some(save_casted)}, + _ => godot_warn!("Camera Arm was not of type 'Spatial'"), + } + }, + _ => godot_warn!("No vertical arm found.") + } + }, + _ => godot_warn!("No horizontal arm to look for the vertical arm") + } + } + + #[export] + unsafe fn _physics_process(&mut self, owner: &RigidBody, delta: f64) { + + if matches!(self.input_state, InputState::Moving) { + // TODO check if velocity reached a certain threshold and set the mode to default again + }; + } + + #[export] + unsafe fn _input(&mut self, owner: &RigidBody, event: Ref) { + + self.general_input(event.borrow()); + + match self.input_state { + InputState::Default => self.default_input(event), + InputState::Shooting => self.shooting_input(event), + InputState::Moving => self.moving_input(event), + } + } + + /// this input method will always be called, regardless of the input state + unsafe fn general_input(&mut self, event: &Ref) { + let save_event = event.assume_safe(); + + // get the input as mouse input + let mouse_event = save_event.cast::(); + match mouse_event { + Some(motion_event) => { + let x_mov = motion_event.relative().x * self.mouse_sensitivity.x; + self.rotate_cam_horizontal(x_mov); + }, + _ => {} + } + } + + /// this input method will be called when looking around, before taking a shot + unsafe fn default_input(&mut self, event: Ref) { + let save_event = event.assume_safe(); + + godot_print!("default input"); + + // left mouse button was pressed => switch to shooting mode + if save_event.is_action_pressed(GodotString::from_str(&self.action_shooting), false, false) { + godot_print!("mouse_button, switching to shooting mode"); + self.input_state = InputState::Shooting; + return; + } + + // get the input as mouse input + let mouse_event = save_event.cast::(); + match mouse_event { + Some(motion_event) => { + let y_mov = -motion_event.relative().y * self.mouse_sensitivity.y; + self.rotate_cam_vertical(y_mov); + }, + _ => {} + }; + } + + /// this input method will be called when player is currently taking a shot + unsafe fn shooting_input(&mut self, event: Ref) { + let save_event = event.assume_safe(); + + godot_print!("shooting input"); + + if save_event.is_action_released(GodotString::from_str(&self.action_shooting), false) { + self.input_state = InputState::Moving; + // TODO add force based on input and strength + return; + } + + // get the input as mouse input + let mouse_event = save_event.cast::(); + match mouse_event { + Some(motion_event) => { + let y_mov = motion_event.relative().y * self.mouse_sensitivity.y; + self.current_force = match self.current_force + y_mov { + x if x < 0.0 => 0.0, + x if x > self.max_force => self.max_force, + _ => self.current_force + y_mov + }; + + godot_print!("current force: {}", self.current_force); + }, + _ => {} + }; + } + + /// this input method will be called when player is moving + unsafe fn moving_input(&mut self, event: Ref) { + let save_event = event.assume_safe(); + + godot_print!("moving input"); + + // get the input as mouse input + let mouse_event = save_event.cast::(); + match mouse_event { + Some(motion_event) => { + let y_mov = motion_event.relative().y * self.mouse_sensitivity.y; + self.rotate_cam_vertical(y_mov); + }, + _ => {} + }; + } + + unsafe fn rotate_cam_horizontal(&mut self, input: f32) { + // make sure the arm exists + match self.node_camera_arm_horizontal { + Some(arm) => { + // rotate the horizontal camera arm + let save_arm = arm.assume_safe(); + save_arm.rotate_object_local(Vector3 {x: 0.0, y: 1.0, z: 0.0}, input as f64) + }, + _ => godot_warn!("No horizontal camera arm assigned.") + } + } + + unsafe fn rotate_cam_vertical(&mut self, input: f32) { + // make sure the camera arm actually exists + match self.node_camera_arm_vertical { + Some(arm) => { + // check for the current rotation + let save_arm = arm.assume_safe(); + let current_rot = save_arm.rotation(); + godot_print!("current rotation: {} | {} | {} ", current_rot.x, current_rot.y, current_rot.z); + + // clamp the rotation + if current_rot.x + input > self.camera_clamp.x || current_rot.x + input <= self.camera_clamp.y { + return; + } + + // actually rotate if possible + let save_arm = arm.assume_safe(); + save_arm.rotate_object_local(Vector3 {x: 1.0, y: 0.0, z: 0.0}, input as f64) + }, + _ => godot_warn!("No vertical camera arm assigned") + } + } +} diff --git a/rust/src/game.rs b/rust/src/game.rs index ece5a54..16c93d8 100644 --- a/rust/src/game.rs +++ b/rust/src/game.rs @@ -40,7 +40,5 @@ impl Game { // This function will be called in every frame #[export] - unsafe fn _process(&self, _owner: &Spatial, delta: f64) { - godot_print!("Inside {} _process(), delta is {}", self.name, delta); - } + unsafe fn _process(&self, _owner: &Spatial, delta: f64) {} } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 1cc766a..e2ed1ac 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -1,5 +1,6 @@ mod game; mod spinning_cube; +mod BasicDie; use gdnative::prelude::{godot_init, InitHandle}; @@ -7,6 +8,7 @@ use gdnative::prelude::{godot_init, InitHandle}; fn init(handle: InitHandle) { handle.add_class::(); handle.add_class::(); + handle.add_class::(); } // macros that create the entry-points of the dynamic library. -- cgit From bc0a59f2955b1e8bfd0870860d33c04205e6389e Mon Sep 17 00:00:00 2001 From: IcECreAm777 <31211782+IcECreAm777@users.noreply.github.com> Date: Sat, 16 Jul 2022 05:58:09 +0200 Subject: applying force after pressing leftclick and dragging the mouse --- rust/src/BasicDie.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 9 deletions(-) (limited to 'rust/src') diff --git a/rust/src/BasicDie.rs b/rust/src/BasicDie.rs index 7bc6d65..3f15de5 100644 --- a/rust/src/BasicDie.rs +++ b/rust/src/BasicDie.rs @@ -14,14 +14,14 @@ enum InputState { #[inherit(RigidBody)] #[register_with(Self::register_builder)] pub struct BasicDie { - #[property(path="base/camera_offset")] - camera_offset: f32, - #[property(path="base/camera_clamp")] + #[property(path="camera/camera_clamp")] camera_clamp: Vector2, #[property(path="shooting/max_force")] max_force: f32, #[property(path="shooting/up_angle")] up_angle: f32, + #[property(path="shooting/stopping_velocity")] + stopping_velocity: f32, #[property(path="input/mouse_sensitivity")] mouse_sensitivity: Vector2, @@ -32,6 +32,7 @@ pub struct BasicDie { node_camera_arm_horizontal: Option>, node_camera_arm_vertical: Option>, + node_camera: Option>, } #[methods] @@ -43,10 +44,10 @@ impl BasicDie { fn new(_owner: &RigidBody) -> Self { BasicDie { - camera_offset: 0.0, camera_clamp: Vector2 { x: 0.0, y: 0.0 }, max_force: 0.0, up_angle: 5.0, + stopping_velocity: 0.0, mouse_sensitivity: Vector2 { x: 1.0, y: 1.0 }, input_state: InputState::Default, @@ -56,6 +57,7 @@ impl BasicDie { node_camera_arm_horizontal: None, node_camera_arm_vertical: None, + node_camera: None, } } @@ -96,13 +98,41 @@ impl BasicDie { }, _ => godot_warn!("No horizontal arm to look for the vertical arm") } + + // look for the camera + match self.node_camera_arm_vertical { + Some(arm) => { + let save_arm = arm.assume_safe(); + match save_arm.get_node(NodePath::from_str("Camera")) { + Some(node) => { + let save_node = node.assume_safe(); + match save_node.cast::() { + Some(casted) => { + let save_casted = casted.claim(); + self.node_camera = Some(save_casted)}, + _ => godot_warn!("Camera was not of type 'Spatial'"), + } + }, + _ => godot_warn!("No camera found.") + } + }, + _ => godot_warn!("No vertical arm to look for the camera") + } } #[export] unsafe fn _physics_process(&mut self, owner: &RigidBody, delta: f64) { if matches!(self.input_state, InputState::Moving) { - // TODO check if velocity reached a certain threshold and set the mode to default again + // get the current velocity + let current_vel = owner.linear_velocity().length(); + + godot_print!("current velocity: {}", current_vel); + + // check if the velocity is less than the threshold and change input state in that case + if current_vel <= self.stopping_velocity { + self.input_state = InputState::Default; + } }; } @@ -113,7 +143,7 @@ impl BasicDie { match self.input_state { InputState::Default => self.default_input(event), - InputState::Shooting => self.shooting_input(event), + InputState::Shooting => self.shooting_input(owner, event), InputState::Moving => self.moving_input(event), } } @@ -158,14 +188,14 @@ impl BasicDie { } /// this input method will be called when player is currently taking a shot - unsafe fn shooting_input(&mut self, event: Ref) { + unsafe fn shooting_input(&mut self, owner: &RigidBody, event: Ref) { let save_event = event.assume_safe(); godot_print!("shooting input"); if save_event.is_action_released(GodotString::from_str(&self.action_shooting), false) { self.input_state = InputState::Moving; - // TODO add force based on input and strength + self.shoot(owner); return; } @@ -222,7 +252,6 @@ impl BasicDie { // check for the current rotation let save_arm = arm.assume_safe(); let current_rot = save_arm.rotation(); - godot_print!("current rotation: {} | {} | {} ", current_rot.x, current_rot.y, current_rot.z); // clamp the rotation if current_rot.x + input > self.camera_clamp.x || current_rot.x + input <= self.camera_clamp.y { @@ -236,4 +265,28 @@ impl BasicDie { _ => godot_warn!("No vertical camera arm assigned") } } + + unsafe fn shoot(&mut self, owner: &RigidBody) { + // make sure the camera actually exists + match self.node_camera { + Some(cam) => { + // get the base position of the node + let base_pos = owner.transform().origin; + + // get the save reference + let save_cam = cam.assume_safe(); + + // get the forward vector of the camera setting the up angle to the defined value in the editor + let mut forward_vector = save_cam.global_transform().basis.c(); + forward_vector.y = self.up_angle; + + // calculate the impulse force + let impulse = forward_vector.normalized() * self.current_force; + + // actually add the force + owner.apply_impulse(base_pos, impulse); + }, + None => godot_warn!("No camera assigned!"), + } + } } -- cgit From 5678a04042b7b2ea52a927101cf66064535dc272 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 16 Jul 2022 11:42:39 +0200 Subject: add separate shooting intensity --- rust/src/BasicDie.rs | 292 -------------------------------------------- rust/src/basic_die.rs | 331 ++++++++++++++++++++++++++++++++++++++++++++++++++ rust/src/game.rs | 2 +- rust/src/lib.rs | 4 +- 4 files changed, 334 insertions(+), 295 deletions(-) delete mode 100644 rust/src/BasicDie.rs create mode 100644 rust/src/basic_die.rs (limited to 'rust/src') diff --git a/rust/src/BasicDie.rs b/rust/src/BasicDie.rs deleted file mode 100644 index 3f15de5..0000000 --- a/rust/src/BasicDie.rs +++ /dev/null @@ -1,292 +0,0 @@ -use std::borrow::Borrow; -use gdnative::api::*; -use gdnative::prelude::*; - -/// the input state for the player -enum InputState { - Default, - Shooting, - Moving -} - -/// the basic die used by the player -#[derive(NativeClass)] -#[inherit(RigidBody)] -#[register_with(Self::register_builder)] -pub struct BasicDie { - #[property(path="camera/camera_clamp")] - camera_clamp: Vector2, - #[property(path="shooting/max_force")] - max_force: f32, - #[property(path="shooting/up_angle")] - up_angle: f32, - #[property(path="shooting/stopping_velocity")] - stopping_velocity: f32, - #[property(path="input/mouse_sensitivity")] - mouse_sensitivity: Vector2, - - input_state: InputState, - current_force: f32, - - action_shooting: String, - - node_camera_arm_horizontal: Option>, - node_camera_arm_vertical: Option>, - node_camera: Option>, -} - -#[methods] -impl BasicDie { - // Register the builder for methods, properties and/or signals. - fn register_builder(_builder: &ClassBuilder) { - godot_print!("BasicDie builder is registered!"); - } - - fn new(_owner: &RigidBody) -> Self { - BasicDie { - camera_clamp: Vector2 { x: 0.0, y: 0.0 }, - max_force: 0.0, - up_angle: 5.0, - stopping_velocity: 0.0, - mouse_sensitivity: Vector2 { x: 1.0, y: 1.0 }, - - input_state: InputState::Default, - current_force: 0.0, - - action_shooting: String::from("mouse_btn_left"), - - node_camera_arm_horizontal: None, - node_camera_arm_vertical: None, - node_camera: None, - } - } - - #[export] - unsafe fn _ready(&mut self, owner: &RigidBody) { - owner.set_physics_process(true); - - // look for the horizontal camera arm - match owner.get_node(NodePath::from_str("CameraArmHorizontal")) { - Some(node) => { - let save_node = node.assume_safe(); - match save_node.cast::() { - Some(casted) => { - let save_casted = casted.claim(); - self.node_camera_arm_horizontal = Some(save_casted)}, - _ => godot_warn!("Camera Arm was not of type 'Spatial'"), - } - }, - _ => godot_warn!("No child node called 'Camera found'") - } - - // look for the vertical camera arm - match self.node_camera_arm_horizontal { - Some(arm) => { - let save_arm = arm.assume_safe(); - match save_arm.get_node(NodePath::from_str("CameraArmVertical")) { - Some(node) => { - let save_node = node.assume_safe(); - match save_node.cast::() { - Some(casted) => { - let save_casted = casted.claim(); - self.node_camera_arm_vertical = Some(save_casted)}, - _ => godot_warn!("Camera Arm was not of type 'Spatial'"), - } - }, - _ => godot_warn!("No vertical arm found.") - } - }, - _ => godot_warn!("No horizontal arm to look for the vertical arm") - } - - // look for the camera - match self.node_camera_arm_vertical { - Some(arm) => { - let save_arm = arm.assume_safe(); - match save_arm.get_node(NodePath::from_str("Camera")) { - Some(node) => { - let save_node = node.assume_safe(); - match save_node.cast::() { - Some(casted) => { - let save_casted = casted.claim(); - self.node_camera = Some(save_casted)}, - _ => godot_warn!("Camera was not of type 'Spatial'"), - } - }, - _ => godot_warn!("No camera found.") - } - }, - _ => godot_warn!("No vertical arm to look for the camera") - } - } - - #[export] - unsafe fn _physics_process(&mut self, owner: &RigidBody, delta: f64) { - - if matches!(self.input_state, InputState::Moving) { - // get the current velocity - let current_vel = owner.linear_velocity().length(); - - godot_print!("current velocity: {}", current_vel); - - // check if the velocity is less than the threshold and change input state in that case - if current_vel <= self.stopping_velocity { - self.input_state = InputState::Default; - } - }; - } - - #[export] - unsafe fn _input(&mut self, owner: &RigidBody, event: Ref) { - - self.general_input(event.borrow()); - - match self.input_state { - InputState::Default => self.default_input(event), - InputState::Shooting => self.shooting_input(owner, event), - InputState::Moving => self.moving_input(event), - } - } - - /// this input method will always be called, regardless of the input state - unsafe fn general_input(&mut self, event: &Ref) { - let save_event = event.assume_safe(); - - // get the input as mouse input - let mouse_event = save_event.cast::(); - match mouse_event { - Some(motion_event) => { - let x_mov = motion_event.relative().x * self.mouse_sensitivity.x; - self.rotate_cam_horizontal(x_mov); - }, - _ => {} - } - } - - /// this input method will be called when looking around, before taking a shot - unsafe fn default_input(&mut self, event: Ref) { - let save_event = event.assume_safe(); - - godot_print!("default input"); - - // left mouse button was pressed => switch to shooting mode - if save_event.is_action_pressed(GodotString::from_str(&self.action_shooting), false, false) { - godot_print!("mouse_button, switching to shooting mode"); - self.input_state = InputState::Shooting; - return; - } - - // get the input as mouse input - let mouse_event = save_event.cast::(); - match mouse_event { - Some(motion_event) => { - let y_mov = -motion_event.relative().y * self.mouse_sensitivity.y; - self.rotate_cam_vertical(y_mov); - }, - _ => {} - }; - } - - /// this input method will be called when player is currently taking a shot - unsafe fn shooting_input(&mut self, owner: &RigidBody, event: Ref) { - let save_event = event.assume_safe(); - - godot_print!("shooting input"); - - if save_event.is_action_released(GodotString::from_str(&self.action_shooting), false) { - self.input_state = InputState::Moving; - self.shoot(owner); - return; - } - - // get the input as mouse input - let mouse_event = save_event.cast::(); - match mouse_event { - Some(motion_event) => { - let y_mov = motion_event.relative().y * self.mouse_sensitivity.y; - self.current_force = match self.current_force + y_mov { - x if x < 0.0 => 0.0, - x if x > self.max_force => self.max_force, - _ => self.current_force + y_mov - }; - - godot_print!("current force: {}", self.current_force); - }, - _ => {} - }; - } - - /// this input method will be called when player is moving - unsafe fn moving_input(&mut self, event: Ref) { - let save_event = event.assume_safe(); - - godot_print!("moving input"); - - // get the input as mouse input - let mouse_event = save_event.cast::(); - match mouse_event { - Some(motion_event) => { - let y_mov = motion_event.relative().y * self.mouse_sensitivity.y; - self.rotate_cam_vertical(y_mov); - }, - _ => {} - }; - } - - unsafe fn rotate_cam_horizontal(&mut self, input: f32) { - // make sure the arm exists - match self.node_camera_arm_horizontal { - Some(arm) => { - // rotate the horizontal camera arm - let save_arm = arm.assume_safe(); - save_arm.rotate_object_local(Vector3 {x: 0.0, y: 1.0, z: 0.0}, input as f64) - }, - _ => godot_warn!("No horizontal camera arm assigned.") - } - } - - unsafe fn rotate_cam_vertical(&mut self, input: f32) { - // make sure the camera arm actually exists - match self.node_camera_arm_vertical { - Some(arm) => { - // check for the current rotation - let save_arm = arm.assume_safe(); - let current_rot = save_arm.rotation(); - - // clamp the rotation - if current_rot.x + input > self.camera_clamp.x || current_rot.x + input <= self.camera_clamp.y { - return; - } - - // actually rotate if possible - let save_arm = arm.assume_safe(); - save_arm.rotate_object_local(Vector3 {x: 1.0, y: 0.0, z: 0.0}, input as f64) - }, - _ => godot_warn!("No vertical camera arm assigned") - } - } - - unsafe fn shoot(&mut self, owner: &RigidBody) { - // make sure the camera actually exists - match self.node_camera { - Some(cam) => { - // get the base position of the node - let base_pos = owner.transform().origin; - - // get the save reference - let save_cam = cam.assume_safe(); - - // get the forward vector of the camera setting the up angle to the defined value in the editor - let mut forward_vector = save_cam.global_transform().basis.c(); - forward_vector.y = self.up_angle; - - // calculate the impulse force - let impulse = forward_vector.normalized() * self.current_force; - - // actually add the force - owner.apply_impulse(base_pos, impulse); - }, - None => godot_warn!("No camera assigned!"), - } - } -} diff --git a/rust/src/basic_die.rs b/rust/src/basic_die.rs new file mode 100644 index 0000000..9dab3ce --- /dev/null +++ b/rust/src/basic_die.rs @@ -0,0 +1,331 @@ +use std::borrow::Borrow; +use gdnative::api::*; +use gdnative::prelude::*; + +/// the input state for the player +enum InputState { + Default, + Shooting, + Moving +} + +/// the basic die used by the player +#[derive(NativeClass)] +#[inherit(RigidBody)] +#[register_with(Self::register_builder)] +pub struct BasicDie { + #[property(path="camera/camera_clamp")] + camera_clamp: Vector2, + #[property(path="shooting/max_force")] + max_force: f32, + #[property(path="shooting/up_angle")] + up_angle: f32, + #[property(path="shooting/stopping_velocity")] + stopping_velocity: f32, + #[property(path="input/camera_mouse_sensitivity")] + mouse_sensitivity: Vector2, + #[property(path="input/shoot_sensitivity")] + shoot_sensitivity: f32, + + input_state: InputState, + current_force: f32, + + action_shooting: String, + + node_camera_arm_horizontal: Option>, + node_camera_arm_vertical: Option>, + node_camera: Option>, +} + +#[methods] +impl BasicDie { + // Register the builder for methods, properties and/or signals. + fn register_builder(_builder: &ClassBuilder) { + godot_print!("BasicDie builder is registered!"); + } + + fn new(_owner: &RigidBody) -> Self { + BasicDie { + camera_clamp: Vector2 { x: 0.0, y: 0.0 }, + max_force: 0.0, + up_angle: 5.0, + stopping_velocity: 0.0, + mouse_sensitivity: Vector2 { x: 1.0, y: 1.0 }, + shoot_sensitivity: 1.0, + + input_state: InputState::Default, + current_force: 0.0, + + action_shooting: String::from("mouse_btn_left"), + + node_camera_arm_horizontal: None, + node_camera_arm_vertical: None, + node_camera: None, + } + } + + #[export] + unsafe fn _ready(&mut self, owner: &RigidBody) { + owner.set_physics_process(true); + + // look for the vertical camera arm + match self.node_camera_arm_horizontal { + Some(arm) => { + let save_arm = arm.assume_safe(); + match save_arm.get_node(NodePath::from_str("CameraArmVertical")) { + Some(node) => { + let save_node = node.assume_safe(); + match save_node.cast::() { + Some(casted) => { + let save_casted = casted.claim(); + self.node_camera_arm_vertical = Some(save_casted)}, + _ => godot_warn!("Camera Arm was not of type 'Spatial'"), + } + }, + _ => godot_warn!("No vertical arm found.") + } + }, + _ => godot_warn!("No horizontal arm to look for the vertical arm") + } + + // look for the camera + match self.node_camera_arm_vertical { + Some(arm) => { + let save_arm = arm.assume_safe(); + match save_arm.get_node(NodePath::from_str("Camera")) { + Some(node) => { + let save_node = node.assume_safe(); + match save_node.cast::() { + Some(casted) => { + let save_casted = casted.claim(); + self.node_camera = Some(save_casted)}, + _ => godot_warn!("Camera was not of type 'Spatial'"), + } + }, + _ => godot_warn!("No camera found.") + } + }, + _ => godot_warn!("No vertical arm to look for the camera") + } + } + + #[export] + unsafe fn _physics_process(&mut self, owner: &RigidBody, _delta: f64) { + + // detect if the die stops moving + if matches!(self.input_state, InputState::Moving) { + // get the current velocity + let current_vel = owner.linear_velocity().length(); + + godot_print!("current velocity: {}", current_vel); + + // check if the velocity is less than the threshold and change input state in that case + if current_vel <= self.stopping_velocity { + self.input_state = InputState::Default; + } + }; + } + + #[export] + unsafe fn _input(&mut self, owner: &RigidBody, event: Ref) { + + self.general_input(event.borrow()); + + match self.input_state { + InputState::Default => self.default_input(event), + InputState::Shooting => self.shooting_input(owner, event), + InputState::Moving => self.moving_input(event), + } + + // look for the horizontal camera arm + match owner.get_node(NodePath::from_str("CameraArmHorizontal")) { + Some(node) => { + let save_node = node.assume_safe(); + match save_node.cast::() { + Some(casted) => { + let save_casted = casted.claim(); + self.node_camera_arm_horizontal = Some(save_casted)}, + _ => godot_warn!("Camera Arm was not of type 'Spatial'"), + } + }, + _ => godot_warn!("No horizontal arm found") + } + + // look for the vertical camera arm + match self.node_camera_arm_horizontal { + Some(arm) => { + let save_arm = arm.assume_safe(); + match save_arm.get_node(NodePath::from_str("CameraArmVertical")) { + Some(node) => { + let save_node = node.assume_safe(); + match save_node.cast::() { + Some(casted) => { + let save_casted = casted.claim(); + self.node_camera_arm_vertical = Some(save_casted)}, + _ => godot_warn!("Camera Arm was not of type 'Spatial'"), + } + }, + _ => godot_warn!("No vertical arm found.") + } + }, + _ => godot_warn!("No horizontal arm to look for the vertical arm") + } + + // look for the camera + match self.node_camera_arm_vertical { + Some(arm) => { + let save_arm = arm.assume_safe(); + match save_arm.get_node(NodePath::from_str("Camera")) { + Some(node) => { + let save_node = node.assume_safe(); + match save_node.cast::() { + Some(casted) => { + let save_casted = casted.claim(); + self.node_camera = Some(save_casted)}, + _ => godot_warn!("Camera was not of type 'Spatial'"), + } + }, + _ => godot_warn!("No camera found.") + } + }, + _ => godot_warn!("No vertical arm to look for the camera") + } + } + + /// this input method will always be called, regardless of the input state + unsafe fn general_input(&mut self, event: &Ref) { + let save_event = event.assume_safe(); + + // rotate camera horizontally + let mouse_event = save_event.cast::(); // get the input as mouse input + match mouse_event { + Some(motion_event) => { + let x_mov = motion_event.relative().x * self.mouse_sensitivity.x; + self.rotate_cam_horizontal(x_mov); + }, + _ => {} + } + } + + /// this input method will be called when looking around, before taking a shot + unsafe fn default_input(&mut self, event: Ref) { + let save_event = event.assume_safe(); + + // left mouse button was pressed => switch to shooting mode + if save_event.is_action_pressed(GodotString::from_str(&self.action_shooting), false, false) { + godot_print!("mouse_button, switching to shooting mode"); + self.input_state = InputState::Shooting; + return; + } + + // rotate camera vertically + let mouse_event = save_event.cast::(); // get the input as mouse input + match mouse_event { + Some(motion_event) => { + let y_mov = -motion_event.relative().y * self.mouse_sensitivity.y; + self.rotate_cam_vertical(y_mov); + }, + _ => {} + }; + } + + /// this input method will be called when player is currently taking a shot + unsafe fn shooting_input(&mut self, owner: &RigidBody, event: Ref) { + let save_event = event.assume_safe(); + + // mouse released, shoot + if save_event.is_action_released(GodotString::from_str(&self.action_shooting), false) { + self.input_state = InputState::Moving; + self.shoot(owner); + return; + } + + // charge shot with vertical mouse movement + let mouse_event = save_event.cast::(); // get the input as mouse input + match mouse_event { + Some(motion_event) => { + let y_mov = motion_event.relative().y * self.mouse_sensitivity.y; + self.current_force = match self.current_force + y_mov { + x if x < 0.0 => 0.0, + x if x > self.max_force => self.max_force, + x => x + }; + + godot_print!("current force: {}", self.current_force); + }, + _ => {} + }; + } + + /// this input method will be called when player is moving + unsafe fn moving_input(&mut self, event: Ref) { + let save_event = event.assume_safe(); + + // rotate camera vertically + let mouse_event = save_event.cast::(); // get the input as mouse input + match mouse_event { + Some(motion_event) => { + let y_mov = motion_event.relative().y * self.mouse_sensitivity.y; + self.rotate_cam_vertical(y_mov); + }, + _ => {} + }; + } + + unsafe fn rotate_cam_horizontal(&mut self, input: f32) { + // make sure the arm exists + match self.node_camera_arm_horizontal { + Some(arm) => { + // rotate the horizontal camera arm + let save_arm = arm.assume_safe(); + save_arm.rotate_object_local(Vector3 {x: 0.0, y: 1.0, z: 0.0}, input as f64) + }, + _ => godot_warn!("No horizontal camera arm assigned.") + } + } + + unsafe fn rotate_cam_vertical(&mut self, input: f32) { + // make sure the camera arm actually exists + match self.node_camera_arm_vertical { + Some(arm) => { + // check for the current rotation + let save_arm = arm.assume_safe(); + let current_rot = save_arm.rotation(); + + // clamp the rotation + if current_rot.x + input > self.camera_clamp.x || current_rot.x + input <= self.camera_clamp.y { + return; + } + + // actually rotate if possible + let save_arm = arm.assume_safe(); + save_arm.rotate_object_local(Vector3 {x: 1.0, y: 0.0, z: 0.0}, input as f64) + }, + _ => godot_warn!("No vertical camera arm assigned") + } + } + + unsafe fn shoot(&mut self, owner: &RigidBody) { + // make sure the camera actually exists + match self.node_camera { + Some(cam) => { + // get the base position of the node + let base_pos = owner.transform().origin; + + // get the save reference + let save_cam = cam.assume_safe(); + + // get the forward vector of the camera setting the up angle to the defined value in the editor + let mut forward_vector = save_cam.global_transform().basis.c(); + forward_vector.y = self.up_angle; + + // calculate the impulse force + let impulse = forward_vector.normalized() * self.current_force; + + // actually add the force + owner.apply_impulse(base_pos, impulse); + }, + None => godot_warn!("No camera assigned!"), + } + } +} diff --git a/rust/src/game.rs b/rust/src/game.rs index 16c93d8..61b23ed 100644 --- a/rust/src/game.rs +++ b/rust/src/game.rs @@ -40,5 +40,5 @@ impl Game { // This function will be called in every frame #[export] - unsafe fn _process(&self, _owner: &Spatial, delta: f64) {} + unsafe fn _process(&self, _owner: &Spatial, _delta: f64) { } } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index e2ed1ac..1041272 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -1,6 +1,6 @@ mod game; mod spinning_cube; -mod BasicDie; +mod basic_die; use gdnative::prelude::{godot_init, InitHandle}; @@ -8,7 +8,7 @@ use gdnative::prelude::{godot_init, InitHandle}; fn init(handle: InitHandle) { handle.add_class::(); handle.add_class::(); - handle.add_class::(); + handle.add_class::(); } // macros that create the entry-points of the dynamic library. -- cgit