diff options
Diffstat (limited to '')
-rw-r--r-- | rust/src/basic_die.rs | 85 |
1 files changed, 83 insertions, 2 deletions
diff --git a/rust/src/basic_die.rs b/rust/src/basic_die.rs index 2a61197..304df90 100644 --- a/rust/src/basic_die.rs +++ b/rust/src/basic_die.rs @@ -1,6 +1,12 @@ use std::borrow::Borrow; use gdnative::api::*; use gdnative::prelude::*; +use gdnative::core_types::VariantArray; +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 { @@ -8,9 +14,10 @@ enum InputState { Shooting, Moving } - + type SpatialRef = Option<Ref<Spatial>>; +type NodeRef = Option<Ref<Node>>; /// the basic die used by the player #[derive(NativeClass)] @@ -31,6 +38,10 @@ pub struct BasicDie { mouse_sensitivity: Vector2, #[property(path="input/shoot_sensitivity")] shoot_sensitivity: f32, + #[property(path="input/current_buff_index")] + current_buff_index: i32, + + all_buffs: [Option<Box<dyn Buff>>; 5], input_state: InputState, current_force: f32, @@ -44,6 +55,7 @@ pub struct BasicDie { node_camera_arm_horizontal: SpatialRef, node_camera_arm_vertical: SpatialRef, node_camera: SpatialRef, + node_loader: NodeRef, } #[methods] @@ -62,6 +74,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, @@ -75,6 +89,7 @@ impl BasicDie { node_camera_arm_horizontal: None, node_camera_arm_vertical: None, node_camera: None, + node_loader: None, } } @@ -109,13 +124,35 @@ impl BasicDie { 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")); + // look for the level loader in the hierarchy + self.node_loader = owner.get_node("/root/Game/LevelLoader"); + 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); + godot_print!("{:?}", self.node_loader); 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::<RigidBody>() { + 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!("Current Buff: {}", self.current_buff_index); godot_print!("Player is ready"); } @@ -138,8 +175,16 @@ 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 { + self.stop_die(_owner); + } + + // detect if the die stops moving if matches!(self.input_state, InputState::Moving) && (delta_ms > self.stopping_min_ms) { @@ -148,6 +193,8 @@ 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); + + self.stop_die(_owner); } }; } @@ -317,5 +364,39 @@ impl BasicDie { die.apply_impulse(die.transform().origin, impulse_dir); self.last_shot_time = OS::godot_singleton().get_ticks_msec(); + + // call the stuff in GDScript + match self.node_loader { + Some(loader) => { + let save_loader = loader.assume_safe(); + save_loader.call("add_stroke", &[]); + }, + None => godot_warn!("No node loader assigned"), + } + } + + unsafe fn stop_die(&mut self, owner: &Spatial) { + //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 => {} + } + + // get the facing direction of the die and determine the next buff based on that + match self.node_die { + Some(die) => { + let forward = die.assume_safe().global_transform().basis.c().normalized(); + godot_print!("forward: {} | {} | {}", forward.x, forward.y, forward.z); + }, + None => {} + } + + // 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 => {} + } } } |