Skip to content

Commit

Permalink
feat: derive serde for components
Browse files Browse the repository at this point in the history
  • Loading branch information
manankarnik committed Jan 11, 2024
1 parent cce3f31 commit 466fd57
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ gltf = "1.3.0"
image = "0.24.7"
noise = { version = "0.8.2", git = "https://github.com/Razaekel/noise-rs.git" }
rfd = "0.12.1"
serde = "1.0.195"
serde_json = "1.0.111"
wasm-bindgen = "0.2.89"

# Enable max optimizations for dependencies, but not for our code:
Expand Down
3 changes: 2 additions & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use bevy::{
render::{render_resource::TextureFormat, texture::ImageSampler},
};
use image::Pixel;
use serde::{Deserialize, Serialize};

use crate::{
noise::{generate_noise_map, Noise},
Expand All @@ -39,7 +40,7 @@ impl Plugin for MapPlugin {
}

/// Component for map configuration
#[derive(Component)]
#[derive(Component, Serialize, Deserialize)]
pub struct Map {
/// Noise configuration of the map
pub noise: Noise,
Expand Down
10 changes: 8 additions & 2 deletions src/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use bevy::prelude::{Handle, Image};
use noise::{BasicMulti, Billow, Fbm, HybridMulti, RidgedMulti};
use noise::{MultiFractal, NoiseFn, Seedable};
use noise::{OpenSimplex, Perlin, PerlinSurflet, Simplex, SuperSimplex, Value, Worley};
use serde::{Deserialize, Serialize};

/// 2D noise method used to generate noise map
#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Serialize, Deserialize)]
pub enum Method {
/// Open Simplex noise
OpenSimplex,
Expand Down Expand Up @@ -39,7 +40,7 @@ impl fmt::Display for Method {
}

/// Fractal function that should be applied on the noise values
#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Serialize, Deserialize)]
pub enum FunctionName {
/// See [`BasicMulti`](https://docs.rs/noise/latest/noise/struct.BasicMulti.html)
BasicMulti,
Expand All @@ -66,6 +67,7 @@ impl fmt::Display for FunctionName {
}

/// Fractal function configuration
#[derive(Serialize, Deserialize)]
pub struct Function {
/// Name of the function
pub name: Option<FunctionName>,
Expand All @@ -92,6 +94,7 @@ impl Default for Function {
}

/// Region based on height
#[derive(Serialize, Deserialize)]
pub struct Region {
/// Label of the region
pub label: String,
Expand All @@ -112,8 +115,10 @@ impl Default for Region {
}

/// Gradient used to map color values
#[derive(Serialize, Deserialize)]
pub struct Gradient {
/// Image handle of gradient
#[serde(skip_serializing, skip_deserializing)]
pub image: Handle<Image>,
/// Size of gradient
pub size: [u32; 2],
Expand All @@ -135,6 +140,7 @@ impl Default for Gradient {
}

/// Noise configuration
#[derive(Serialize, Deserialize)]
pub struct Noise {
pub(crate) size: [u32; 2],
/// Seed of the noise
Expand Down
4 changes: 2 additions & 2 deletions src/planet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ use bevy::{
},
render::render_resource::{PrimitiveTopology, TextureFormat},
};

use image::Pixel;
use serde::{Deserialize, Serialize};

use crate::{
noise::{get_noise_at_point_3d, Function, Gradient, Method, Region},
util::export_model,
};

/// Component for planet configuration
#[derive(Component)]
#[derive(Component, Serialize, Deserialize)]
pub struct Planet {
/// Seed of the noise
pub seed: u32,
Expand Down
3 changes: 2 additions & 1 deletion src/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ use bevy::{
render::render_resource::{PrimitiveTopology, TextureFormat},
};
use image::Pixel;
use serde::{Deserialize, Serialize};

use crate::{noise::generate_noise_map, noise::Noise, util::export_model};

/// Component for terrain configuration
#[derive(Component)]
#[derive(Component, Serialize, Deserialize)]
pub struct Terrain {
/// Noise configuration for terrain
pub noise: Noise,
Expand Down

0 comments on commit 466fd57

Please sign in to comment.