-
Notifications
You must be signed in to change notification settings - Fork 4
/
poisson.rs
86 lines (77 loc) · 2.01 KB
/
poisson.rs
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
use bevy::prelude::*;
use fast_poisson::Poisson2D;
use rand::{seq::SliceRandom, thread_rng};
use crate::{GeneratedItem, Scroller, ScrollerGenerator, ScrollerItem};
#[derive(Component, Default, Reflect, Clone)]
#[reflect(Component)]
pub struct PoissonSpriteGenerator {
pub radius: f32,
pub sprites: Vec<String>,
pub item_size: Vec2,
pub sub_item_size: Vec2,
}
#[derive(Debug)]
struct ItemSprite {
path: String,
position: Vec2,
}
#[derive(Debug)]
pub struct PoissonScrollerItem {
sprites: Vec<ItemSprite>,
size: Vec2,
}
impl GeneratedItem for PoissonScrollerItem {
fn size(&self) -> Vec2 {
self.size
}
}
impl ScrollerGenerator for PoissonSpriteGenerator {
type I = PoissonScrollerItem;
fn gen_item(&mut self) -> Self::I {
let mut rng = thread_rng();
Self::I {
size: self.item_size,
sprites: Poisson2D::new()
.with_dimensions(
[
self.item_size.x - self.sub_item_size.x,
self.item_size.y - self.sub_item_size.y,
],
self.radius,
)
.iter()
.map(|point| ItemSprite {
path: self.sprites.choose(&mut rng).unwrap().clone(),
position: Vec2::from(point) - (self.item_size - self.sub_item_size) / 2.,
})
.collect(),
}
}
}
pub fn poisson_generator(
In(input): In<Vec<(Entity, Scroller, Box<PoissonScrollerItem>)>>,
mut commands: Commands,
asset_server: Res<AssetServer>,
) {
for (entity, _, item) in input.iter() {
commands
.spawn((
ScrollerItem {
size: item.size,
parent: *entity,
},
Name::new("scroller item"),
SpatialBundle::default(),
))
.with_children(|parent| {
for subitem in item.sprites.iter() {
let image_handle = asset_server.load(subitem.path.clone());
parent.spawn(SpriteBundle {
texture: image_handle,
transform: Transform::from_translation(subitem.position.extend(0.)),
..default()
});
}
});
}
}