This commit is contained in:
Magnus von Wachenfeldt 2022-08-07 17:39:15 +02:00
commit e5a83229f1
Signed by: magnus
GPG Key ID: A469F7D71D09F795
9 changed files with 3370 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

3211
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

3
Cargo.toml Normal file
View File

@ -0,0 +1,3 @@
[workspace]
resolver = "2"
members = ["client", "server"]

25
client/.cargo/config.toml Normal file
View File

@ -0,0 +1,25 @@
# Add the contents of this file to `config.toml` to enable "fast build" configuration. Please read the notes below.
# NOTE: For maximum performance, build using a nightly compiler
# If you are using rust stable, remove the "-Zshare-generics=y" below.
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-Clink-arg=-fuse-ld=lld", "-Zshare-generics=y"]
# NOTE: you must manually install https://github.com/michaeleisel/zld on mac. you can easily do this with the "brew" package manager:
# `brew install michaeleisel/zld/zld`
[target.x86_64-apple-darwin]
rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/zld", "-Zshare-generics=y"]
[target.aarch64-apple-darwin]
rustflags = ["-C", "link-arg=-fuse-ld=/opt/homebrew/bin/zld", "-Zshare-generics=y"]
[target.x86_64-pc-windows-msvc]
linker = "rust-lld.exe"
rustflags = ["-Zshare-generics=n"]
# Optional: Uncommenting the following improves compile times, but reduces the amount of debug info to 'line number tables only'
# In most cases the gains are negligible, but if you are on macos and have slow compile times you should see significant gains.
#[profile.dev]
#debug = 1

16
client/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "daggmask"
version = "0.1.0"
edition = "2021"
[dependencies]
bevy = "0.8"
# Enable a small amount of optimization in debug mode
[profile.dev]
opt-level = 1
# Enable high optimizations for dependencies (incl. Bevy), but not for our code:
[profile.dev.package."*"]
opt-level = 3

59
client/src/main.rs Normal file
View File

@ -0,0 +1,59 @@
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_startup_system(spawn_player)
.add_system(move_player)
.run();
}
fn setup(mut commands: Commands) {
info!("hehe");
let mut camera_bundle = Camera2dBundle::default();
camera_bundle.projection.scale = 1. / 50.;
commands.spawn_bundle(camera_bundle);
}
#[derive(Component)]
struct Player;
fn spawn_player(mut commands: Commands) {
commands
.spawn_bundle(SpriteBundle {
sprite: Sprite {
color: Color::rgb(0., 0.47, 1.),
custom_size: Some(Vec2::new(1., 1.)),
..Default::default()
},
..Default::default()
})
.insert(Player);
}
fn move_player(keys: Res<Input<KeyCode>>, mut player_query: Query<&mut Transform, With<Player>>) {
let mut direction = Vec2::ZERO;
if keys.any_pressed([KeyCode::Up, KeyCode::W]) {
direction.y += 1.;
}
if keys.any_pressed([KeyCode::Down, KeyCode::S]) {
direction.y -= 1.;
}
if keys.any_pressed([KeyCode::Right, KeyCode::D]) {
direction.x += 1.;
}
if keys.any_pressed([KeyCode::Left, KeyCode::A]) {
direction.x -= 1.;
}
if direction == Vec2::ZERO {
return;
}
let move_speed = 0.13;
let move_delta = (direction * move_speed).extend(0.);
for mut transform in player_query.iter_mut() {
transform.translation += move_delta;
}
}

3
rust-toolchain.toml Normal file
View File

@ -0,0 +1,3 @@
[toolchain]
channel = "nightly"

7
server/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "daggmask-server"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

45
server/src/main.rs Normal file
View File

@ -0,0 +1,45 @@
use std::net::UdpSocket;
fn main() -> std::io::Result<()> {
// replace xxxx with your desired port
// replace S.S.S.S with your server address
let socket = UdpSocket::bind("S.S.S.S:xxxx")?;
let mut peers: Vec<String> = vec![];
loop {
let mut buf = [0; 1024];
let (_, src) = socket.recv_from(&mut buf)?;
let stringified_buff = String::from_utf8(buf.to_vec()).unwrap();
let stringified_buff = stringified_buff.trim_matches(char::from(0));
println!("[NEW MESSAGE]{:?} => {:?}", src, stringified_buff);
if stringified_buff != "register" {
continue;
}
if !peers.contains(&format!("{}", src)) {
peers.push(format!("{}", src));
}
for p in &peers {
let filtered_peers = filter_peers(&peers, p);
if !filtered_peers.is_empty() {
socket.send_to(filtered_peers.join(",").as_bytes(), p)?;
}
}
}
}
fn filter_peers(peers: &Vec<String>, filter: &String) -> Vec<String> {
let mut new_peers: Vec<String> = vec![];
for p in peers {
if p != filter {
new_peers.push(String::from(p));
}
}
new_peers
}