Skip to content
Extraits de code Groupes Projets
Sélectionner une révision Git
1 résultat Searching

dummy.rs

Blame
  • dummy.rs 1,37 Kio
    use godot::{
        classes::{ISprite2D, Sprite2D},
        prelude::*,
    };
    
    #[derive(GodotClass)]
    #[class(base=Sprite2D)]
    struct Player {
        speed: f64,
        angular_speed: f64,
    
        base: Base<Sprite2D>,
    }
    
    #[godot_api]
    impl ISprite2D for Player {
        fn init(base: Base<Sprite2D>) -> Self {
            godot_print!("Hello, world!"); // Prints to the Godot console
    
            Self {
                speed: 400.0,
                angular_speed: std::f64::consts::PI,
                base,
            }
        }
    
        fn physics_process(&mut self, delta: f64) {
            // GDScript code:
            //
            // rotation += angular_speed * delta
            // var velocity = Vector2.UP.rotated(rotation) * speed
            // position += velocity * delta
    
            let radians = (self.angular_speed * delta) as f32;
            self.base_mut().rotate(radians);
    
            let rotation = self.base().get_rotation();
            let velocity = Vector2::UP.rotated(rotation) * self.speed as f32;
            self.base_mut().translate(velocity * delta as f32);
    
            // or verbose:
            // let this = self.base_mut();
            // this.set_position(
            //     this.position() + velocity * delta as f32
            // );
        }
    }
    
    #[godot_api]
    impl Player {
        #[func]
        fn increase_speed(&mut self, amount: f64) {
            self.speed += amount;
            self.base_mut().emit_signal("speed_increased", &[]);
        }
    
        #[signal]
        fn speed_increased();
    }