aboutsummaryrefslogtreecommitdiffstats
path: root/rust/src
diff options
context:
space:
mode:
Diffstat (limited to 'rust/src')
-rw-r--r--rust/src/basic_die.rs (renamed from rust/src/BasicDie.rs)101
-rw-r--r--rust/src/game.rs2
-rw-r--r--rust/src/lib.rs4
3 files changed, 73 insertions, 34 deletions
diff --git a/rust/src/BasicDie.rs b/rust/src/basic_die.rs
index 3f15de5..9dab3ce 100644
--- a/rust/src/BasicDie.rs
+++ b/rust/src/basic_die.rs
@@ -22,8 +22,10 @@ pub struct BasicDie {
up_angle: f32,
#[property(path="shooting/stopping_velocity")]
stopping_velocity: f32,
- #[property(path="input/mouse_sensitivity")]
+ #[property(path="input/camera_mouse_sensitivity")]
mouse_sensitivity: Vector2,
+ #[property(path="input/shoot_sensitivity")]
+ shoot_sensitivity: f32,
input_state: InputState,
current_force: f32,
@@ -49,6 +51,7 @@ impl BasicDie {
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,
@@ -65,20 +68,6 @@ impl BasicDie {
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::<Spatial>() {
- 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) => {
@@ -121,8 +110,9 @@ impl BasicDie {
}
#[export]
- unsafe fn _physics_process(&mut self, owner: &RigidBody, delta: f64) {
+ 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();
@@ -146,14 +136,68 @@ impl BasicDie {
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::<Spatial>() {
+ 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::<Spatial>() {
+ 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::<Spatial>() {
+ 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<InputEvent>) {
let save_event = event.assume_safe();
- // get the input as mouse input
- let mouse_event = save_event.cast::<InputEventMouseMotion>();
+ // rotate camera horizontally
+ let mouse_event = save_event.cast::<InputEventMouseMotion>(); // get the input as mouse input
match mouse_event {
Some(motion_event) => {
let x_mov = motion_event.relative().x * self.mouse_sensitivity.x;
@@ -167,8 +211,6 @@ impl BasicDie {
unsafe fn default_input(&mut self, event: Ref<InputEvent>) {
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");
@@ -176,8 +218,8 @@ impl BasicDie {
return;
}
- // get the input as mouse input
- let mouse_event = save_event.cast::<InputEventMouseMotion>();
+ // rotate camera vertically
+ let mouse_event = save_event.cast::<InputEventMouseMotion>(); // get the input as mouse input
match mouse_event {
Some(motion_event) => {
let y_mov = -motion_event.relative().y * self.mouse_sensitivity.y;
@@ -191,23 +233,22 @@ impl BasicDie {
unsafe fn shooting_input(&mut self, owner: &RigidBody, event: Ref<InputEvent>) {
let save_event = event.assume_safe();
- godot_print!("shooting input");
-
+ // 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;
}
- // get the input as mouse input
- let mouse_event = save_event.cast::<InputEventMouseMotion>();
+ // charge shot with vertical mouse movement
+ let mouse_event = save_event.cast::<InputEventMouseMotion>(); // 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,
- _ => self.current_force + y_mov
+ x => x
};
godot_print!("current force: {}", self.current_force);
@@ -220,10 +261,8 @@ impl BasicDie {
unsafe fn moving_input(&mut self, event: Ref<InputEvent>) {
let save_event = event.assume_safe();
- godot_print!("moving input");
-
- // get the input as mouse input
- let mouse_event = save_event.cast::<InputEventMouseMotion>();
+ // rotate camera vertically
+ let mouse_event = save_event.cast::<InputEventMouseMotion>(); // get the input as mouse input
match mouse_event {
Some(motion_event) => {
let y_mov = motion_event.relative().y * self.mouse_sensitivity.y;
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::<game::Game>();
handle.add_class::<spinning_cube::SpinningCube>();
- handle.add_class::<BasicDie::BasicDie>();
+ handle.add_class::<basic_die::BasicDie>();
}
// macros that create the entry-points of the dynamic library.