Init commit
This commit is contained in:
265
rust/Cargo.lock
generated
Normal file
265
rust/Cargo.lock
generated
Normal file
@ -0,0 +1,265 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "gdextension-api"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af8707eca996b28193b772a4a9a28a97364bb93c97e3c313542e812f2963fb93"
|
||||
|
||||
[[package]]
|
||||
name = "gensym"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "913dce4c5f06c2ea40fc178c06f777ac89fc6b1383e90c254fafb1abe4ba3c82"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "glam"
|
||||
version = "0.28.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "779ae4bf7e8421cf91c0b3b64e7e8b40b862fba4d393f59150042de7c4965a94"
|
||||
|
||||
[[package]]
|
||||
name = "godot"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6985eb6c22a0370c40d67db1f225f62222a2e04966f76d0beb53245e745744fa"
|
||||
dependencies = [
|
||||
"godot-core",
|
||||
"godot-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "godot-bindings"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77d85815de344b419513c9854b82c49aad45c7dd9fa4fcc10302aaf6ce6e07c7"
|
||||
dependencies = [
|
||||
"gdextension-api",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "godot-cell"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4750f0da2c2286f8a0f1e2b0aeb5adb2178251086119e1a96186c34cd8857ba1"
|
||||
|
||||
[[package]]
|
||||
name = "godot-codegen"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fd9382da869c3e5f4ba9614f43157bb96f5acf3d1f8341bc25147b7aeebd49a"
|
||||
dependencies = [
|
||||
"godot-bindings",
|
||||
"heck",
|
||||
"nanoserde",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "godot-core"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d7d014ea57227d84955d9db7bba53654e5e9040f3b5a41d6aad370707cad5b7a"
|
||||
dependencies = [
|
||||
"glam",
|
||||
"godot-bindings",
|
||||
"godot-cell",
|
||||
"godot-codegen",
|
||||
"godot-ffi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "godot-ffi"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3a93bc8ea912fc629cc7c6ad889f919e99b4e7f32cd912cc65b9be2a986dc6eb"
|
||||
dependencies = [
|
||||
"gensym",
|
||||
"godot-bindings",
|
||||
"godot-codegen",
|
||||
"libc",
|
||||
"paste",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "godot-macros"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "66b16a49485735010c107030dc2e9fa8701fb3bf45194e34b14d1305ad5a8b4f"
|
||||
dependencies = [
|
||||
"godot-bindings",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"venial",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.169"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||
|
||||
[[package]]
|
||||
name = "nanoserde"
|
||||
version = "0.1.37"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5de9cf844ab1e25a0353525bd74cb889843a6215fa4a0d156fd446f4857a1b99"
|
||||
dependencies = [
|
||||
"nanoserde-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nanoserde-derive"
|
||||
version = "0.1.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e943b2c21337b7e3ec6678500687cdc741b7639ad457f234693352075c082204"
|
||||
|
||||
[[package]]
|
||||
name = "open-strike-2"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"godot",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "paste"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.93"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.38"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.96"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034"
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "1.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b3758f5e68192bb96cc8f9b7e2c2cfdabb435499a28499a42f8f984092adad4b"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "venial"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6816bc32f30bf8dd1b3adb04de8406c7bf187d2f923bd9e4c0b99365d012613f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
10
rust/Cargo.toml
Normal file
10
rust/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "open-strike-2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
godot = "0.2.3"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
1
rust/src/game_root/mod.rs
Normal file
1
rust/src/game_root/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
|
2
rust/src/globals.rs
Normal file
2
rust/src/globals.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub(crate) const DEFAULT_JUMP_VELOCITY: f32 = 4.5;
|
||||
pub(crate) const DEFAULT_CHARACTER_SPEED: f32 = 5.0;
|
10
rust/src/lib.rs
Normal file
10
rust/src/lib.rs
Normal file
@ -0,0 +1,10 @@
|
||||
use godot::prelude::*;
|
||||
struct MyExtension;
|
||||
|
||||
//mod game_root;
|
||||
mod player;
|
||||
//mod server;
|
||||
mod globals;
|
||||
|
||||
#[gdextension]
|
||||
unsafe impl ExtensionLibrary for MyExtension {}
|
1
rust/src/player/client_node.rs
Normal file
1
rust/src/player/client_node.rs
Normal file
@ -0,0 +1 @@
|
||||
|
2
rust/src/player/mod.rs
Normal file
2
rust/src/player/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
mod client_node;
|
||||
mod server_node;
|
119
rust/src/player/server_node.rs
Normal file
119
rust/src/player/server_node.rs
Normal file
@ -0,0 +1,119 @@
|
||||
use godot::classes::{CharacterBody3D, ICharacterBody3D};
|
||||
use godot::obj::WithBaseField;
|
||||
use godot::prelude::*;
|
||||
|
||||
use crate::globals;
|
||||
|
||||
#[derive(GodotClass)]
|
||||
#[class(base=CharacterBody3D)]
|
||||
struct PlayerPlaceholder {
|
||||
base: Base<CharacterBody3D>,
|
||||
#[export]
|
||||
owner_id: i32,
|
||||
jumping: bool,
|
||||
input_direction: Vector2,
|
||||
}
|
||||
|
||||
#[godot_api]
|
||||
impl ICharacterBody3D for PlayerPlaceholder {
|
||||
fn init(base: Base<CharacterBody3D>) -> Self {
|
||||
Self {
|
||||
owner_id: 0,
|
||||
base,
|
||||
jumping: false,
|
||||
input_direction: Vector2::new(0.0, 0.0),
|
||||
}
|
||||
}
|
||||
|
||||
fn ready(&mut self) {
|
||||
if let Some(mut multiplayer) = self.base().get_multiplayer() {
|
||||
self.owner_id = multiplayer.get_unique_id();
|
||||
}
|
||||
}
|
||||
|
||||
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 = globals::DEFAULT_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 * globals::DEFAULT_CHARACTER_SPEED,
|
||||
self.base().get_velocity().y,
|
||||
direction.z * globals::DEFAULT_CHARACTER_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), globals::DEFAULT_CHARACTER_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 PlayerPlaceholder {
|
||||
#[rpc(any_peer, call_local, unreliable)]
|
||||
fn jump(&mut self) {
|
||||
self.jumping = true
|
||||
}
|
||||
|
||||
#[rpc(any_peer, call_local, unreliable)]
|
||||
fn set_input_direction(&mut self, new_input_direction: Vector2) {
|
||||
self.input_direction = new_input_direction;
|
||||
}
|
||||
|
||||
#[rpc(any_peer, call_local, unreliable)]
|
||||
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);
|
||||
}
|
||||
|
||||
#[rpc(any_peer, call_local, unreliable)]
|
||||
fn set_rotation_x(&mut self, x: f32) {
|
||||
let mut camera_mount = self.base().get_node_as::<Node3D>("BulletStartingPoint");
|
||||
let mut new_rotation = camera_mount.get_rotation();
|
||||
new_rotation.x = x;
|
||||
camera_mount.set_rotation(new_rotation);
|
||||
}
|
||||
|
||||
#[rpc(any_peer, call_local, unreliable_ordered)]
|
||||
fn shoot(&mut self) {
|
||||
let bullet_starting_poing = match self.base().find_child("BulletStartingPoint") {
|
||||
Some(node) => node,
|
||||
None => return,
|
||||
};
|
||||
let casted_bullet_node = bullet_starting_poing.cast::<Node3D>();
|
||||
let mut map = match self.base().find_parent("Map") {
|
||||
Some(map) => map,
|
||||
None => return,
|
||||
};
|
||||
let args = &[
|
||||
casted_bullet_node.to_variant(),
|
||||
200.to_variant(),
|
||||
50.to_variant(),
|
||||
];
|
||||
map.call("spawn_bullet", args);
|
||||
}
|
||||
}
|
80
rust/src/server/game_server.rs
Normal file
80
rust/src/server/game_server.rs
Normal file
@ -0,0 +1,80 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use godot::classes::{ENetMultiplayerPeer, ICharacterBody3D, INode, Node};
|
||||
use godot::meta::ParamType;
|
||||
use godot::obj::{NewGd, WithBaseField};
|
||||
use godot::prelude::*;
|
||||
|
||||
use crate::globals;
|
||||
|
||||
use super::player_data::PlayerData;
|
||||
|
||||
#[derive(GodotClass)]
|
||||
#[class(base=Node)]
|
||||
struct GameServer {
|
||||
base: Base<Node>,
|
||||
#[export]
|
||||
port: i32,
|
||||
#[export]
|
||||
player_limit: i32,
|
||||
#[export]
|
||||
current_map: GString,
|
||||
#[export]
|
||||
current_player_data: Gd<PlayerData>,
|
||||
#[export]
|
||||
pub(crate) players: Dictionary
|
||||
}
|
||||
|
||||
#[godot_api]
|
||||
impl INode for GameServer {
|
||||
fn init(base: Base<Node>) -> Self {
|
||||
let players = Dictionary::new();
|
||||
Self {
|
||||
base,
|
||||
players,
|
||||
port: 27015,
|
||||
player_limit: 10,
|
||||
current_map: "lowpoly_tdm_2".into(),
|
||||
current_player_data: PlayerData::new_gd(),
|
||||
}
|
||||
}
|
||||
|
||||
fn ready(&mut self) {
|
||||
if let Some(multiplayer) = self.base().get_multiplayer() {
|
||||
let on_connected = multiplayer.callable("peer_connected");
|
||||
self.base_mut().connect("on_player_connected", &on_connected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[godot_api]
|
||||
impl GameServer {
|
||||
// Signals
|
||||
#[func]
|
||||
fn on_player_connected(&mut self, id: i32) {
|
||||
godot_print!("test");
|
||||
}
|
||||
// Main methods
|
||||
#[func]
|
||||
fn create_server(&mut self, server_only: bool) {
|
||||
let mut peer = ENetMultiplayerPeer::new_gd();
|
||||
let server = peer.create_server_ex(self.port)
|
||||
.max_clients(self.player_limit)
|
||||
.done();
|
||||
|
||||
if let Some(mut multiplayer) = self.base().get_multiplayer() {
|
||||
let id = multiplayer.get_unique_id();
|
||||
multiplayer.set_multiplayer_peer(&peer);
|
||||
let player = PlayerData::new_gd();
|
||||
self.players.set(id, player);
|
||||
}
|
||||
}
|
||||
#[func]
|
||||
fn join_server(&mut self, ip: GString, port: i32) {
|
||||
let mut peer = ENetMultiplayerPeer::new_gd();
|
||||
peer.create_client(&ip, port);
|
||||
if let Some(mut multiplayer) = self.base().get_multiplayer() {
|
||||
multiplayer.set_multiplayer_peer(&peer);
|
||||
}
|
||||
}
|
||||
}
|
2
rust/src/server/mod.rs
Normal file
2
rust/src/server/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
mod game_server;
|
||||
mod player_data;
|
21
rust/src/server/player_data.rs
Normal file
21
rust/src/server/player_data.rs
Normal file
@ -0,0 +1,21 @@
|
||||
use godot::{classes::{IResource, Resource}, obj::{Base, NoBase}, prelude::godot_api};
|
||||
use godot::prelude::*;
|
||||
|
||||
#[derive(GodotClass)]
|
||||
#[class(base=Resource)]
|
||||
pub(crate) struct PlayerData {
|
||||
base: Base<Resource>,
|
||||
#[export]
|
||||
pub(crate) id: i32,
|
||||
#[export]
|
||||
pub(crate) username: GString,
|
||||
}
|
||||
|
||||
#[godot_api]
|
||||
impl IResource for PlayerData {
|
||||
fn init(base: Base<Resource>) -> Self {
|
||||
let id = 0;
|
||||
let username = "";
|
||||
Self { base, id, username: username.into() }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user