From d1bac1c41a75fdf901e1f7c5f604783f0685190a Mon Sep 17 00:00:00 2001 From: David Date: Sat, 16 Jul 2022 20:38:58 +0200 Subject: make buffs work --- rust/src/buff_ball.rs | 91 ++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 48 deletions(-) (limited to 'rust/src/buff_ball.rs') 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 { -- cgit