forked from joey/godottest
42 lines
1.0 KiB
Rust
42 lines
1.0 KiB
Rust
use godot::classes::{AnimatedSprite2D, IRigidBody2D, RigidBody2D};
|
|
use godot::prelude::*;
|
|
|
|
#[derive(GodotClass)]
|
|
#[class(base=RigidBody2D)]
|
|
pub struct Mob {
|
|
// required
|
|
base: Base<RigidBody2D>,
|
|
}
|
|
|
|
#[godot_api]
|
|
impl IRigidBody2D for Mob {
|
|
fn init(base: Base<RigidBody2D>) -> Self {
|
|
Self { base }
|
|
}
|
|
|
|
fn ready(&mut self) {
|
|
self
|
|
.signals()
|
|
.body_exited()
|
|
.connect_self(Self::on_visible_on_screen_notifier_2d_screen_exited);
|
|
|
|
let mut animated_sprite_2d = self.base().get_node_as::<AnimatedSprite2D>("AnimatedSprite2D");
|
|
let types = animated_sprite_2d
|
|
.get_sprite_frames()
|
|
.expect("failed to get sprite frames??")
|
|
.get_animation_names();
|
|
|
|
let animation = Array::from(&types).pick_random().expect("no animations found");
|
|
|
|
animated_sprite_2d.set_animation(animation.stringify().arg());
|
|
animated_sprite_2d.play();
|
|
}
|
|
}
|
|
|
|
#[godot_api]
|
|
impl Mob {
|
|
fn on_visible_on_screen_notifier_2d_screen_exited(&mut self, _body: Gd<Node>) {
|
|
self.base_mut().queue_free();
|
|
}
|
|
}
|