nightshade_api/lib.rs
1//! # nightshade-api
2//!
3//! A procedural high level API over the nightshade engine. Write a full 3d
4//! scene or a small game as straight-line code with free functions and plain
5//! data. No trait to implement, no callbacks to wire up, no ECS knowledge
6//! required to get started.
7//!
8//! A spinning cube:
9//!
10//! ```ignore
11//! use nightshade_api::prelude::*;
12//!
13//! fn main() {
14//! let mut app = open();
15//! let cube = spawn_cube(&mut app.world, vec3(0.0, 0.5, 0.0));
16//! while frame(&mut app) {
17//! let step = delta_time(&app.world);
18//! rotate(&mut app.world, cube, Vec3::y(), step);
19//! }
20//! }
21//! ```
22//!
23//! Add to Cargo.toml:
24//!
25//! ```toml
26//! nightshade-api = "0.51"
27//! ```
28//!
29//! ## What you get for free
30//!
31//! [`prelude::open`] gives you a window with a sky, a sun with shadows, a
32//! reference grid, an orbit camera focused on the origin, prototype textures
33//! like `"checkerboard"`, and escape to exit. Every program starts from a lit,
34//! navigable scene. Override any of it with one call: [`prelude::set_background`],
35//! [`prelude::show_grid`], [`prelude::fly_camera`], [`prelude::set_sun`].
36//!
37//! ## The two entry points
38//!
39//! Own the loop (native only). Setup is ordinary code before the loop, state
40//! is ordinary locals across loop iterations:
41//!
42//! ```ignore
43//! let mut app = open();
44//! let mut score = 0;
45//! while frame(&mut app) {
46//! score += 1;
47//! }
48//! ```
49//!
50//! Or hand the engine the loop with [`run`](fn@crate::prelude::run), which also works on
51//! wasm. Setup returns your state, the update closure receives it back every
52//! frame:
53//!
54//! ```ignore
55//! run(
56//! |world| spawn_cube(world, vec3(0.0, 0.5, 0.0)),
57//! |world, cube| {
58//! let step = delta_time(world);
59//! rotate(world, *cube, Vec3::y(), step);
60//! },
61//! )
62//! .unwrap();
63//! ```
64//!
65//! `run` returns a `Result`, so a real `main` returns it. For several per-frame
66//! jobs, the [`run!`](crate::run!) macro takes any number of update systems:
67//!
68//! ```ignore
69//! fn main() -> Result<(), Box<dyn std::error::Error>> {
70//! run!(setup, handle_input, move_player, check_collisions)
71//! }
72//! ```
73//!
74//! For immediate-mode UI, enable the `egui` feature, call
75//! [`enable_egui`](fn@crate::prelude::enable_egui) once in setup, then draw from
76//! your update closure by pulling the frame's context with
77//! [`egui_context`](fn@crate::prelude::egui_context). No extra closure: it
78//! composes with [`run`](fn@crate::prelude::run) and [`run!`](crate::run!) as is.
79//! `egui` is re-exported from the prelude.
80//!
81//! ```ignore
82//! run(
83//! |world| { enable_egui(world); spawn_cube(world, vec3(0.0, 0.5, 0.0)) },
84//! |world, cube| {
85//! rotate(world, *cube, Vec3::y(), delta_time(world));
86//! if let Some(ctx) = egui_context(world) {
87//! egui::Window::new("Inspector").show(&ctx, |ui| ui.label(format!("{cube:?}")));
88//! }
89//! },
90//! )
91//! .unwrap();
92//! ```
93//!
94//! ## Vocabulary
95//!
96//! Two verbs carry the lifetime rules. `spawn_` is retained: the thing exists
97//! until you [`prelude::despawn`] it. `draw_` is immediate: visible for
98//! exactly one frame, redraw it every frame you want it on screen.
99//!
100//! - Scene content: [`prelude::spawn_cube`], [`prelude::spawn_sphere`],
101//! [`prelude::spawn_floor`], [`prelude::spawn_model`], [`prelude::spawn_object`]
102//! - Crowds: [`prelude::spawn_objects`], [`prelude::spawn_instanced`]
103//! - Tags: [`prelude::tag`], [`prelude::tagged`], [`prelude::for_each_tagged`]
104//! - Looks: [`prelude::set_color`], [`prelude::set_metallic_roughness`],
105//! [`prelude::set_emissive`], [`prelude::set_texture`],
106//! [`prelude::set_normal_texture`]
107//! - Placement: [`prelude::set_position`], [`prelude::rotate`], [`prelude::set_scale`],
108//! [`prelude::set_parent`]
109//! - Cameras: [`prelude::orbit_camera`], [`prelude::fly_camera`],
110//! [`prelude::first_person`], [`prelude::fixed_camera`],
111//! [`prelude::set_field_of_view`], [`prelude::set_orthographic`]
112//! - Input: [`prelude::key_down`], [`prelude::wasd`], [`prelude::mouse_clicked`],
113//! [`prelude::clicked_entity`]
114//! - Time: [`prelude::delta_time`], [`prelude::set_time_scale`], [`prelude::pause`],
115//! [`prelude::Timer`], [`prelude::Stopwatch`]
116//! - Immediate drawing: [`prelude::draw_cube`], [`prelude::draw_sphere`],
117//! [`prelude::draw_line`]
118//! - Text: [`prelude::spawn_text`], [`prelude::set_text`], [`prelude::spawn_label`]
119//! - UI: [`prelude::spawn_panel`], [`prelude::panel_button`],
120//! [`prelude::panel_label`], [`prelude::button_clicked`]; plus the full widget
121//! set: [`prelude::panel_data_grid`], [`prelude::panel_tree_view`],
122//! [`prelude::panel_property_grid`], [`prelude::panel_color_picker`],
123//! [`prelude::panel_date_picker`], [`prelude::panel_command_palette`],
124//! [`prelude::panel_modal`], [`prelude::panel_virtual_list`]
125//! - Animation: [`prelude::play_animation`], [`prelude::play_animation_named`],
126//! [`prelude::blend_to_animation`], [`prelude::set_animation_speed`]
127//! - Post-processing: [`prelude::set_ssao`], [`prelude::set_ssr`],
128//! [`prelude::set_tonemap`], [`prelude::set_color_grading`]
129//! - Live physics tuning: [`prelude::set_friction`], [`prelude::set_restitution`],
130//! [`prelude::set_mass`], [`prelude::set_gravity_scale`]
131//! - Layout containers: [`prelude::panel_row`], [`prelude::panel_grid`],
132//! [`prelude::panel_scroll`]; world-space UI: [`prelude::spawn_world_panel`]
133//! - Animation extras: [`prelude::add_animation_event`],
134//! [`prelude::add_animation_layer`], [`prelude::reach_to`], [`prelude::aim_at`]
135//! - Prefabs and undo: [`prelude::make_prefab`], [`prelude::spawn_prefab`],
136//! [`prelude::UndoStack`]
137//!
138//! ## Reading the scene back
139//!
140//! The setters have readers, which is what a tool that edits a scene rather than
141//! just building one needs. [`prelude::describe_entity`] gathers an entity's
142//! whole editable state, [`prelude::color`] and friends read one field,
143//! [`prelude::scene_tree`] and [`prelude::children`] walk the hierarchy,
144//! [`prelude::list_materials`] reads the shared material registry, and
145//! [`prelude::bounds_of`] with [`prelude::frame_entities`] measure and frame a
146//! selection. [`prelude::save_scene`] and [`prelude::load_scene`] round-trip the
147//! whole world to bytes. Components are added, removed, and snapshotted for undo
148//! by [`prelude::ComponentKind`], the surface the standalone editor is built on.
149//!
150//! ## Commands
151//!
152//! Every call also has a data form. [`prelude::Command`] is a serde enum with
153//! one variant per function, [`prelude::submit_command`] runs one, and
154//! [`prelude::submit_commands`] runs a batch where a later command can name an
155//! entity an earlier one produced with [`prelude::Ref::Result`], so one batch
156//! builds and wires up a scene. The enum is the wire format a binding targets:
157//! build `Command` values, read [`prelude::CommandReply`] back, with the json
158//! schema from [`prelude::command_schema`]. The free functions stay the real
159//! implementations and the dispatch forwards to them.
160//! See the `commands` example and `docs/COMMAND_API.md`.
161//!
162//! ## Dropping down to the engine
163//!
164//! Every function here takes the real engine [`prelude::World`] and bottoms
165//! out in normal nightshade calls. Nothing is hidden behind a wrapper type,
166//! so when a program outgrows the facade you replace one call site at a time.
167//! The full engine is re-exported at [`nightshade`], one path away:
168//!
169//! ```ignore
170//! use nightshade_api::nightshade::prelude::*;
171//! ```
172//!
173//! ## A member world for game components
174//!
175//! A game that outgrows plain entities declares its own components with the
176//! `freecs::dynamic_schema!` macro and registers them as a third member world
177//! of the engine group, sharing entity identity with the render entities. The
178//! engine [`World`](prelude::World) owns rendering and transforms while the
179//! member world holds movement, behavior, and rules on the same entities, so
180//! there is no link component and one recursive despawn tears down both. Push
181//! game state into the engine with ordinary facade calls like
182//! [`set_position`](prelude::set_position); `freecs` comes from the prelude,
183//! so `use nightshade_api::prelude::*` is enough to define and drive the
184//! member world. The `member_world` example builds the whole pattern end to
185//! end.
186//!
187//! ## Web apps
188//!
189//! For a browser app the engine runs in a web worker on an `OffscreenCanvas`
190//! and a Leptos page drives it from the main thread. Both halves of that seam
191//! are features of this crate: `offscreen` is the worker side
192//! (`offscreen::run_offscreen` owns the canvas, the world, the render loop,
193//! and the page conversation), and `leptos` is the page side (the `web`
194//! module, an engine-free Leptos component library with a worker-backed
195//! `EngineViewport`, usable with `default-features = false`). The shared wire
196//! types are in `wire`. The `template_leptos` and `template_api_leptos`
197//! directories are the working references.
198//!
199//! ## Examples
200//!
201//! The `examples/` directory is the tour. Run one with
202//! `just run-example solar_system` from the repo root, or
203//! `cargo run -r -p nightshade-api --example solar_system`. Every example also
204//! runs in the browser with `just run-example-wasm solar_system`, which serves
205//! it through trunk.
206
207#[cfg(feature = "engine")]
208pub use nightshade;
209
210#[cfg(feature = "engine")]
211mod animate;
212#[cfg(all(feature = "engine", not(target_arch = "wasm32")))]
213mod app;
214#[cfg(feature = "engine")]
215mod appearance;
216#[cfg(feature = "audio")]
217mod audio;
218#[cfg(feature = "engine")]
219mod bounds;
220#[cfg(feature = "engine")]
221mod camera;
222#[cfg(feature = "physics")]
223mod character;
224#[cfg(feature = "engine")]
225mod clock;
226#[cfg(feature = "engine")]
227mod cloth;
228#[cfg(feature = "engine")]
229mod command;
230#[cfg(feature = "engine")]
231mod cutscene;
232#[cfg(feature = "engine")]
233mod decals;
234#[cfg(feature = "engine")]
235mod draw;
236#[cfg(feature = "scripting")]
237mod dynamic_de;
238#[cfg(feature = "protocol")]
239pub mod editor;
240#[cfg(feature = "engine")]
241mod effects;
242#[cfg(feature = "engine")]
243mod environment;
244#[cfg(feature = "engine")]
245mod events;
246#[cfg(feature = "engine")]
247mod filesystem;
248#[cfg(feature = "engine")]
249mod groups;
250#[cfg(feature = "engine")]
251mod hierarchy;
252#[cfg(feature = "engine")]
253mod input;
254#[cfg(feature = "engine")]
255mod inspect;
256#[cfg(feature = "engine")]
257mod lighting;
258#[cfg(feature = "engine")]
259mod materials;
260#[cfg(feature = "engine")]
261mod mesh;
262#[cfg(feature = "engine")]
263mod messaging;
264#[cfg(feature = "engine")]
265mod morph;
266#[cfg(feature = "navmesh")]
267mod navigation;
268#[cfg(all(feature = "net", not(target_arch = "wasm32")))]
269mod net;
270#[cfg(all(target_arch = "wasm32", feature = "offscreen"))]
271pub mod offscreen;
272#[cfg(feature = "engine")]
273mod palette;
274#[cfg(feature = "physics")]
275mod physics;
276#[cfg(feature = "picking")]
277mod picking;
278#[cfg(feature = "engine")]
279mod placement;
280#[cfg(feature = "engine")]
281mod prefab;
282#[cfg(feature = "engine")]
283mod reflect;
284#[cfg(feature = "engine")]
285mod render;
286#[cfg(feature = "engine")]
287mod runner;
288#[cfg(feature = "engine")]
289mod scene;
290#[cfg(feature = "scripting")]
291mod scripting;
292#[cfg(feature = "engine")]
293mod serialize;
294#[cfg(any(feature = "terrain", feature = "grass"))]
295mod terrain;
296#[cfg(feature = "engine")]
297mod text;
298#[cfg(feature = "engine")]
299mod ui;
300#[cfg(feature = "engine")]
301mod undo;
302#[cfg(feature = "leptos")]
303pub mod web;
304#[cfg(feature = "engine")]
305mod window;
306#[cfg(any(feature = "leptos", feature = "offscreen"))]
307pub mod wire;
308#[cfg(feature = "picking")]
309mod world_ui;
310
311/// Everything in one import.
312///
313/// ```ignore
314/// use nightshade_api::prelude::*;
315/// ```
316///
317/// Pulls in the full api surface plus the engine types it hands you:
318/// [`World`](crate::prelude::World), [`Entity`](crate::prelude::Entity), the math types, [`KeyCode`](crate::prelude::KeyCode), and [`MouseButton`](crate::prelude::MouseButton).
319#[cfg(feature = "engine")]
320pub mod prelude {
321 pub use crate::animate::*;
322 #[cfg(not(target_arch = "wasm32"))]
323 pub use crate::app::{App, Window, frame, open, open_with, render_image};
324 pub use crate::appearance::*;
325 #[cfg(feature = "audio")]
326 pub use crate::audio::*;
327 pub use crate::bounds::*;
328 pub use crate::camera::*;
329 #[cfg(feature = "physics")]
330 pub use crate::character::*;
331 pub use crate::clock::*;
332 pub use crate::cloth::*;
333 pub use crate::command::*;
334 pub use crate::cutscene::*;
335 pub use crate::decals::*;
336 pub use crate::draw::*;
337 pub use crate::effects::*;
338 pub use crate::environment::*;
339 pub use crate::events::*;
340 pub use crate::filesystem::*;
341 pub use crate::groups::*;
342 pub use crate::hierarchy::*;
343 pub use crate::input::*;
344 pub use crate::inspect::*;
345 pub use crate::lighting::*;
346 pub use crate::materials::*;
347 pub use crate::mesh::*;
348 pub use crate::messaging::*;
349 pub use crate::morph::*;
350 #[cfg(feature = "navmesh")]
351 pub use crate::navigation::*;
352 #[cfg(all(feature = "net", not(target_arch = "wasm32")))]
353 pub use crate::net::*;
354 pub use crate::palette::*;
355 #[cfg(feature = "physics")]
356 pub use crate::physics::*;
357 #[cfg(feature = "picking")]
358 pub use crate::picking::*;
359 pub use crate::placement::*;
360 pub use crate::prefab::*;
361 pub use crate::reflect::*;
362 pub use crate::render::*;
363 pub use crate::run;
364 pub use crate::runner::{run, run_scene, systems};
365 pub use crate::scene::*;
366 #[cfg(feature = "scripting")]
367 pub use crate::scripting::*;
368 pub use crate::serialize::*;
369 #[cfg(any(feature = "terrain", feature = "grass"))]
370 pub use crate::terrain::*;
371 pub use crate::text::*;
372 pub use crate::ui::*;
373 pub use crate::undo::*;
374 pub use crate::window::*;
375 #[cfg(feature = "picking")]
376 pub use crate::world_ui::*;
377 pub use nightshade::ecs::world::{CORE, GAME, UI};
378 #[cfg(feature = "navmesh")]
379 pub use nightshade::prelude::RecastNavMeshConfig;
380 pub use nightshade::prelude::freecs;
381 #[cfg(feature = "egui")]
382 pub use nightshade::prelude::{egui, egui_context};
383 pub use nightshade::render::material::Material;
384 pub use nightshade::render::particles::ParticleEmitter;
385
386 #[cfg(feature = "physics")]
387 pub use nightshade::ecs::physics::joints::{
388 FixedJoint, JointAxisDirection, JointHandle, RevoluteJoint, RopeJoint, SpringJoint,
389 };
390 #[cfg(feature = "physics")]
391 pub use nightshade::prelude::{CollisionEvent, RaycastHit};
392 pub use nightshade::prelude::{
393 EasingFunction, Entity, Fog, InstanceTransform, KeyCode, Line, MouseButton, ShadingMode,
394 SplitDirection, TextAlignment, Vec2, Vec3, Vec4, VerticalAlignment, ViewportShading, World,
395 nalgebra_glm, vec2, vec3, vec4,
396 };
397 pub use nightshade::render::config::DepthOfField;
398}