Browse Source

Added a asteroids like movement system for Player

Player will rotate but gradually slow down in rotation

Player will move forward or backward but gradually slow down
david 1 year ago
parent
commit
14f8c54e89
2 changed files with 29 additions and 10 deletions
  1. 5 2
      README.md
  2. 24 8
      rcode/src/lib.rs

+ 5 - 2
README.md

@@ -1,3 +1,6 @@
-# gr-spaceconstruct
+# Space Construct
 
-Godot + Rust = Space Construct
+\[[Git](https://git.red-green.com/david/gr-spaceconstruct)\] \[[Issues](https://git.red-green.com/david/gr-spaceconstruct/issues)\]
+
++ \[[Godot](https://beanzilla.net/godot/)\]
++ \[[Rust](https://beanzilla.net/rust/)\] (via \[[gdext](https://github.com/godot-rust/gdext)\])

+ 24 - 8
rcode/src/lib.rs

@@ -2,12 +2,11 @@ use godot::prelude::*;
 use godot::engine::{Sprite2D, ISprite2D};
 
 struct MyExtension;
-
 #[gdextension]
 unsafe impl ExtensionLibrary for MyExtension {}
 
 #[derive(GodotClass)]
-#[class(init, base=Sprite2D)]
+#[class(base=Sprite2D)]
 struct Player {
     #[export]
     speed: f64,
@@ -20,17 +19,34 @@ struct Player {
 
 #[godot_api]
 impl ISprite2D for Player {
-    /*fn init(base: Base<Sprite2D>) -> Self {
+    fn init(base: Base<Sprite2D>) -> Self {
         godot_print!("Hello world!");
         Self {
-            speed: 400.0,
-            angular_speed: std::f64::consts::PI,
+            speed: 0.0,
+            angular_speed: 0.0,
             base,
         }
-    }*/
+    }
 
     fn process(&mut self, delta: f64) {
-        let rads = (self.angular_speed * delta) as f32;
-        self.base_mut().rotate(rads);
+        if self.angular_speed != 0.0 {
+            let rads = (self.angular_speed * delta) as f32;
+            self.base_mut().rotate(rads);
+            if self.angular_speed > 0.0 {
+                self.angular_speed -= 0.01;
+            } else {
+                self.angular_speed += 0.01;
+            }
+        }
+        if self.speed != 0.0 {
+            let rot = self.base().get_rotation();
+            let vel = Vector2::UP.rotated(rot) * self.speed as f32;
+            self.base_mut().translate(vel * delta as f32);
+            if self.speed > 0.0 {
+                self.speed -= 0.1;
+            } else {
+                self.speed += 0.1;
+            }
+        }
     }
 }