WIP: Add rust logic for the plater physics
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
2025-01-31 19:51:01 +01:00
parent f6141f0462
commit 221734d8c7
15 changed files with 459 additions and 15 deletions

31
rust/src/lib.rs Normal file
View File

@@ -0,0 +1,31 @@
use godot::prelude::*;
use godot::classes::Sprite2D;
use godot::classes::ISprite2D;
struct MyExtension;
mod player;
#[gdextension]
unsafe impl ExtensionLibrary for MyExtension {}
#[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,
}
}
}

View File

2
rust/src/player/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
mod server_node;
mod client_node;

View File

@@ -0,0 +1,94 @@
use godot::classes::{CharacterBody3D, ICharacterBody3D};
use godot::obj::WithBaseField;
use godot::prelude::*;
#[derive(GodotClass)]
#[class(base=CharacterBody3D)]
struct PlayerServerNode {
base: Base<CharacterBody3D>,
jumping: bool,
input_direction: Vector2,
input_rotation: Vector3,
}
const JUMP_VELOCITY: f32 = 4.5;
const SPEED: f32 = 5.0;
#[godot_api]
impl ICharacterBody3D for PlayerServerNode{
fn init(base: Base<CharacterBody3D>) -> Self {
Self {
base,
jumping: false,
input_direction: Vector2::new(0.0, 0.0),
input_rotation: Vector3::new(0.0, 0.0, 0.0),
}
}
fn ready(&mut self) {}
fn physics_process(&mut self, delta: f64) {
if !self.base().is_on_floor() {
let new_gravity = self.base().get_gravity() * delta as f32;
let new_velocity = self.base().get_velocity() + new_gravity;
self.base_mut().set_velocity(new_velocity);
}
if self.base().is_on_floor() && self.jumping {
let mut new_velocity = self.base().get_velocity();
new_velocity.y = JUMP_VELOCITY;
self.base_mut().set_velocity(new_velocity);
}
self.jumping = false;
if self.base().is_on_floor() {
let direction = self.base().get_transform().basis * Vector3::new(self.input_direction.x, 0.0, self.input_direction.y);
if direction.length() > 0.0 {
let direction = direction.normalized();
let new_velocity = Vector3::new(
direction.x * SPEED,
self.base().get_velocity().y,
direction.z * SPEED,
);
self.base_mut().set_velocity(new_velocity);
} else {
let moved = self.base().get_velocity().clone().move_toward(Vector3::new(0.0, 0.0, 0.0), SPEED);
let new_velocity = Vector3::new(
moved.x,
self.base().get_velocity().y,
moved.z,
);
self.base_mut().set_velocity(new_velocity);
}
}
self.base_mut().move_and_slide();
}
}
#[godot_api]
impl PlayerServerNode {
#[rpc(any_peer, call_local, unreliable_ordered)]
fn jump(&mut self) {
self.jumping = true
}
#[rpc(any_peer, call_local, unreliable_ordered)]
fn set_input_direction(&mut self, new_input_direction: Vector2) {
self.input_direction = new_input_direction;
}
#[rpc(any_peer, call_local, unreliable_ordered)]
fn set_rotation_x(&mut self, x: f32) {
let mut new_rotation = self.base().get_rotation();
new_rotation.x = x;
self.base_mut().set_rotation(new_rotation);
}
#[rpc(any_peer, call_local, unreliable_ordered)]
fn set_rotation_y(&mut self, y: f32) {
let mut new_rotation = self.base().get_rotation();
new_rotation.y = y;
self.base_mut().set_rotation(new_rotation);
}
}