1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use bevy::{
asset::{AssetLoader, Handle, LoadContext, LoadedAsset},
math::{Vec2, Vec3, Vec4},
reflect::TypeUuid,
render::texture::Image,
utils::BoxedFuture,
};
use serde::{Deserialize, Serialize};
use crate::{
modifiers::{ForceFieldParam, FFNUM},
Gradient, InitModifier, RenderModifier, Spawner, UpdateModifier,
};
#[derive(Default, Clone)]
pub struct InitLayout {
pub position_code: String,
pub force_field_code: String,
}
#[derive(Default, Clone, Copy)]
pub struct UpdateLayout {
pub accel: Vec3,
pub force_field: [ForceFieldParam; FFNUM],
}
#[derive(Default, Clone)]
pub struct RenderLayout {
pub particle_texture: Option<Handle<Image>>,
pub lifetime_color_gradient: Option<Gradient<Vec4>>,
pub size_color_gradient: Option<Gradient<Vec2>>,
}
#[derive(Default, Serialize, Deserialize, TypeUuid)]
#[uuid = "249aefa4-9b8e-48d3-b167-3adf6c081c34"]
pub struct EffectAsset {
pub name: String,
pub capacity: u32,
pub spawner: Spawner,
#[serde(skip)]
pub init_layout: InitLayout,
#[serde(skip)]
pub update_layout: UpdateLayout,
#[serde(skip)]
pub render_layout: RenderLayout,
}
impl EffectAsset {
pub fn init<M: InitModifier + Send + Sync + 'static>(mut self, modifier: M) -> Self {
modifier.apply(&mut self.init_layout);
self
}
pub fn update<M: UpdateModifier + Send + Sync + 'static>(mut self, modifier: M) -> Self {
modifier.apply(&mut self.update_layout);
self
}
pub fn render<M: RenderModifier + Send + Sync + 'static>(mut self, modifier: M) -> Self {
modifier.apply(&mut self.render_layout);
self
}
}
#[derive(Default)]
pub struct EffectAssetLoader;
impl AssetLoader for EffectAssetLoader {
fn load<'a>(
&'a self,
bytes: &'a [u8],
load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<(), anyhow::Error>> {
Box::pin(async move {
let custom_asset = ron::de::from_bytes::<EffectAsset>(bytes)?;
load_context.set_default_asset(LoadedAsset::new(custom_asset));
Ok(())
})
}
fn extensions(&self) -> &[&str] {
&["effect"]
}
}