tirbofish/dropbear
main / crates / eucalyptus-core / src / scene / scripting.rs · 4555 bytes
crates/eucalyptus-core/src/scene/scripting.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
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
pub mod shared {
use crate::command::CommandBuffer;
use crate::scene::loading::{SceneLoadResult, SceneLoader};
use crate::scripting::native::DropbearNativeError;
use crate::scripting::result::DropbearNativeResult;
use crossbeam_channel::Sender;
use parking_lot::Mutex;
pub fn load_scene_async(
command_buffer: &Sender<CommandBuffer>,
scene_loader: &Mutex<SceneLoader>,
scene_name: String,
_loading_scene: Option<String>,
) -> DropbearNativeResult<u64> {
let mut loader = scene_loader.lock();
if let Some(existing_id) = loader.find_pending_id_by_name(&scene_name) {
return Ok(existing_id);
}
let id = loader.register_load(scene_name.clone());
let handle = crate::scene::loading::SceneLoadHandle {
id,
scene_name: scene_name.clone(),
};
command_buffer
.try_send(CommandBuffer::LoadSceneAsync(handle))
.map_err(|_| DropbearNativeError::SendError)?;
Ok(id)
}
pub fn switch_to_scene_immediate(
command_buffer: &Sender<CommandBuffer>,
scene_name: String,
) -> DropbearNativeResult<()> {
command_buffer
.try_send(CommandBuffer::SwitchSceneImmediate(scene_name))
.map_err(|_| DropbearNativeError::SendError)?;
Ok(())
}
pub fn switch_to_scene_async(
command_buffer: &Sender<CommandBuffer>,
scene_loader: &Mutex<SceneLoader>,
scene_id: u64,
) -> DropbearNativeResult<()> {
let loader = scene_loader.lock();
if let Some(entry) = loader.get_entry(scene_id) {
if matches!(entry.result, SceneLoadResult::Success) {
let handle = crate::scene::loading::SceneLoadHandle {
id: scene_id,
scene_name: entry.scene_name.clone(),
};
command_buffer
.try_send(CommandBuffer::SwitchToAsync(handle))
.map_err(|_| DropbearNativeError::SendError)?;
Ok(())
} else {
Err(DropbearNativeError::PrematureSceneSwitch)
}
} else {
Err(DropbearNativeError::NoSuchHandle)
}
}
pub fn get_scene_load_handle_scene_name(
scene_loader: &Mutex<SceneLoader>,
scene_id: u64,
) -> DropbearNativeResult<String> {
let loader = scene_loader.lock();
if let Some(entry) = loader.get_entry(scene_id) {
Ok(entry.scene_name.clone())
} else {
Err(DropbearNativeError::NoSuchHandle)
}
}
pub fn get_scene_load_progress(
scene_loader: &Mutex<SceneLoader>,
scene_id: u64,
) -> DropbearNativeResult<crate::utils::Progress> {
let mut loader = scene_loader.lock();
if let Some(entry) = loader.get_entry_mut(scene_id) {
if let Some(ref rx) = entry.status {
while let Ok(status) = rx.try_recv() {
match status {
crate::states::WorldLoadingStatus::Idle => {
entry.progress.message = "Idle".to_string();
}
crate::states::WorldLoadingStatus::LoadingEntity { index, name, total } => {
entry.progress.current = index;
entry.progress.total = total;
entry.progress.message = format!("Loading entity: {}", name);
}
crate::states::WorldLoadingStatus::Completed => {
entry.progress.current = entry.progress.total;
entry.progress.message = "Completed".to_string();
}
}
}
}
Ok(entry.progress.clone())
} else {
Err(DropbearNativeError::NoSuchHandle)
}
}
pub fn get_scene_load_status(
scene_loader: &Mutex<SceneLoader>,
scene_id: u64,
) -> DropbearNativeResult<u32> {
let loader = scene_loader.lock();
if let Some(entry) = loader.get_entry(scene_id) {
let status = match entry.result {
SceneLoadResult::Pending => 0,
SceneLoadResult::Success => 1,
SceneLoadResult::Error(_) => 2,
};
Ok(status)
} else {
Err(DropbearNativeError::NoSuchHandle)
}
}
}