11 个版本 (7 个不兼容版本)

0.8.2 2019年1月14日
0.7.1 2019年1月4日

#1004 in 游戏开发

Download history 27/week @ 2024-03-28 14/week @ 2024-04-04 5/week @ 2024-06-27 52/week @ 2024-07-04

每月下载量 57

MIT 许可证

40KB
908

Victorem

为 Rust 开发简单 2D 和 3D 在线游戏原型而设计的易用 UDP 游戏服务器和客户端框架。

示例

Cargo.toml

[dependencies]
victorem = "0.8.2"

客户端

use victorem;
use std::time::{Duration, Instant};

fn main() {
    let mut client = victorem::ClientSocket::new(11111, "127.0.0.1:22222").unwrap();
    let mut id: u32 = 0;
    let mut timer = Instant::now();
    let period = Duration::from_millis(100);
    loop {
        if timer.elapsed() > period {
            timer = Instant::now();
            id += 1;
            let _ = client.send(format!("Ping {}", id).into_bytes());
        }
        let _ = client
            .recv()
            .map(|v| String::from_utf8(v).unwrap_or(String::new()))
            .map(|s| println!("From Server: {}", s));
    }
}

服务器

use victorem;
use std::net::SocketAddr;
use std::time::Duration;

struct PingPongGame {
    id: u32,
}

impl victorem::Game for PingPongGame {
    fn handle_command(
        &mut self,
        delta_time: Duration,
        commands: Vec<Vec<u8>>,
        from: SocketAddr,
    ) -> victorem::ContinueRunning {
        for v in commands {
            println!(
                "From Client: {:?} {} {}",
                delta_time,
                from,
                String::from_utf8(v).unwrap_or(String::new()),
            );
        }
        true
    }

    fn draw(&mut self, delta_time: Duration) -> Vec<u8> {
        self.id += 1;
        format!("Pong {} {:?}", self.id, delta_time).into_bytes()
    }
}

fn main() {
    let mut server = victorem::GameServer::new(PingPongGame { id: 0 }, 22222).unwrap();
    server.run();
}

依赖

~1–1.7MB
~37K SLoC