diff options
Diffstat (limited to 'rust/src/buff_ball.rs')
-rw-r--r-- | rust/src/buff_ball.rs | 91 |
1 files changed, 43 insertions, 48 deletions
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<Ref<RigidBody>>, - sphere_body: Option<Ref<RigidBody>> + collision_die: Ref<CollisionShape>, + collision_sphere: Ref<CollisionShape>, + mesh_die: Ref<MeshInstance>, + mesh_sphere: Ref<MeshInstance>, } impl BuffBall { - fn new(die_body: Option<Ref<RigidBody>>, sphere_body: Option<Ref<RigidBody>>) -> Self { + + pub fn new(die_body: Box<Ref<RigidBody>>) -> Self { + + // find a collision shape + fn search<T: SubClass<Node>> (die_body: &Box<Ref<RigidBody>>, name: String) -> Option<Ref<T>> { + unsafe{ + match die_body.assume_safe().get_node(&name) { + None => { + godot_warn!("Could not find {}", name); + None + } + Some(node) => { + match node.assume_safe().cast::<T>() { + None => { + godot_warn!("{} is not a {}", name, std::any::type_name::<T>()); + None + }, + Some(cs) => Some(cs.claim()) + } + } + } + } + } + BuffBall { name: String::from("Ball"), description: String::from("Roll the dice"), - - die_body, - sphere_body + collision_die: search::<CollisionShape>(&die_body, String::from("CollisionShapeDie" )).unwrap(), + collision_sphere: search::<CollisionShape>(&die_body, String::from("CollisionShapeSphere")).unwrap(), + mesh_die: search::<MeshInstance>(&die_body, String::from("MeshDie" )).unwrap(), + mesh_sphere: search::<MeshInstance>(&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 { |