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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::interface::WebScriptInterface;
use core::{
    ecs::World,
    state::{State, StateChange},
};
use js_sys::{Function, JsString, Reflect};
use wasm_bindgen::{JsCast, JsValue};

pub trait WebScriptStateScripted {
    fn on_enter(&mut self, _world: &mut World) {}
    fn on_exit(&mut self, _world: &mut World) {}
    fn on_process(&mut self, _world: &mut World) -> Option<String> {
        None
    }
}

pub struct WebScriptBootState {
    initial_state_name: String,
}

impl WebScriptBootState {
    pub fn new(initial_state_name: &str) -> Self {
        Self {
            initial_state_name: initial_state_name.to_owned(),
        }
    }
}

impl State for WebScriptBootState {
    fn on_process(&mut self, _: &mut World) -> StateChange {
        if WebScriptInterface::is_ready() {
            return if let Some(result) = WebScriptInterface::build_state(&self.initial_state_name) {
                StateChange::Swap(Box::new(WebScriptState::new(result)))
            } else if let Some(result) =
                WebScriptInterface::build_state_scripted(&self.initial_state_name)
            {
                StateChange::Swap(Box::new(WebScriptStateWrapped::new(result)))
            } else {
                StateChange::Pop
            };
        }
        StateChange::None
    }
}

pub(crate) struct WebScriptStateWrapped {
    inner: Box<dyn WebScriptStateScripted>,
}

impl WebScriptStateWrapped {
    pub fn new(inner: Box<dyn WebScriptStateScripted>) -> Self {
        Self { inner }
    }
}

impl State for WebScriptStateWrapped {
    fn on_enter(&mut self, world: &mut World) {
        WebScriptInterface::set_world(world);
        self.inner.on_enter(world);
    }

    fn on_process(&mut self, world: &mut World) -> StateChange {
        WebScriptInterface::set_world(world);
        WebScriptInterface::run_systems();
        WebScriptInterface::maintain_entities(world);

        if let Some(name) = self.inner.on_process(world) {
            if let Some(result) = WebScriptInterface::build_state(&name) {
                return StateChange::Swap(Box::new(WebScriptState::new(result)));
            } else if let Some(result) = WebScriptInterface::build_state_scripted(&name) {
                return StateChange::Swap(Box::new(WebScriptStateWrapped::new(result)));
            }
        }
        StateChange::None
    }

    fn on_exit(&mut self, world: &mut World) {
        self.inner.on_exit(world);
        WebScriptInterface::unset_world();
    }
}

pub(crate) struct WebScriptState {
    context: JsValue,
    on_enter: Option<Function>,
    on_process: Option<Function>,
    on_exit: Option<Function>,
}

impl WebScriptState {
    pub fn new(context: JsValue) -> Self {
        let on_enter = if let Ok(m) = Reflect::get(&context, &JsValue::from_str("onEnter")) {
            m.dyn_ref::<Function>().cloned()
        } else {
            None
        };
        let on_process = if let Ok(m) = Reflect::get(&context, &JsValue::from_str("onProcess")) {
            m.dyn_ref::<Function>().cloned()
        } else {
            None
        };
        let on_exit = if let Ok(m) = Reflect::get(&context, &JsValue::from_str("onExit")) {
            m.dyn_ref::<Function>().cloned()
        } else {
            None
        };
        Self {
            context,
            on_enter,
            on_process,
            on_exit,
        }
    }
}

impl State for WebScriptState {
    fn on_enter(&mut self, world: &mut World) {
        WebScriptInterface::set_world(world);

        if let Some(on_enter) = &self.on_enter {
            drop(on_enter.call0(&self.context));
        }
    }

    fn on_process(&mut self, world: &mut World) -> StateChange {
        WebScriptInterface::set_world(world);
        WebScriptInterface::run_systems();
        WebScriptInterface::maintain_entities(world);

        if let Some(on_process) = &self.on_process {
            if let Ok(result) = on_process.call0(&self.context) {
                if let Some(result) = result.dyn_ref::<JsString>() {
                    let name = String::from(result);
                    if let Some(result) = WebScriptInterface::build_state(&name) {
                        return StateChange::Swap(Box::new(WebScriptState::new(result)));
                    } else if let Some(result) = WebScriptInterface::build_state_scripted(&name) {
                        return StateChange::Swap(Box::new(WebScriptStateWrapped::new(result)));
                    }
                }
            }
        }
        StateChange::None
    }

    fn on_exit(&mut self, _: &mut World) {
        if let Some(on_exit) = &self.on_exit {
            drop(on_exit.call0(&self.context));
        }

        WebScriptInterface::unset_world();
    }
}