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 --- cargo | 0 godot/native/BasicDie.gdns | 8 + godot/project.godot | 8 + godot/scenes/Game.tscn | 29 ++- .../code_with_your_friends2022.dll | Bin 0 -> 477696 bytes rust/src/BasicDie.rs | 239 +++++++++++++++++++++ rust/src/game.rs | 4 +- rust/src/lib.rs | 2 + 8 files changed, 280 insertions(+), 10 deletions(-) create mode 100644 cargo create mode 100644 godot/native/BasicDie.gdns create mode 100644 lib/x86_64-pc-windows-msvc/code_with_your_friends2022.dll create mode 100644 rust/src/BasicDie.rs diff --git a/cargo b/cargo new file mode 100644 index 0000000..e69de29 diff --git a/godot/native/BasicDie.gdns b/godot/native/BasicDie.gdns new file mode 100644 index 0000000..ab5d534 --- /dev/null +++ b/godot/native/BasicDie.gdns @@ -0,0 +1,8 @@ +[gd_resource type="NativeScript" load_steps=2 format=2] + +[ext_resource path="res://native/game.gdnlib" type="GDNativeLibrary" id=1] + +[resource] +resource_name = "BasicDie" +class_name = "BasicDie" +library = ExtResource( 1 ) diff --git a/godot/project.godot b/godot/project.godot index 50f9a46..8439ce7 100644 --- a/godot/project.godot +++ b/godot/project.godot @@ -18,6 +18,14 @@ config/name="code-with-your-friends2022" run/main_scene="res://scenes/Game.tscn" config/icon="res://assets/godot-ferris-32x32.png" +[input] + +mouse_btn_left={ +"deadzone": 0.5, +"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":1,"pressed":false,"doubleclick":false,"script":null) + ] +} + [rendering] environment/default_environment="res://default_env.tres" diff --git a/godot/scenes/Game.tscn b/godot/scenes/Game.tscn index a37a7eb..320f926 100644 --- a/godot/scenes/Game.tscn +++ b/godot/scenes/Game.tscn @@ -1,18 +1,33 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=5 format=2] [ext_resource path="res://native/Game.gdns" type="Script" id=1] -[ext_resource path="res://scenes/SpinningCube.tscn" type="PackedScene" id=2] +[ext_resource path="res://native/BasicDie.gdns" type="Script" id=2] [sub_resource type="CubeMesh" id=1] +[sub_resource type="BoxShape" id=2] + [node name="Game" type="Spatial"] script = ExtResource( 1 ) -[node name="Camera" type="Camera" parent="."] -transform = Transform( 0.572229, -0.327396, 0.751909, 0, 0.916856, 0.399217, -0.820094, -0.228443, 0.524651, 4.71648, 2.5, 3.45846 ) -current = true +[node name="RigidBody" type="RigidBody" parent="."] +script = ExtResource( 2 ) +base/camera_offset = 500.0 +base/camera_clamp = Vector2( 0, -1 ) +shooting/max_force = 100.0 +shooting/up_angle = 5.0 +input/mouse_sensitivity = Vector2( 0.05, 0.05 ) -[node name="SpinningCube" parent="." instance=ExtResource( 2 )] +[node name="MeshInstance" type="MeshInstance" parent="RigidBody"] mesh = SubResource( 1 ) material/0 = null -base/rotate_speed = 1.0 + +[node name="CollisionShape" type="CollisionShape" parent="RigidBody"] +shape = SubResource( 2 ) + +[node name="CameraArmHorizontal" type="Spatial" parent="RigidBody"] + +[node name="CameraArmVertical" type="Spatial" parent="RigidBody/CameraArmHorizontal"] + +[node name="Camera" type="Camera" parent="RigidBody/CameraArmHorizontal/CameraArmVertical"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 10 ) diff --git a/lib/x86_64-pc-windows-msvc/code_with_your_friends2022.dll b/lib/x86_64-pc-windows-msvc/code_with_your_friends2022.dll new file mode 100644 index 0000000..ceaed1c Binary files /dev/null and b/lib/x86_64-pc-windows-msvc/code_with_your_friends2022.dll differ 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 7a003485fbbad7354416a6a6fe086ebb47103511 Mon Sep 17 00:00:00 2001 From: IcECreAm777 <31211782+IcECreAm777@users.noreply.github.com> Date: Sat, 16 Jul 2022 04:20:25 +0200 Subject: ignoring local bat file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5146acf..c0cdeaa 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ rust/Cargo.lock rust/debug/ target/ tramp +make.bat -- 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 --- godot/scenes/Game.tscn | 23 +++++-- .../code_with_your_friends2022.dll | Bin 477696 -> 482304 bytes rust/src/BasicDie.rs | 71 ++++++++++++++++++--- 3 files changed, 81 insertions(+), 13 deletions(-) diff --git a/godot/scenes/Game.tscn b/godot/scenes/Game.tscn index 320f926..f569f85 100644 --- a/godot/scenes/Game.tscn +++ b/godot/scenes/Game.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=7 format=2] [ext_resource path="res://native/Game.gdns" type="Script" id=1] [ext_resource path="res://native/BasicDie.gdns" type="Script" id=2] @@ -7,15 +7,20 @@ [sub_resource type="BoxShape" id=2] +[sub_resource type="CubeMesh" id=3] + +[sub_resource type="BoxShape" id=4] + [node name="Game" type="Spatial"] script = ExtResource( 1 ) [node name="RigidBody" type="RigidBody" parent="."] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.25589, 0 ) script = ExtResource( 2 ) -base/camera_offset = 500.0 -base/camera_clamp = Vector2( 0, -1 ) +camera/camera_clamp = Vector2( 0, -1.5 ) shooting/max_force = 100.0 -shooting/up_angle = 5.0 +shooting/up_angle = 1.0 +shooting/stopping_velocity = 10.0 input/mouse_sensitivity = Vector2( 0.05, 0.05 ) [node name="MeshInstance" type="MeshInstance" parent="RigidBody"] @@ -31,3 +36,13 @@ shape = SubResource( 2 ) [node name="Camera" type="Camera" parent="RigidBody/CameraArmHorizontal/CameraArmVertical"] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 10 ) + +[node name="StaticBody" type="StaticBody" parent="."] +transform = Transform( 100, 0, 0, 0, 0.1, 0, 0, 0, 100, 0, 0, 0 ) + +[node name="MeshInstance" type="MeshInstance" parent="StaticBody"] +mesh = SubResource( 3 ) +material/0 = null + +[node name="CollisionShape" type="CollisionShape" parent="StaticBody"] +shape = SubResource( 4 ) diff --git a/lib/x86_64-pc-windows-msvc/code_with_your_friends2022.dll b/lib/x86_64-pc-windows-msvc/code_with_your_friends2022.dll index ceaed1c..115f39f 100644 Binary files a/lib/x86_64-pc-windows-msvc/code_with_your_friends2022.dll and b/lib/x86_64-pc-windows-msvc/code_with_your_friends2022.dll differ 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 --- godot/scenes/Game.tscn | 8 +- .../libcode_with_your_friends2022.so | Bin 25309816 -> 28011904 bytes rust/src/BasicDie.rs | 292 ------------------ rust/src/basic_die.rs | 331 +++++++++++++++++++++ rust/src/game.rs | 2 +- rust/src/lib.rs | 4 +- 6 files changed, 338 insertions(+), 299 deletions(-) delete mode 100644 rust/src/BasicDie.rs create mode 100644 rust/src/basic_die.rs diff --git a/godot/scenes/Game.tscn b/godot/scenes/Game.tscn index f569f85..ca90458 100644 --- a/godot/scenes/Game.tscn +++ b/godot/scenes/Game.tscn @@ -17,11 +17,11 @@ script = ExtResource( 1 ) [node name="RigidBody" type="RigidBody" parent="."] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.25589, 0 ) script = ExtResource( 2 ) -camera/camera_clamp = Vector2( 0, -1.5 ) -shooting/max_force = 100.0 +camera/camera_clamp = Vector2( 1.608, -2.526 ) +shooting/max_force = 30.0 shooting/up_angle = 1.0 -shooting/stopping_velocity = 10.0 -input/mouse_sensitivity = Vector2( 0.05, 0.05 ) +input/camera_mouse_sensitivity = Vector2( 0.01, 0.008 ) +input/shoot_sensitivity = 0.069 [node name="MeshInstance" type="MeshInstance" parent="RigidBody"] mesh = SubResource( 1 ) 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 140affc..dcceb97 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/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 From fe8d9853148e7d3a798d9e14cb0288f7538f88ba Mon Sep 17 00:00:00 2001 From: David Date: Sat, 16 Jul 2022 14:34:11 +0200 Subject: new node structure --- godot/project.godot | 4 ++ godot/scenes/Game.tscn | 53 ++-------------------- godot/scenes/levels/test_scene/TestScene.tscn | 11 ----- godot/scenes/levels/test_scene_movement/Floor.tscn | 22 +++++++++ .../test_scene_movement/TestSceneMovement.tscn | 32 +++++++++++++ godot/scenes/levels/test_scene_movement/Wall.tscn | 23 ++++++++++ godot/scenes/objects/Camera.tscn | 10 ++++ godot/scenes/objects/Player.tscn | 14 ++++++ 8 files changed, 108 insertions(+), 61 deletions(-) delete mode 100644 godot/scenes/levels/test_scene/TestScene.tscn create mode 100644 godot/scenes/levels/test_scene_movement/Floor.tscn create mode 100644 godot/scenes/levels/test_scene_movement/TestSceneMovement.tscn create mode 100644 godot/scenes/levels/test_scene_movement/Wall.tscn create mode 100644 godot/scenes/objects/Camera.tscn create mode 100644 godot/scenes/objects/Player.tscn diff --git a/godot/project.godot b/godot/project.godot index d7b88c5..0191f34 100644 --- a/godot/project.godot +++ b/godot/project.godot @@ -8,6 +8,10 @@ config_version=4 +_global_script_classes=[ ] +_global_script_class_icons={ +} + [application] config/name="code-with-your-friends2022" diff --git a/godot/scenes/Game.tscn b/godot/scenes/Game.tscn index 0eb902d..72fef4e 100644 --- a/godot/scenes/Game.tscn +++ b/godot/scenes/Game.tscn @@ -1,56 +1,9 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=3 format=2] [ext_resource path="res://native/Game.gdns" type="Script" id=1] -[ext_resource path="res://native/BasicDie.gdns" type="Script" id=2] -[ext_resource path="res://scenes/levels/test_scene_uuuhhh/TestSceneUuuhhh.tscn" type="PackedScene" id=2] - -[sub_resource type="CubeMesh" id=1] - -[sub_resource type="BoxShape" id=2] - -[sub_resource type="CubeMesh" id=3] - -[sub_resource type="BoxShape" id=4] +[ext_resource path="res://scenes/levels/test_scene_movement/TestSceneMovement.tscn" type="PackedScene" id=2] [node name="Game" type="Spatial"] script = ExtResource( 1 ) -[node name="Camera" type="Camera" parent="."] -transform = Transform( -0.538224, 0.336461, -0.772728, 0, 0.916856, 0.399217, 0.842802, 0.214868, -0.493474, -6.58972, 3.31005, -7.31211 ) -current = true -fov = 64.6875 - -[node name="TestSceneUuuhhh" parent="." instance=ExtResource( 2 )] - -[node name="RigidBody" type="RigidBody" parent="."] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.25589, 0 ) -script = ExtResource( 2 ) -camera/camera_clamp = Vector2( 1.608, -2.526 ) -shooting/max_force = 30.0 -shooting/up_angle = 1.0 -input/camera_mouse_sensitivity = Vector2( 0.01, 0.008 ) -input/shoot_sensitivity = 0.069 - -[node name="MeshInstance" type="MeshInstance" parent="RigidBody"] -mesh = SubResource( 1 ) -material/0 = null - -[node name="CollisionShape" type="CollisionShape" parent="RigidBody"] -shape = SubResource( 2 ) - -[node name="CameraArmHorizontal" type="Spatial" parent="RigidBody"] - -[node name="CameraArmVertical" type="Spatial" parent="RigidBody/CameraArmHorizontal"] - -[node name="Camera" type="Camera" parent="RigidBody/CameraArmHorizontal/CameraArmVertical"] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 10 ) - -[node name="StaticBody" type="StaticBody" parent="."] -transform = Transform( 100, 0, 0, 0, 0.1, 0, 0, 0, 100, 0, 0, 0 ) - -[node name="MeshInstance" type="MeshInstance" parent="StaticBody"] -mesh = SubResource( 3 ) -material/0 = null - -[node name="CollisionShape" type="CollisionShape" parent="StaticBody"] -shape = SubResource( 4 ) +[node name="TestScene" parent="." instance=ExtResource( 2 )] diff --git a/godot/scenes/levels/test_scene/TestScene.tscn b/godot/scenes/levels/test_scene/TestScene.tscn deleted file mode 100644 index ed981f8..0000000 --- a/godot/scenes/levels/test_scene/TestScene.tscn +++ /dev/null @@ -1,11 +0,0 @@ -[gd_scene load_steps=3 format=2] - -[ext_resource path="res://scenes/levels/test_scene/Floor.tscn" type="PackedScene" id=1] -[ext_resource path="res://scenes/W8.tscn" type="PackedScene" id=2] - -[node name="TestScene" type="Spatial"] - -[node name="Floor" parent="." instance=ExtResource( 1 )] - -[node name="RigidBody" parent="." instance=ExtResource( 2 )] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0339947, 2.91518, -1.40456 ) diff --git a/godot/scenes/levels/test_scene_movement/Floor.tscn b/godot/scenes/levels/test_scene_movement/Floor.tscn new file mode 100644 index 0000000..f8970a7 --- /dev/null +++ b/godot/scenes/levels/test_scene_movement/Floor.tscn @@ -0,0 +1,22 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://assets/textures/grass-texture-26.jpg" type="Texture" id=1] + +[sub_resource type="CubeMesh" id=1] + +[sub_resource type="SpatialMaterial" id=3] +albedo_texture = ExtResource( 1 ) + +[sub_resource type="BoxShape" id=2] + +[node name="StaticBody" type="StaticBody"] + +[node name="MeshInstance" type="MeshInstance" parent="."] +transform = Transform( 10, 0, 0, 0, 1, 0, 0, 0, 10, 0, 0, 0 ) +mesh = SubResource( 1 ) +skeleton = NodePath("../CollisionShape") +material/0 = SubResource( 3 ) + +[node name="CollisionShape" type="CollisionShape" parent="."] +transform = Transform( 10, 0, 0, 0, 1, 0, 0, 0, 10, 0, 0, 0 ) +shape = SubResource( 2 ) diff --git a/godot/scenes/levels/test_scene_movement/TestSceneMovement.tscn b/godot/scenes/levels/test_scene_movement/TestSceneMovement.tscn new file mode 100644 index 0000000..688f1c7 --- /dev/null +++ b/godot/scenes/levels/test_scene_movement/TestSceneMovement.tscn @@ -0,0 +1,32 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://scenes/levels/test_scene_uuuhhh/Wall.tscn" type="PackedScene" id=1] +[ext_resource path="res://scenes/levels/test_scene_uuuhhh/Floor.tscn" type="PackedScene" id=2] +[ext_resource path="res://scenes/objects/Player.tscn" type="PackedScene" id=3] + +[node name="TestScene" type="Spatial"] + +[node name="Floor" parent="." instance=ExtResource( 2 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0290833, -0.981747, 0.00208664 ) + +[node name="Floor2" parent="." instance=ExtResource( 2 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, -20 ) + +[node name="Floor3" parent="." instance=ExtResource( 2 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 20, -1, 0 ) + +[node name="Floor4" parent="." instance=ExtResource( 2 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 20, -1, -20 ) + +[node name="Floor5" parent="." instance=ExtResource( 2 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 40, -1, 0 ) + +[node name="Floor6" parent="." instance=ExtResource( 2 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 40, -1, -20 ) + +[node name="Wall" parent="." instance=ExtResource( 1 )] +transform = Transform( 0.766044, 0, 0.642788, 0, 2, 0, -0.642788, 0, 0.766044, 19.47, 0, -14 ) + +[node name="PlayerRoot" parent="." instance=ExtResource( 3 )] +input/camera_mouse_sensitivity = Vector2( 0.014, 0.01 ) +input/shoot_sensitivity = 0.041 diff --git a/godot/scenes/levels/test_scene_movement/Wall.tscn b/godot/scenes/levels/test_scene_movement/Wall.tscn new file mode 100644 index 0000000..e3afe23 --- /dev/null +++ b/godot/scenes/levels/test_scene_movement/Wall.tscn @@ -0,0 +1,23 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://assets/textures/sandstone-brick-wall-texture.jpg" type="Texture" id=1] + +[sub_resource type="CubeMesh" id=1] + +[sub_resource type="SpatialMaterial" id=2] +albedo_texture = ExtResource( 1 ) + +[sub_resource type="ConvexPolygonShape" id=3] +points = PoolVector3Array( 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1 ) + +[node name="StaticBody" type="StaticBody"] + +[node name="MeshInstance" type="MeshInstance" parent="."] +transform = Transform( 0.3, 0, 0, 0, 1, 0, 0, 0, 5, 0, 1, 0 ) +mesh = SubResource( 1 ) +skeleton = NodePath("") +material/0 = SubResource( 2 ) + +[node name="CollisionShape" type="CollisionShape" parent="."] +transform = Transform( 0.3, 0, 0, 0, 1, 0, 0, 0, 5, 0, 1, 0 ) +shape = SubResource( 3 ) diff --git a/godot/scenes/objects/Camera.tscn b/godot/scenes/objects/Camera.tscn new file mode 100644 index 0000000..1c13dd0 --- /dev/null +++ b/godot/scenes/objects/Camera.tscn @@ -0,0 +1,10 @@ +[gd_scene format=2] + +[node name="CameraRoot" type="Spatial"] + +[node name="CameraArmHorizontal" type="Spatial" parent="."] + +[node name="CameraArmVertical" type="Spatial" parent="CameraArmHorizontal"] + +[node name="Camera" type="Camera" parent="CameraArmHorizontal/CameraArmVertical"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 10 ) diff --git a/godot/scenes/objects/Player.tscn b/godot/scenes/objects/Player.tscn new file mode 100644 index 0000000..0d162f4 --- /dev/null +++ b/godot/scenes/objects/Player.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://scenes/objects/W8.tscn" type="PackedScene" id=1] +[ext_resource path="res://scenes/objects/Camera.tscn" type="PackedScene" id=2] +[ext_resource path="res://native/BasicDie.gdns" type="Script" id=3] + +[node name="PlayerRoot" type="Spatial"] +script = ExtResource( 3 ) + +[node name="W8" parent="." instance=ExtResource( 1 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.1, 0 ) + +[node name="Camera" parent="." instance=ExtResource( 2 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0 ) -- cgit From bb721621d661ab4e068a589a853c2420b7ac6566 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 16 Jul 2022 14:36:27 +0200 Subject: change player script to work with new structure --- .../libcode_with_your_friends2022.so | Bin 25714184 -> 28423064 bytes rust/src/basic_die.rs | 197 ++++++++------------- 2 files changed, 78 insertions(+), 119 deletions(-) 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 14b6058..d878485 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 9dab3ce..0b028b8 100644 --- a/rust/src/basic_die.rs +++ b/rust/src/basic_die.rs @@ -8,10 +8,13 @@ enum InputState { Shooting, Moving } + + +type SpatialRef = Option>; /// the basic die used by the player #[derive(NativeClass)] -#[inherit(RigidBody)] +#[inherit(Spatial)] #[register_with(Self::register_builder)] pub struct BasicDie { #[property(path="camera/camera_clamp")] @@ -32,9 +35,12 @@ pub struct BasicDie { action_shooting: String, - node_camera_arm_horizontal: Option>, - node_camera_arm_vertical: Option>, - node_camera: Option>, + node_die: SpatialRef, + + node_camera_root: SpatialRef, + node_camera_arm_horizontal: SpatialRef, + node_camera_arm_vertical: SpatialRef, + node_camera: SpatialRef, } #[methods] @@ -44,7 +50,7 @@ impl BasicDie { godot_print!("BasicDie builder is registered!"); } - fn new(_owner: &RigidBody) -> Self { + fn new(_owner: &Spatial) -> Self { BasicDie { camera_clamp: Vector2 { x: 0.0, y: 0.0 }, max_force: 0.0, @@ -58,6 +64,9 @@ impl BasicDie { action_shooting: String::from("mouse_btn_left"), + node_die: None, + + node_camera_root: None, node_camera_arm_horizontal: None, node_camera_arm_vertical: None, node_camera: None, @@ -65,58 +74,57 @@ impl BasicDie { } #[export] - unsafe fn _ready(&mut self, owner: &RigidBody) { + unsafe fn _ready(&mut self, owner: &Spatial) { + godot_print!("Player starting"); 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.") + let search_node = | parent: &SpatialRef, name: String | { + let parent_node = match parent { + Some(node) => node.assume_safe().as_ref(), + None => owner + }; + match parent_node.get_node(NodePath::from_str(&name)) { + None => { godot_warn!("Could not find {}.", name); None }, + Some(node) => { + let save_node = node.assume_safe(); + match save_node.cast::() { + None => { godot_warn!("{} was not of type 'Spatial'.", name); None }, + Some(casted) => Some(casted.claim()) + } } - }, - _ => 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") - } + // look for the camera and arm nodes + self.node_camera_root = search_node(&None, String::from("Camera")); + self.node_camera_arm_horizontal = search_node(&self.node_camera_root, String::from("CameraArmHorizontal")); + self.node_camera_arm_vertical = search_node(&self.node_camera_arm_horizontal, String::from("CameraArmVertical")); + self.node_camera = search_node(&self.node_camera_arm_vertical, String::from("Camera")); + + godot_print!("{:?}", self.node_camera_root); + godot_print!("{:?}", self.node_camera_arm_horizontal); + godot_print!("{:?}", self.node_camera_arm_vertical); + godot_print!("{:?}", self.node_camera); + + self.node_die = search_node(&None, String::from("W8")); + + godot_print!("Player is ready"); } #[export] - unsafe fn _physics_process(&mut self, owner: &RigidBody, _delta: f64) { + unsafe fn _physics_process(&mut self, _owner: &Spatial, _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(); - + 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() + } + }; godot_print!("current velocity: {}", current_vel); // check if the velocity is less than the threshold and change input state in that case @@ -127,69 +135,15 @@ impl BasicDie { } #[export] - unsafe fn _input(&mut self, owner: &RigidBody, event: Ref) { + unsafe fn _input(&mut self, _owner: &Spatial, 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::Shooting => self.shooting_input(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 @@ -230,13 +184,13 @@ impl BasicDie { } /// this input method will be called when player is currently taking a shot - unsafe fn shooting_input(&mut self, owner: &RigidBody, event: Ref) { + unsafe fn shooting_input(&mut self, 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); + self.shoot(); return; } @@ -305,27 +259,32 @@ impl BasicDie { } } - unsafe fn shoot(&mut self, owner: &RigidBody) { + unsafe fn shoot(&mut self) { + // make sure the camera actually exists - match self.node_camera { + let impulse_dir = match self.node_camera { + None => { godot_warn!("No camera assigned!"); return; }, 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(); + let mut forward_vector = cam.assume_safe().global_transform().basis.c(); forward_vector.y = self.up_angle; // calculate the impulse force - let impulse = forward_vector.normalized() * self.current_force; + forward_vector.normalized() * self.current_force + } + }; - // actually add the force - owner.apply_impulse(base_pos, impulse); - }, - None => godot_warn!("No camera assigned!"), - } + // get the die + 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 + } + }; + + // actually add the force + die.apply_impulse(die.transform().origin, impulse_dir); } } -- cgit From 635cfebff3ec0ce5b74ac728a5655339755efe39 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 16 Jul 2022 15:12:11 +0200 Subject: better mouse handling. correct impulse --- .../test_scene_movement/TestSceneMovement.tscn | 2 -- godot/scenes/objects/Player.tscn | 6 ++++++ .../libcode_with_your_friends2022.so | Bin 28423064 -> 28904944 bytes rust/src/basic_die.rs | 19 +++++++++++++++---- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/godot/scenes/levels/test_scene_movement/TestSceneMovement.tscn b/godot/scenes/levels/test_scene_movement/TestSceneMovement.tscn index 688f1c7..d23236b 100644 --- a/godot/scenes/levels/test_scene_movement/TestSceneMovement.tscn +++ b/godot/scenes/levels/test_scene_movement/TestSceneMovement.tscn @@ -28,5 +28,3 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 40, -1, -20 ) transform = Transform( 0.766044, 0, 0.642788, 0, 2, 0, -0.642788, 0, 0.766044, 19.47, 0, -14 ) [node name="PlayerRoot" parent="." instance=ExtResource( 3 )] -input/camera_mouse_sensitivity = Vector2( 0.014, 0.01 ) -input/shoot_sensitivity = 0.041 diff --git a/godot/scenes/objects/Player.tscn b/godot/scenes/objects/Player.tscn index 0d162f4..f8a3fba 100644 --- a/godot/scenes/objects/Player.tscn +++ b/godot/scenes/objects/Player.tscn @@ -6,6 +6,12 @@ [node name="PlayerRoot" type="Spatial"] script = ExtResource( 3 ) +camera/camera_clamp = Vector2( 0, -2 ) +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 [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 d878485..4b82952 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 0b028b8..ffd3bff 100644 --- a/rust/src/basic_die.rs +++ b/rust/src/basic_die.rs @@ -75,7 +75,8 @@ impl BasicDie { #[export] unsafe fn _ready(&mut self, owner: &Spatial) { - godot_print!("Player starting"); + Input::godot_singleton().set_mouse_mode(Input::MOUSE_MODE_CAPTURED); + owner.set_physics_process(true); let search_node = | parent: &SpatialRef, name: String | { @@ -125,11 +126,11 @@ impl BasicDie { Some(rb) => rb.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; + godot_print!("Die stopped moving"); } }; } @@ -159,6 +160,16 @@ impl BasicDie { }, _ => {} } + + let key_event = save_event.cast::(); + match key_event { + Some(key_event) => { + if key_event.scancode() == GlobalConstants::KEY_ESCAPE { + Input::godot_singleton().set_mouse_mode(Input::MOUSE_MODE_VISIBLE); + } + }, + _ => {} + } } /// this input method will be called when looking around, before taking a shot @@ -198,7 +209,7 @@ impl BasicDie { 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; + let y_mov = motion_event.relative().y * self.shoot_sensitivity; 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, @@ -266,7 +277,7 @@ impl BasicDie { None => { godot_warn!("No camera assigned!"); return; }, Some(cam) => { // get the forward vector of the camera setting the up angle to the defined value in the editor - let mut forward_vector = cam.assume_safe().global_transform().basis.c(); + let mut forward_vector = -cam.assume_safe().global_transform().basis.c(); forward_vector.y = self.up_angle; // calculate the impulse force -- cgit 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 From bdf6f0328e870c308299f37155b9e89d91a2859c Mon Sep 17 00:00:00 2001 From: David Date: Sat, 16 Jul 2022 16:01:31 +0200 Subject: fix up angle and force --- godot/scenes/objects/Player.tscn | 2 +- .../libcode_with_your_friends2022.so | Bin 29349872 -> 29349984 bytes rust/src/basic_die.rs | 3 ++- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/godot/scenes/objects/Player.tscn b/godot/scenes/objects/Player.tscn index 9730147..aeb8f51 100644 --- a/godot/scenes/objects/Player.tscn +++ b/godot/scenes/objects/Player.tscn @@ -8,7 +8,7 @@ script = ExtResource( 3 ) camera/camera_clamp = Vector2( 0, -1.3 ) shooting/max_force = 30.0 -shooting/up_angle = 1.0 +shooting/up_angle = 0.3 shooting/stopping_velocity = 0.003 input/camera_mouse_sensitivity = Vector2( 0.003, 0.002 ) input/shoot_sensitivity = 0.1 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 97b6350..03ade02 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 ab53720..2a61197 100644 --- a/rust/src/basic_die.rs +++ b/rust/src/basic_die.rs @@ -219,6 +219,7 @@ impl BasicDie { if save_event.is_action_released(GodotString::from_str(&self.action_shooting), false) { self.input_state = InputState::Moving; self.shoot(); + self.current_force = 0.0; return; } @@ -294,7 +295,7 @@ impl BasicDie { None => { godot_warn!("No camera assigned!"); return; }, Some(cam) => { // get the forward vector of the camera setting the up angle to the defined value in the editor - let mut forward_vector = -cam.assume_safe().global_transform().basis.c(); + let mut forward_vector = -cam.assume_safe().global_transform().basis.c().normalized(); forward_vector.y = self.up_angle; // calculate the impulse force -- cgit From 36db665defbfc1394691ecc25d750e3c7583bbe3 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 16 Jul 2022 16:18:30 +0200 Subject: remove scenes from lfs --- .gitattributes | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitattributes b/.gitattributes index 4257496..f37160a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -14,5 +14,4 @@ thirdparty/* linguist-vendored *.godot filter=lfs diff=lfs merge=lfs -text *.material filter=lfs diff=lfs merge=lfs -text *.import filter=lfs diff=lfs merge=lfs -text -*.tscn filter=lfs diff=lfs merge=lfs -text *.mp3 filter=lfs diff=lfs merge=lfs -text -- cgit From d1bac1c41a75fdf901e1f7c5f604783f0685190a Mon Sep 17 00:00:00 2001 From: David Date: Sat, 16 Jul 2022 20:38:58 +0200 Subject: make buffs work --- .../test_scene_movement/TestSceneMovement.tscn | 1 + godot/scenes/objects/W8.tscn | 21 ++++- .../libcode_with_your_friends2022.so | Bin 29349984 -> 30388072 bytes rust/src/basic_die.rs | 41 ++++++++++ rust/src/buff_ball.rs | 91 ++++++++++----------- rust/src/buff_bounce.rs | 52 +++++------- rust/src/buff_extra.rs | 4 +- rust/src/buff_phase.rs | 27 ++---- 8 files changed, 130 insertions(+), 107 deletions(-) diff --git a/godot/scenes/levels/test_scene_movement/TestSceneMovement.tscn b/godot/scenes/levels/test_scene_movement/TestSceneMovement.tscn index d23236b..d1c08e3 100644 --- a/godot/scenes/levels/test_scene_movement/TestSceneMovement.tscn +++ b/godot/scenes/levels/test_scene_movement/TestSceneMovement.tscn @@ -28,3 +28,4 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 40, -1, -20 ) transform = Transform( 0.766044, 0, 0.642788, 0, 2, 0, -0.642788, 0, 0.766044, 19.47, 0, -14 ) [node name="PlayerRoot" parent="." instance=ExtResource( 3 )] +input/current_buff_index = 1 diff --git a/godot/scenes/objects/W8.tscn b/godot/scenes/objects/W8.tscn index 91f58eb..8766011 100644 --- a/godot/scenes/objects/W8.tscn +++ b/godot/scenes/objects/W8.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://assets/game_objects/W8baseColor_Mat.material" type="Material" id=2] @@ -20,12 +20,27 @@ surfaces/0 = { [sub_resource type="ConvexPolygonShape" id=2] points = PoolVector3Array( -0.0850063, -0.879389, -0.0282584, 0.0288993, 0.929375, 0.0289349, 0.0288993, 0.929375, -0.0289349, 0.929367, -0.0289314, 0.0289314, -0.0289784, -0.0289338, 0.929338, -0.929369, 0.028929, 0.028929, 0.0288993, -0.0289349, -0.929375, 0.0288993, -0.929375, 0.0289349, -0.0850063, 0.0282584, -0.879389, 0.0282247, 0.084959, 0.87945, 0.857489, 0.056933, -0.0855785, -0.879454, -0.0849424, 0.0282549, 0.0570775, -0.885814, -0.0571088, 0.0570775, -0.0571088, 0.885814, -0.0571532, 0.885743, -0.0571043, -0.0571532, 0.885743, 0.0571043, 0.0570775, 0.0571088, -0.885814, -0.885806, -0.057097, -0.057097, 0.879446, 0.0849494, 0.0282572, 0.857335, -0.0571018, -0.0855632, -0.0571532, 0.0571043, 0.885743, -0.0571532, -0.885743, 0.0571043, -0.885806, 0.057097, -0.057097, -0.0571532, -0.0571043, -0.885743, 0.0570775, 0.0571088, 0.885814, -0.0571532, -0.885743, -0.0571043, -0.0571532, 0.0571043, -0.885743, 0.0570775, -0.885814, 0.0571088, 0.0570775, 0.885814, 0.0571088, -0.0571532, -0.0571043, 0.885743, -0.885806, 0.057097, 0.057097, 0.0570775, 0.885814, -0.0571088, 0.0570775, -0.0571088, -0.885814, 0.857335, -0.0571018, 0.0855632, -0.885806, -0.057097, 0.057097, 0.857489, 0.056933, 0.0855785, 0.929425, 0.0287513, -0.0289333, 0.929367, -0.0289314, -0.0289314, -0.0289784, 0.929338, -0.0289338, 0.0288993, 0.0289349, -0.929375, -0.929369, -0.028929, -0.028929, -0.0283039, -0.87942, -0.0849561, 0.084933, 0.0282608, 0.879461, -0.0289784, 0.929338, 0.0289338, -0.0289784, -0.0289338, -0.929338, -0.0289784, 0.0289338, 0.929338, 0.0288993, -0.0289349, 0.929375, 0.0288993, -0.929375, -0.0289349, -0.0289784, -0.929338, 0.0289338, -0.929369, 0.028929, -0.028929, -0.0283039, 0.0849561, -0.87942, -0.879454, -0.0282549, 0.0849424, 0.879512, 0.0280816, 0.0849558, 0.879446, -0.0849494, -0.0282572, -0.0282988, -0.822574, 0.141805, 0.17021, 0.0280745, -0.794261, -0.879454, 0.0849424, -0.0282549, -0.0283039, 0.87942, 0.0849561, 0.0282247, 0.87945, -0.084959, 0.0282247, -0.084959, -0.87945, -0.879454, -0.0282549, -0.0849424, -0.0850063, 0.0282584, 0.879389, -0.0850063, 0.879389, 0.0282584, 0.084933, -0.0282608, 0.879461 ) +[sub_resource type="SphereShape" id=3] + +[sub_resource type="SphereMesh" id=4] + [node name="RigidBody" type="RigidBody"] -[node name="Cone" type="MeshInstance" parent="."] +[node name="MeshDie" type="MeshInstance" parent="."] mesh = SubResource( 1 ) skeleton = NodePath("../..") material/0 = null -[node name="CollisionShape" type="CollisionShape" parent="."] +[node name="CollisionShapeDie" type="CollisionShape" parent="."] shape = SubResource( 2 ) + +[node name="CollisionShapeSphere" type="CollisionShape" parent="."] +transform = Transform( 0.7, 0, 0, 0, 0.7, 0, 0, 0, 0.7, 0, 0, 0 ) +shape = SubResource( 3 ) +disabled = true + +[node name="MeshSphere" type="MeshInstance" parent="."] +transform = Transform( 0.7, 0, 0, 0, 0.7, 0, 0, 0, 0.7, 0, 0, 0 ) +visible = false +mesh = SubResource( 4 ) +material/0 = ExtResource( 2 ) 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 03ade02..78eca51 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 2a61197..dc425b4 100644 --- a/rust/src/basic_die.rs +++ b/rust/src/basic_die.rs @@ -1,6 +1,11 @@ use std::borrow::Borrow; use gdnative::api::*; use gdnative::prelude::*; +use crate::buff_trait::Buff; +use crate::buff_ball::BuffBall; +use crate::buff_bounce::BuffBounce; +use crate::buff_phase::BuffPhase; +use crate::buff_extra::BuffExtra; /// the input state for the player enum InputState { @@ -31,6 +36,10 @@ pub struct BasicDie { mouse_sensitivity: Vector2, #[property(path="input/shoot_sensitivity")] shoot_sensitivity: f32, + #[property(path="input/current_buff_index")] + current_buff_index: i8, + + all_buffs: [Option>; 5], input_state: InputState, current_force: f32, @@ -62,6 +71,8 @@ impl BasicDie { stopping_min_ms: 1000, mouse_sensitivity: Vector2 { x: 1.0, y: 1.0 }, shoot_sensitivity: 1.0, + current_buff_index: 0, + all_buffs: [None, None, None, None, None], input_state: InputState::Default, current_force: 0.0, @@ -116,6 +127,23 @@ impl BasicDie { self.node_die = search_node(&None, String::from("W8")); + 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.claim() + } + }; + + self.all_buffs = [ + None, + Some(Box::new(BuffBall ::new(Box::new(die)))), + Some(Box::new(BuffBounce::new(Box::new(die)))), + Some(Box::new(BuffPhase ::new(Box::new(die)))), + Some(Box::new(BuffExtra ::new())), + ]; + godot_print!("Player is ready"); } @@ -148,6 +176,13 @@ impl BasicDie { if current_vel <= self.stopping_velocity { self.input_state = InputState::Default; godot_print!("Die stopped moving at velocity {} after {} ms", current_vel, delta_ms); + + //deactivate the old buff + match self.all_buffs[self.current_buff_index as usize] { + Some(ref mut buff) => buff.revert_buff(), + None => {} + } + //TODO: find out which side is up and update the current buff index accordingly } }; } @@ -313,6 +348,12 @@ impl BasicDie { } }; + // apply the current buff + match self.all_buffs[self.current_buff_index as usize] { + Some(ref mut buff) => buff.execute_buff(), + None => {} + } + // actually add the force die.apply_impulse(die.transform().origin, impulse_dir); diff --git a/rust/src/buff_ball.rs b/rust/src/buff_ball.rs index 4b89dbb..219e675 100644 --- a/rust/src/buff_ball.rs +++ b/rust/src/buff_ball.rs @@ -2,71 +2,66 @@ use gdnative::api::*; use gdnative::prelude::*; use crate::buff_trait::Buff; -struct BuffBall { +pub struct BuffBall { name: String, description: String, - - die_body: Option>, - sphere_body: Option> + collision_die: Ref, + collision_sphere: Ref, + mesh_die: Ref, + mesh_sphere: Ref, } impl BuffBall { - fn new(die_body: Option>, sphere_body: Option>) -> Self { + + pub fn new(die_body: Box>) -> Self { + + // find a collision shape + fn search> (die_body: &Box>, name: String) -> Option> { + unsafe{ + match die_body.assume_safe().get_node(&name) { + None => { + godot_warn!("Could not find {}", name); + None + } + Some(node) => { + match node.assume_safe().cast::() { + None => { + godot_warn!("{} is not a {}", name, std::any::type_name::()); + None + }, + Some(cs) => Some(cs.claim()) + } + } + } + } + } + BuffBall { name: String::from("Ball"), description: String::from("Roll the dice"), - - die_body, - sphere_body + collision_die: search::(&die_body, String::from("CollisionShapeDie" )).unwrap(), + collision_sphere: search::(&die_body, String::from("CollisionShapeSphere")).unwrap(), + mesh_die: search::(&die_body, String::from("MeshDie" )).unwrap(), + mesh_sphere: search::(&die_body, String::from("MeshSphere")).unwrap(), } } } impl Buff for BuffBall { unsafe fn execute_buff(&mut self) { - // make sure the sphere rigid body exists - match &self.sphere_body { - Some(sphere) => { - // make sure the dice rigid body exists - match &self.die_body { - Some(die) => { - // get the safe references from the ref<> - let save_sphere = sphere.assume_safe(); - let save_die = die.assume_safe(); - - // set the properties of the rigid bodies - save_die.set_process(false); - save_sphere.set_process(true); - save_sphere.show(); - }, - None => godot_warn!("Die body not assigned") - } - }, - None => godot_warn!("Sphere body not assigned") - } + self.collision_die.assume_safe().set_disabled(true); + self.collision_sphere.assume_safe().set_disabled(false); + self.mesh_die.assume_safe().set_visible(false); + self.mesh_sphere.assume_safe().set_visible(true); + godot_print!("Ball activated"); } unsafe fn revert_buff(&mut self) { - // make sure the sphere rigid body exists - match &self.sphere_body { - Some(sphere) => { - // make sure the dice rigid body exists - match &self.die_body { - Some(die) => { - // get the safe references from the ref<> - let save_sphere = sphere.assume_safe(); - let save_die = die.assume_safe(); - - // set the properties of the rigid bodies - save_die.set_process(true); - save_sphere.set_process(false); - save_sphere.hide(); - }, - None => godot_warn!("Die body not assigned") - } - }, - None => godot_warn!("Sphere body not assigned") - } + self.collision_die.assume_safe().set_disabled(false); + self.collision_sphere.assume_safe().set_disabled(true); + self.mesh_sphere.assume_safe().set_visible(false); + self.mesh_die.assume_safe().set_visible(true); + godot_print!("Ball deactivated"); } fn get_name(self) -> GodotString { diff --git a/rust/src/buff_bounce.rs b/rust/src/buff_bounce.rs index 179642d..cb9ded8 100644 --- a/rust/src/buff_bounce.rs +++ b/rust/src/buff_bounce.rs @@ -2,22 +2,21 @@ use gdnative::api::*; use gdnative::prelude::*; use crate::buff_trait::Buff; -struct BuffBounce { +pub struct BuffBounce { name: String, description: String, - rigid_body: Option>, + rigid_body: Box>, target_bounciness: f64, previous_bounciness: f64 } impl BuffBounce { - fn new(rigid_body: Option>) -> Self { + pub fn new(rigid_body: Box>) -> Self { BuffBounce { name: String::from("Bounce"), description: String::from("Let's the die bounce more than usual."), rigid_body, - target_bounciness: 1.0, previous_bounciness: 0.0 } @@ -25,43 +24,30 @@ impl BuffBounce { } impl Buff for BuffBounce { - unsafe fn execute_buff(&mut self) { - // make sure the rigid body exists - match &self.rigid_body { - Some(body) => { - let safe_body = body.assume_safe(); - // get the physics material - match safe_body.physics_material_override() { - Some(mat) => { - let save_mat = mat.assume_safe(); - self.previous_bounciness = save_mat.bounce(); - save_mat.set_bounce(self.target_bounciness); - }, - None => godot_warn!("Physics material was not found") - } + unsafe fn execute_buff(&mut self) { + // get the physics material + match self.rigid_body.assume_safe().physics_material_override() { + Some(mat) => { + let save_mat = mat.assume_safe(); + self.previous_bounciness = save_mat.bounce(); + save_mat.set_bounce(self.target_bounciness); }, - None => godot_warn!("No rigid body initialized to apply properties to") + None => godot_warn!("Physics material was not found") } + godot_print!("Bounce activated"); } unsafe fn revert_buff(&mut self) { - // make sure the rigid body exists - match &self.rigid_body { - Some(body) => { - let safe_body = body.assume_safe(); - - // get the physics material - match safe_body.physics_material_override() { - Some(mat) => { - let save_mat = mat.assume_safe(); - save_mat.set_bounce(self.previous_bounciness); - }, - None => godot_warn!("Physics material was not found") - } + // get the physics material + match self.rigid_body.assume_safe().physics_material_override() { + Some(mat) => { + let save_mat = mat.assume_safe(); + save_mat.set_bounce(self.previous_bounciness); }, - None => godot_warn!("No rigid body initialized to apply properties to") + None => godot_warn!("Physics material was not found") } + godot_print!("Bounce deactivated"); } fn get_name(self) -> GodotString { diff --git a/rust/src/buff_extra.rs b/rust/src/buff_extra.rs index 473cdde..a7b7902 100644 --- a/rust/src/buff_extra.rs +++ b/rust/src/buff_extra.rs @@ -2,13 +2,13 @@ use gdnative::api::*; use gdnative::prelude::*; use crate::buff_trait::Buff; -struct BuffExtra { +pub struct BuffExtra { name: String, description: String, } impl BuffExtra { - fn new() -> Self { + pub fn new() -> Self { BuffExtra { name: String::from("Extra Stroke"), description: String::from("One additional stroke that doesn't count"), diff --git a/rust/src/buff_phase.rs b/rust/src/buff_phase.rs index e64508e..f69fecc 100644 --- a/rust/src/buff_phase.rs +++ b/rust/src/buff_phase.rs @@ -2,16 +2,16 @@ use gdnative::api::*; use gdnative::prelude::*; use crate::buff_trait::Buff; -struct BuffPhase { +pub struct BuffPhase { name: String, description: String, - rigid_body: Option>, + rigid_body: Box>, wall_layer_bit: i64, } impl BuffPhase { - fn new(rigid_body: Option>) -> Self { + pub fn new(rigid_body: Box>) -> Self { // calculate the bit for the mask to enable/ disable collision detection let mask_layer = 2; let mask_bit = 2_i64.pow(mask_layer - 1); @@ -20,7 +20,6 @@ impl BuffPhase { name: String::from("Phase"), description: String::from("Phases through thin fences"), rigid_body, - wall_layer_bit: mask_bit, } } @@ -28,26 +27,12 @@ impl BuffPhase { impl Buff for BuffPhase { unsafe fn execute_buff(&mut self) { - match &self.rigid_body { - Some(body) => { - // actually disable the collision to the fences - let save_body = body.assume_safe(); - save_body.set_collision_mask_bit(self.wall_layer_bit, false); - }, - None => godot_warn!("Rigid body not found") - } + // actually disable the collision to the fences + self.rigid_body.assume_safe().set_collision_mask_bit(self.wall_layer_bit, false); } unsafe fn revert_buff(& mut self) { - // make sure the rigid body exists - match &self.rigid_body { - Some(body) => { - // actually enable the collision again - let save_body = body.assume_safe(); - save_body.set_collision_mask_bit(self.wall_layer_bit, false); - }, - None => godot_warn!("Rigid body not found") - } + self.rigid_body.assume_safe().set_collision_mask_bit(self.wall_layer_bit, false); } fn get_name(self) -> GodotString { -- cgit From f561a1f69d5fda68e0a23ab04f448e9364b2968c Mon Sep 17 00:00:00 2001 From: David Date: Sat, 16 Jul 2022 21:02:56 +0200 Subject: deactivate ball after 5s --- godot/scenes/objects/Player.tscn | 1 + .../libcode_with_your_friends2022.so | Bin 30388072 -> 30387968 bytes rust/src/basic_die.rs | 20 +++++++++++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/godot/scenes/objects/Player.tscn b/godot/scenes/objects/Player.tscn index aeb8f51..8cfdd6e 100644 --- a/godot/scenes/objects/Player.tscn +++ b/godot/scenes/objects/Player.tscn @@ -12,6 +12,7 @@ shooting/up_angle = 0.3 shooting/stopping_velocity = 0.003 input/camera_mouse_sensitivity = Vector2( 0.003, 0.002 ) input/shoot_sensitivity = 0.1 +input/current_buff_index = 5 [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 78eca51..962bdb5 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 dc425b4..dbcccc8 100644 --- a/rust/src/basic_die.rs +++ b/rust/src/basic_die.rs @@ -13,7 +13,7 @@ enum InputState { Shooting, Moving } - + type SpatialRef = Option>; @@ -37,7 +37,7 @@ pub struct BasicDie { #[property(path="input/shoot_sensitivity")] shoot_sensitivity: f32, #[property(path="input/current_buff_index")] - current_buff_index: i8, + current_buff_index: i32, all_buffs: [Option>; 5], @@ -144,6 +144,7 @@ impl BasicDie { Some(Box::new(BuffExtra ::new())), ]; + godot_print!("Current Buff: {}", self.current_buff_index); godot_print!("Player is ready"); } @@ -166,8 +167,19 @@ impl BasicDie { } } - // detect if the die stops moving let delta_ms = OS::godot_singleton().get_ticks_msec() - self.last_shot_time; + + // deactivate the Ball Buff after 5 seconds + if matches!(self.input_state, InputState::Moving) + && delta_ms > 5000 + && self.current_buff_index == 1 { + match self.all_buffs[1] { + Some(ref mut buff) => buff.revert_buff(), + None => {} + } + } + + // detect if the die stops moving if matches!(self.input_state, InputState::Moving) && (delta_ms > self.stopping_min_ms) { @@ -178,6 +190,7 @@ impl BasicDie { godot_print!("Die stopped moving at velocity {} after {} ms", current_vel, delta_ms); //deactivate the old buff + godot_print!("Current Buff: {}", self.current_buff_index); match self.all_buffs[self.current_buff_index as usize] { Some(ref mut buff) => buff.revert_buff(), None => {} @@ -349,6 +362,7 @@ impl BasicDie { }; // apply the current buff + godot_print!("Current Buff: {}", self.current_buff_index); match self.all_buffs[self.current_buff_index as usize] { Some(ref mut buff) => buff.execute_buff(), None => {} -- cgit