veloren_world/util/
unit_chooser.rs1use super::{RandomPerm, Sampler};
2use vek::*;
3
4const UNIT_CHOICES: [(Vec2<i32>, Vec2<i32>); 8] = [
5 (Vec2 { x: 1, y: 0 }, Vec2 { x: 0, y: 1 }),
6 (Vec2 { x: 1, y: 0 }, Vec2 { x: 0, y: -1 }),
7 (Vec2 { x: -1, y: 0 }, Vec2 { x: 0, y: 1 }),
8 (Vec2 { x: -1, y: 0 }, Vec2 { x: 0, y: -1 }),
9 (Vec2 { x: 0, y: 1 }, Vec2 { x: 1, y: 0 }),
10 (Vec2 { x: 0, y: 1 }, Vec2 { x: -1, y: 0 }),
11 (Vec2 { x: 0, y: -1 }, Vec2 { x: 1, y: 0 }),
12 (Vec2 { x: 0, y: -1 }, Vec2 { x: -1, y: 0 }),
13];
14
15pub struct UnitChooser {
16 perm: RandomPerm,
17}
18
19impl UnitChooser {
20 pub const fn new(seed: u32) -> Self {
21 Self {
22 perm: RandomPerm::new(seed),
23 }
24 }
25}
26
27impl Sampler<'static> for UnitChooser {
28 type Index = u32;
29 type Sample = (Vec2<i32>, Vec2<i32>);
30
31 fn get(&self, perm: Self::Index) -> Self::Sample {
32 UNIT_CHOICES[self.perm.get(perm) as usize % UNIT_CHOICES.len()]
33 }
34}