network code and stuff
This commit is contained in:
11
shared/Cargo.toml
Normal file
11
shared/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "daggmask-shared"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.8.0"
|
||||
bevy_renet = "0.0.5"
|
||||
bevy_egui = "0.15.0"
|
||||
bevy_rapier2d = "0.16.0"
|
||||
serde = { version = "1.0", features = [ "derive" ] }
|
170
shared/src/lib.rs
Normal file
170
shared/src/lib.rs
Normal file
@@ -0,0 +1,170 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_renet::renet::{
|
||||
ChannelConfig, ReliableChannelConfig, RenetConnectionConfig, UnreliableChannelConfig,
|
||||
NETCODE_KEY_BYTES,
|
||||
};
|
||||
|
||||
use bevy_rapier2d::geometry::Collider;
|
||||
use bevy_rapier2d::prelude::*;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub const PRIVATE_KEY: &[u8; NETCODE_KEY_BYTES] = b"en grisars katt hund hemlis key."; // 32-bytes
|
||||
pub const PROTOCOL_ID: u64 = 7;
|
||||
|
||||
#[derive(Debug, Component)]
|
||||
pub struct Player {
|
||||
pub id: u64,
|
||||
pub location: Vec2,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize, Component)]
|
||||
pub struct PlayerInput {
|
||||
pub most_recent_tick: Option<u32>,
|
||||
pub up: bool,
|
||||
pub down: bool,
|
||||
pub left: bool,
|
||||
pub right: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Component)]
|
||||
pub enum PlayerCommand {
|
||||
BasicAttack { cast_at: Vec2 },
|
||||
}
|
||||
|
||||
pub enum ClientChannel {
|
||||
Input,
|
||||
Command,
|
||||
}
|
||||
|
||||
pub enum ServerChannel {
|
||||
ServerMessages,
|
||||
NetworkFrame,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Component)]
|
||||
pub enum ServerMessages {
|
||||
PlayerCreate {
|
||||
entity: Entity,
|
||||
id: u64,
|
||||
translation: [f32; 3],
|
||||
},
|
||||
PlayerRemove {
|
||||
id: u64,
|
||||
},
|
||||
SpawnProjectile {
|
||||
entity: Entity,
|
||||
location: Vec2,
|
||||
direction: Vec2,
|
||||
},
|
||||
DespawnProjectile {
|
||||
entity: Entity,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||
pub struct NetworkedEntities {
|
||||
pub entities: Vec<Entity>,
|
||||
pub translations: Vec<[f32; 3]>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||
pub struct NetworkFrame {
|
||||
pub tick: u32,
|
||||
pub entities: NetworkedEntities,
|
||||
}
|
||||
|
||||
impl ClientChannel {
|
||||
pub fn id(&self) -> u8 {
|
||||
match self {
|
||||
Self::Input => 0,
|
||||
Self::Command => 1,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn channels_config() -> Vec<ChannelConfig> {
|
||||
vec![
|
||||
ReliableChannelConfig {
|
||||
channel_id: Self::Input.id(),
|
||||
message_resend_time: Duration::ZERO,
|
||||
..Default::default()
|
||||
}
|
||||
.into(),
|
||||
ReliableChannelConfig {
|
||||
channel_id: Self::Command.id(),
|
||||
message_resend_time: Duration::ZERO,
|
||||
..Default::default()
|
||||
}
|
||||
.into(),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl ServerChannel {
|
||||
pub fn id(&self) -> u8 {
|
||||
match self {
|
||||
Self::NetworkFrame => 0,
|
||||
Self::ServerMessages => 1,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn channels_config() -> Vec<ChannelConfig> {
|
||||
vec![
|
||||
UnreliableChannelConfig {
|
||||
channel_id: Self::NetworkFrame.id(),
|
||||
..Default::default()
|
||||
}
|
||||
.into(),
|
||||
ReliableChannelConfig {
|
||||
channel_id: Self::ServerMessages.id(),
|
||||
message_resend_time: Duration::from_millis(200),
|
||||
..Default::default()
|
||||
}
|
||||
.into(),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
pub fn client_connection_config() -> RenetConnectionConfig {
|
||||
RenetConnectionConfig {
|
||||
send_channels_config: ClientChannel::channels_config(),
|
||||
receive_channels_config: ServerChannel::channels_config(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn server_connection_config() -> RenetConnectionConfig {
|
||||
RenetConnectionConfig {
|
||||
send_channels_config: ServerChannel::channels_config(),
|
||||
receive_channels_config: ClientChannel::channels_config(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// set up the level
|
||||
pub fn setup_level(mut _commands: Commands) {
|
||||
info!("bygger level...");
|
||||
}
|
||||
|
||||
pub fn spawn_projectile(commands: &mut Commands, location: Vec2, direction: Vec2) -> Entity {
|
||||
commands
|
||||
.spawn()
|
||||
.insert(Collider::ball(0.1))
|
||||
.insert(Velocity::linear(direction * 10.))
|
||||
.insert(ActiveEvents::COLLISION_EVENTS)
|
||||
.insert(Projectile {
|
||||
duration: Timer::from_seconds(1.5, false),
|
||||
location,
|
||||
direction,
|
||||
})
|
||||
.id()
|
||||
}
|
||||
|
||||
#[derive(Debug, Component)]
|
||||
pub struct Projectile {
|
||||
pub duration: Timer,
|
||||
pub location: Vec2,
|
||||
pub direction: Vec2,
|
||||
}
|
Reference in New Issue
Block a user