Skip to content

Commit

Permalink
test: add tests and trait impls in earthlike.rs
Browse files Browse the repository at this point in the history
* add a test for the `Material2d`'s `fragement_shader()` fn impl for `Earthlike`
* add `randomise()`, `randomise_seed()`, `randomise_rotation()`, and `set_*()` functions as impls for `PlanetShader` trait
  • Loading branch information
Sycrosity committed Sep 1, 2023
1 parent b9d8926 commit cc61e8d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn setup(
pixels: 100.,
rotation: 90f32.to_radians(),
radius: 100.,
time_speed: 0.2,
time_speed: 10.,
},
land_colours: [
Color::rgb(0.388235, 0.670588, 0.247059),
Expand Down
21 changes: 20 additions & 1 deletion src/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,26 @@ impl Default for CelestialSettings {
}
}

pub trait PlanetShader: ShaderType + Component + AsBindGroup + Material2d {}
pub trait PlanetShader: ShaderType + Component + AsBindGroup + Material2d {
fn randomise(&mut self) {
self.randomise_seed();
self.randomise_rotation();
}
fn randomise_seed(&mut self) {
self.set_seed(rand::thread_rng().gen());
}
fn randomise_rotation(&mut self) {
self.set_rotation(rand::thread_rng().gen_range(0f32..TAU));
}

fn set_seed(&mut self, seed: f32) {}

fn set_rotation(&mut self, rotation: f32) {}

fn set_pixels(&mut self, pixels: f32) {}

fn set_time_speed(&mut self, time_speed: f32) {}
}

#[derive(Bundle, Reflect, Default, Clone)]
pub struct CelestialBundle<P: PlanetShader> {
Expand Down
2 changes: 1 addition & 1 deletion src/shaders/cloud_cover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Default for CloudCover {

impl Material2d for CloudCover {
fn fragment_shader() -> ShaderRef {
"shaderS/celestials/generic/cloud_cover.wgsl".into()
"shaders/celestials/generic/cloud_cover.wgsl".into()
}
}

Expand Down
31 changes: 21 additions & 10 deletions src/shaders/earthlike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@ pub struct Earthlike {
pub river_colours: [Color; 2],
}

impl Earthlike {
pub(crate) fn randomise(&mut self) {
self.randomise_rotation();
self.randomise_seed();
}

pub(crate) fn randomise_seed(&mut self) {
impl PlanetShader for Earthlike {
fn randomise_seed(&mut self) {
self.celestial.seed = rand::thread_rng().gen();
}

pub(crate) fn randomise_rotation(&mut self) {
fn randomise_rotation(&mut self) {
self.celestial.rotation = rand::thread_rng().gen_range(0f32..TAU);
}
}
Expand Down Expand Up @@ -58,13 +53,29 @@ impl Material2d for Earthlike {
}
}

impl PlanetShader for Earthlike {}

#[cfg(test)]
mod test {

use bevy::{asset::AssetPath, render::render_resource::ShaderRef, sprite::Material2d};

use crate::prelude::*;

#[test]
fn test_earthlike_material2d_impl() {
let shader_ref = Earthlike::fragment_shader();

let ShaderRef::Path(asset_path) = shader_ref else {

panic!("\"ShaderRef\" from \"Earthlike::fragment_shader()\" isn't of enum variant \"ShaderRef::Path\"");

};

assert_eq!(
asset_path,
AssetPath::from("shaders/celestials/earthlike.wgsl")
);
}

#[test]
fn test_earthlike_randomise() {
let first = Earthlike::default();
Expand Down

0 comments on commit cc61e8d

Please sign in to comment.