1#![deny(unsafe_code)]
2#![expect(incomplete_features)]
3#![expect(
4 clippy::identity_op,
5 clippy::option_map_unit_fn,
6 clippy::needless_pass_by_ref_mut )]
8#![deny(clippy::clone_on_ref_ptr)]
9#![feature(generic_const_exprs)]
10#![recursion_limit = "2048"]
11
12#[macro_use]
13pub mod ui;
14pub mod audio;
15pub mod cli;
16pub mod cmd;
17mod credits;
18#[cfg(feature = "discord")] pub mod discord;
19mod ecs;
20pub mod error;
21pub mod game_input;
22pub mod hud;
23pub mod key_state;
24pub mod menu;
25pub mod mesh;
26pub mod panic_handler;
27pub mod profile;
28pub mod render;
29pub mod run;
30pub mod scene;
31pub mod session;
32pub mod settings;
33#[cfg(feature = "singleplayer")]
34pub mod singleplayer;
35pub mod window;
36
37#[cfg(feature = "singleplayer")]
38use crate::singleplayer::Singleplayer;
39#[cfg(feature = "singleplayer")]
40use crate::singleplayer::SingleplayerState;
41#[cfg(feature = "egui-ui")]
42use crate::ui::egui::EguiState;
43use crate::{
44 audio::AudioFrontend,
45 profile::Profile,
46 render::{Drawer, GlobalsBindGroup},
47 settings::Settings,
48 window::{Event, Window},
49};
50use common::clock::Clock;
51use common_base::span;
52use i18n::LocalizationHandle;
53use std::path::PathBuf;
54
55use std::sync::Arc;
56use tokio::runtime::Runtime;
57
58pub struct GlobalState {
60 pub userdata_dir: PathBuf,
61 pub config_dir: PathBuf,
62 pub settings: Settings,
63 pub profile: Profile,
64 pub window: Window,
65 pub tokio_runtime: Arc<Runtime>,
66 #[cfg(feature = "egui-ui")]
67 pub egui_state: EguiState,
68 pub lazy_init: scene::terrain::SpriteRenderContextLazy,
69 pub audio: AudioFrontend,
70 pub info_message: Option<String>,
71 pub clock: Clock,
72 #[cfg(feature = "singleplayer")]
73 pub singleplayer: SingleplayerState,
74 pub i18n: LocalizationHandle,
76 pub clipboard: ui::ice::Clipboard,
77 pub clear_shadows_next_frame: bool,
80 pub args: crate::cli::Args,
82 #[cfg(feature = "discord")]
84 pub discord: crate::discord::Discord,
85}
86
87impl GlobalState {
88 pub fn on_play_state_changed(&mut self) {
91 self.window.grab_cursor(false);
92 self.window.needs_refresh_resize();
93 }
94
95 pub fn maintain(&mut self) {
96 span!(_guard, "maintain", "GlobalState::maintain");
97 self.audio.maintain();
98 self.window.renderer().maintain()
99 }
100
101 #[cfg(feature = "singleplayer")]
102 pub fn paused(&self) -> bool {
103 self.singleplayer
104 .as_running()
105 .is_some_and(Singleplayer::is_paused)
106 }
107
108 #[cfg(not(feature = "singleplayer"))]
109 pub fn paused(&self) -> bool { false }
110
111 #[cfg(feature = "singleplayer")]
112 pub fn unpause(&self) { self.singleplayer.as_running().map(|s| s.pause(false)); }
113
114 #[cfg(feature = "singleplayer")]
115 pub fn pause(&self) { self.singleplayer.as_running().map(|s| s.pause(true)); }
116}
117
118pub enum Direction {
120 Forwards,
121 Backwards,
122}
123
124pub enum PlayStateResult {
127 Continue,
129 Shutdown,
131 Pop,
133 Push(Box<dyn PlayState>),
135 Switch(Box<dyn PlayState>),
137}
138
139pub trait PlayState {
142 fn enter(&mut self, global_state: &mut GlobalState, direction: Direction);
144
145 fn tick(&mut self, global_state: &mut GlobalState, events: Vec<Event>) -> PlayStateResult;
147
148 fn name(&self) -> &'static str;
150
151 fn capped_fps(&self) -> bool;
153
154 fn globals_bind_group(&self) -> &GlobalsBindGroup;
155
156 fn render(&self, drawer: &mut Drawer<'_>, settings: &Settings);
158
159 fn egui_enabled(&self) -> bool;
161}