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