-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
399 additions
and
158 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#[cfg(feature = "bevy")] | ||
use bevy::ecs::entity::Entity; | ||
use bincode::{Decode, Encode}; | ||
|
||
/// Bevy ECS Entity derived identification of an entity. | ||
#[derive(Debug, Encode, Decode)] | ||
pub struct EntityNet(u32); | ||
|
||
impl EntityNet { | ||
pub fn index(&self) -> u32 { | ||
self.0 | ||
} | ||
} | ||
|
||
#[cfg(feature = "bevy")] | ||
impl From<Entity> for EntityNet { | ||
fn from(entity: Entity) -> Self { | ||
Self(entity.index()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#[cfg(feature = "bevy")] | ||
use bevy::transform::components::Transform; | ||
use bincode::{Decode, Encode}; | ||
#[cfg(feature = "bevy")] | ||
use glam::Quat; | ||
use glam::{Vec2, Vec3, Vec4}; | ||
|
||
#[derive(Debug, Encode, Decode)] | ||
pub struct TransformNet { | ||
translation: Vec3Net, | ||
rotation: Vec4Net, | ||
scale: Vec3Net, | ||
} | ||
|
||
#[cfg(feature = "bevy")] | ||
impl From<Transform> for TransformNet { | ||
fn from(transform: Transform) -> Self { | ||
Self { | ||
translation: transform.translation.into(), | ||
rotation: Vec4Net { | ||
x: transform.rotation.x, | ||
y: transform.rotation.y, | ||
z: transform.rotation.z, | ||
w: transform.rotation.w, | ||
}, | ||
scale: transform.scale.into(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "bevy")] | ||
impl From<TransformNet> for Transform { | ||
fn from(transform: TransformNet) -> Self { | ||
Self { | ||
translation: transform.translation.into(), | ||
rotation: Quat::from_vec4(transform.rotation.into()), | ||
scale: transform.scale.into(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, Encode, Decode)] | ||
pub struct Vec2Net { | ||
x: f32, | ||
y: f32, | ||
} | ||
|
||
impl From<Vec2> for Vec2Net { | ||
fn from(vec: Vec2) -> Self { | ||
Self { x: vec.x, y: vec.y } | ||
} | ||
} | ||
|
||
impl From<Vec2Net> for Vec2 { | ||
fn from(vec: Vec2Net) -> Self { | ||
Self::new(vec.x, vec.y) | ||
} | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, Encode, Decode)] | ||
pub struct Vec3Net { | ||
x: f32, | ||
y: f32, | ||
z: f32, | ||
} | ||
|
||
impl From<Vec3> for Vec3Net { | ||
fn from(vec: Vec3) -> Self { | ||
Self { | ||
x: vec.x, | ||
y: vec.y, | ||
z: vec.z, | ||
} | ||
} | ||
} | ||
|
||
impl From<Vec3Net> for Vec3 { | ||
fn from(vec: Vec3Net) -> Self { | ||
Self::new(vec.x, vec.y, vec.z) | ||
} | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, Encode, Decode)] | ||
pub struct Vec4Net { | ||
x: f32, | ||
y: f32, | ||
z: f32, | ||
w: f32, | ||
} | ||
|
||
impl From<Vec4> for Vec4Net { | ||
fn from(vec: Vec4) -> Self { | ||
Self { | ||
x: vec.x, | ||
y: vec.y, | ||
z: vec.z, | ||
w: vec.w, | ||
} | ||
} | ||
} | ||
|
||
impl From<Vec4Net> for Vec4 { | ||
fn from(vec: Vec4Net) -> Self { | ||
Self::new(vec.x, vec.y, vec.z, vec.w) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::mem::size_of; | ||
|
||
use bincode::{Decode, Encode}; | ||
use de_types::path::Path; | ||
use glam::Vec2; | ||
use thiserror::Error; | ||
|
||
use super::Vec2Net; | ||
|
||
const MAX_PATH_SIZE: usize = 480; | ||
|
||
#[derive(Debug, Encode, Decode)] | ||
pub struct PathNet(Vec<Vec2Net>); | ||
|
||
impl TryFrom<&Path> for PathNet { | ||
type Error = PathError; | ||
|
||
fn try_from(path: &Path) -> Result<Self, Self::Error> { | ||
let waypoints = path.waypoints(); | ||
|
||
if waypoints.is_empty() { | ||
return Err(PathError::Empty); | ||
} | ||
|
||
let size = waypoints.len() * size_of::<Vec2Net>(); | ||
if size > MAX_PATH_SIZE { | ||
return Err(PathError::TooLarge { | ||
size, | ||
max_size: MAX_PATH_SIZE, | ||
}); | ||
} | ||
|
||
Ok(Self(waypoints.iter().map(|&p| p.into()).collect())) | ||
} | ||
} | ||
|
||
impl From<&PathNet> for Path { | ||
fn from(path: &PathNet) -> Self { | ||
let mut waypoints: Vec<Vec2> = Vec::with_capacity(path.0.len()); | ||
let mut length = 0.; | ||
|
||
for &point in &path.0 { | ||
let point = point.into(); | ||
if let Some(prev) = waypoints.last() { | ||
length += prev.distance(point); | ||
} | ||
waypoints.push(point); | ||
} | ||
|
||
Path::new(length, waypoints) | ||
} | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum PathError { | ||
#[error("The path is empty")] | ||
Empty, | ||
#[error("Too many path way-points: {size} bytes > {max_size} bytes")] | ||
TooLarge { size: usize, max_size: usize }, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.