Skip to main content

gizmo_core/
state.rs

1use crate::world::World;
2
3/// Oyundaki mantıksal durumları yönetmek için kullanılan State yapısı.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct State<S: Clone + PartialEq + Eq + Send + Sync + 'static> {
6    current: S,
7    next: Option<S>,
8}
9
10impl<S: Clone + PartialEq + Eq + Send + Sync + 'static> State<S> {
11    pub fn new(initial: S) -> Self {
12        Self {
13            current: initial,
14            next: None,
15        }
16    }
17
18    pub fn get(&self) -> &S {
19        &self.current
20    }
21
22    pub fn set(&mut self, state: S) {
23        if self.current != state {
24            self.next = Some(state);
25        }
26    }
27
28    /// Bir sonraki durumu aktif duruma geçirir. (Genellikle PreUpdate fazında çalıştırılır).
29    pub fn apply_transitions(&mut self) -> bool {
30        if let Some(next) = self.next.take() {
31            self.current = next;
32            true
33        } else {
34            false
35        }
36    }
37}
38
39/// Sistemin sadece belirli bir state'teyken çalışmasını sağlayan "Run Condition" fonksiyonu.
40pub fn in_state<S>(state: S) -> impl FnMut(&World) -> bool + Send + Sync + 'static
41where
42    S: Clone + PartialEq + Eq + Send + Sync + 'static,
43{
44    move |world: &World| {
45        if let Some(current_state) = world.get_resource::<State<S>>() {
46            *current_state.get() == state
47        } else {
48            false
49        }
50    }
51}