aboutsummaryrefslogtreecommitdiffstats
path: root/rust/src/buff_ball.rs
blob: 219e6754fb04fa62563487b8eca6fcb12fcc7c75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use gdnative::api::*;
use gdnative::prelude::*;
use crate::buff_trait::Buff;

pub struct BuffBall {
    name: String,
    description: String,
    collision_die: Ref<CollisionShape>,
    collision_sphere: Ref<CollisionShape>,
    mesh_die: Ref<MeshInstance>,
    mesh_sphere: Ref<MeshInstance>,
}

impl BuffBall {

    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"),
            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) {
        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) {
        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 {
        GodotString::from_str(self.name)
    }

    fn get_description(self) -> GodotString {
        GodotString::from_str(self.description)
    }
}