Skip to content

Commit a86e4c0

Browse files
committed
feat:develop multi manager
1 parent 995da09 commit a86e4c0

File tree

7 files changed

+87
-15
lines changed

7 files changed

+87
-15
lines changed

gdrust/Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gdrust/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ godot = { git = "https://github.com/godot-rust/gdext", branch = "master" }
99
rand = "0.8"
1010
derive = { path = "./derive" }
1111
proto = { path = "proto" }
12+
tokio = "1"
13+
anyhow = "1"
1214

1315
[lib]
1416
crate-type = ["cdylib"]

gdrust/src/global.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,16 @@ struct GlobalClass {
1818
impl INode for GlobalClass {}
1919

2020
#[godot_api()]
21-
impl GlobalClass {}
21+
impl GlobalClass {
22+
#[func]
23+
fn flush_multi(&mut self) {
24+
match &mut self.multi_manager {
25+
Some(manager) => {
26+
manager;
27+
}
28+
None => {
29+
godot_error!("MultiManager has not been initial")
30+
}
31+
}
32+
}
33+
}

gdrust/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@ mod weapons;
1111
mod zenith;
1212

1313
use godot::prelude::*;
14-
use std::panic::{set_hook, PanicInfo};
14+
use multi::{MultiManager, MultiManagerImpl};
15+
use std::{
16+
panic::{set_hook, PanicInfo},
17+
sync::{Arc, Mutex, OnceLock},
18+
};
1519

1620
struct GdExtension;
1721

22+
type MultiSingle = Arc<Mutex<MultiManagerImpl>>;
23+
fn get_multi_single() -> &'static MultiSingle {
24+
static TMP: OnceLock<MultiSingle> = OnceLock::new();
25+
TMP.get_or_init(|| Arc::new(Mutex::new(MultiManagerImpl::new())))
26+
}
27+
1828
fn panic_handler(info: &PanicInfo) {
1929
if let Some(p) = info.location() {
2030
godot_error!(

gdrust/src/multi.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,63 @@
1+
use anyhow::Ok;
12
use godot::engine::{INode, Node};
23
use godot::prelude::*;
4+
use std::collections::HashMap;
5+
use std::sync::{Arc, Mutex};
6+
use tokio::net::TcpStream;
7+
use tokio::runtime::{Builder, Runtime};
8+
9+
use crate::get_multi_single;
10+
11+
pub struct MultiPlayer {}
12+
13+
pub struct MultiManagerImpl {
14+
clients: HashMap<usize, MultiPlayer>,
15+
socket: Option<TcpStream>,
16+
runtime: Runtime,
17+
}
18+
19+
impl MultiManagerImpl {
20+
fn set_connection(&mut self, socket: TcpStream) {
21+
if self.socket.is_some() {
22+
godot_warn!("Socket has value,but reset")
23+
}
24+
self.socket = Some(socket)
25+
}
26+
27+
pub fn connect_to_server(&mut self, ip: &str) -> anyhow::Result<()> {
28+
Ok(())
29+
}
30+
}
31+
32+
impl MultiManagerImpl {
33+
pub fn new() -> Self {
34+
Self {
35+
clients: HashMap::new(),
36+
socket: None,
37+
runtime: Builder::new_multi_thread().enable_all().build().unwrap(),
38+
}
39+
}
40+
}
341

442
#[derive(GodotClass)]
543
#[class(base = Node)]
644
pub struct MultiManager {
745
base: Base<Node>,
46+
multi_impl: Arc<Mutex<MultiManagerImpl>>,
847
}
948

1049
#[godot_api()]
1150
impl INode for MultiManager {
1251
fn init(base: Base<Node>) -> Self {
13-
Self { base }
52+
Self {
53+
base,
54+
multi_impl: get_multi_single().clone(),
55+
}
1456
}
1557
}
58+
59+
#[godot_api()]
60+
impl MultiManager {
61+
#[func]
62+
fn add_new_player(&mut self) {}
63+
}

gdrust/src/ui/multi_enter.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ impl MultiEnter {
2929
#[func]
3030
fn connect_to_server(&mut self, ip: String) -> bool {
3131
let global = self.base().get_node_as::<Node>("/root/Global");
32-
let ret;
33-
(self.socket, ret) = match TcpStream::connect(&ip) {
34-
Ok(socket) => (Some(socket), true),
35-
Err(error) => {
36-
self.show_dialog(format!("Connect failed:{}", error));
37-
(None, false)
38-
}
39-
};
32+
let ret = true;
33+
// (self.socket, ret) = match TcpStream::connect(&ip) {
34+
// Ok(socket) => (Some(socket), true),
35+
// Err(error) => {
36+
// self.show_dialog(format!("Connect failed:{}", error));
37+
// (None, false)
38+
// }
39+
// };
4040
ret
4141
}
4242

scripts/multi_game/enter.gd

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ func _on_pressed() -> void:
1515
Global.connected_ip = $"../Input/IPBox/IP".text
1616
Global.player_name = $"../Input/PlayerBox/Player".text
1717
if Global.connected_ip.is_empty():
18-
$WrongDialog.dialog_text = "Ip address is empty"
19-
$WrongDialog.popup_centered()
18+
self.show_dialog("Ip address is empty")
2019
return
2120
if Global.player_name.is_empty():
22-
$WrongDialog.dialog_text = "Player name is empty"
23-
$WrongDialog.popup_centered()
21+
self.show_dialog("Player name is empty")
2422
return
2523
var state = self.connect_to_server(Global.connected_ip)
2624
if state:

0 commit comments

Comments
 (0)