aboutsummaryrefslogtreecommitdiff
path: root/examples/render-quad/src/system.rs
blob: 33427740ec4f0f5685e712043e9c4d15ab8d1fe5 (plain)
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
//! An example system that just alternates the colours of our triangle

use stockton_skeleton::types::Vector3;

/// RGB Channels
#[derive(Debug, Clone, Copy)]
enum ColorChannel {
    Red,
    Green,
    Blue,
}

/// A component for our entity.
#[derive(Debug, Clone, Copy)]
pub struct ExampleState {
    channel: ColorChannel,
    falling: bool,
    col_val: Vector3,
}

impl ExampleState {
    pub fn color(&self) -> Vector3 {
        self.col_val
    }
}

impl Default for ExampleState {
    fn default() -> Self {
        Self {
            channel: ColorChannel::Red,
            falling: true,
            col_val: Vector3::new(1.0, 1.0, 1.0),
        }
    }
}

/// The speed at which we change colour
const TRANSITION_SPEED: f32 = 0.1;

/// Keep changing the colour of any ExampleStates in our world.
#[system(for_each)]
pub fn mutate_state(state: &mut ExampleState) {
    // Which value we're changing
    let val = match state.channel {
        ColorChannel::Red => &mut state.col_val.x,
        ColorChannel::Green => &mut state.col_val.y,
        ColorChannel::Blue => &mut state.col_val.z,
    };

    if state.falling {
        *val -= TRANSITION_SPEED;

        // Fall, then rise
        if *val <= 0.0 {
            *val = 0.0;
            state.falling = false;
        }
    } else {
        *val += TRANSITION_SPEED;

        if *val >= 1.0 {
            *val = 1.0;

            // Rather than going back to falling, go to the next channel
            state.falling = true;
            state.channel = match state.channel {
                ColorChannel::Red => ColorChannel::Green,
                ColorChannel::Green => ColorChannel::Blue,
                ColorChannel::Blue => ColorChannel::Red,
            }
        }
    }
}