use bevy_ecs::component::Component;
use godot::{
classes::Node,
obj::{Gd, Inherits, InstanceId},
};
#[derive(Debug, Component, Clone, PartialEq, Eq)]
pub struct GodotNodeHandle {
instance_id: InstanceId,
}
impl GodotNodeHandle {
pub fn get<T: Inherits<Node>>(&mut self) -> Gd<T> {
self.try_get().unwrap_or_else(|| {
panic!(
"failed to get godot node handle as {}",
std::any::type_name::<T>()
)
})
}
pub fn try_get<T: Inherits<Node>>(&mut self) -> Option<Gd<T>> {
Gd::try_from_instance_id(self.instance_id).ok()
}
pub fn new<T: Inherits<Node>>(reference: Gd<T>) -> Self {
Self {
instance_id: reference.instance_id(),
}
}
pub fn instance_id(&self) -> InstanceId {
self.instance_id
}
pub fn from_instance_id(instance_id: InstanceId) -> Self {
Self { instance_id }
}
}