Skip to main content

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.50"
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//! ## Two worlds
174//!
175//! A game that outgrows plain entities can keep its own ECS world beside the
176//! engine's. Declare a second world with the `freecs::ecs!` macro and let the
177//! engine [`World`](prelude::World) own rendering and transforms while your
178//! world holds movement, behavior, and rules as plain components and systems.
179//! Link the two by storing the engine [`Entity`](prelude::Entity) in an
180//! [`EngineEntity`](prelude::EngineEntity) component on each game entity, then
181//! once a frame push your game state into the engine with ordinary facade calls
182//! like [`set_position`](prelude::set_position). Both `freecs` and
183//! [`EngineEntity`](prelude::EngineEntity) come from the prelude, so
184//! `use nightshade_api::prelude::*` is enough to define and drive the second
185//! world. The engine side stays plain data and remains the same command surface
186//! every other call uses, while the game side is ordinary Rust. The `dual_world`
187//! example builds the whole pattern end to end.
188//!
189//! ## Web apps
190//!
191//! For a browser app the engine runs in a web worker on an `OffscreenCanvas`
192//! and a Leptos page drives it from the main thread. Both halves of that seam
193//! are features of this crate: `offscreen` is the worker side
194//! (`offscreen::run_offscreen` owns the canvas, the world, the render loop,
195//! and the page conversation), and `leptos` is the page side (the `web`
196//! module, an engine-free Leptos component library with a worker-backed
197//! `EngineViewport`, usable with `default-features = false`). The shared wire
198//! types are in `wire`. The `template_leptos` and `template_api_leptos`
199//! directories are the working references.
200//!
201//! ## Examples
202//!
203//! The `examples/` directory is the tour. Run one with
204//! `just run-example solar_system` from the repo root, or
205//! `cargo run -r -p nightshade-api --example solar_system`. Every example also
206//! runs in the browser with `just run-example-wasm solar_system`, which serves
207//! it through trunk.
208
209#[cfg(feature = "engine")]
210pub use nightshade;
211
212#[cfg(feature = "engine")]
213mod animate;
214#[cfg(all(feature = "engine", not(target_arch = "wasm32")))]
215mod app;
216#[cfg(feature = "engine")]
217mod appearance;
218#[cfg(feature = "audio")]
219mod audio;
220#[cfg(feature = "engine")]
221mod bounds;
222#[cfg(feature = "engine")]
223mod camera;
224#[cfg(feature = "physics")]
225mod character;
226#[cfg(feature = "engine")]
227mod clock;
228#[cfg(feature = "engine")]
229mod cloth;
230#[cfg(feature = "engine")]
231mod command;
232#[cfg(feature = "engine")]
233mod cutscene;
234#[cfg(feature = "engine")]
235mod decals;
236#[cfg(feature = "engine")]
237mod draw;
238#[cfg(feature = "scripting")]
239mod dynamic_de;
240#[cfg(feature = "protocol")]
241pub mod editor;
242#[cfg(feature = "engine")]
243mod effects;
244#[cfg(feature = "engine")]
245mod environment;
246#[cfg(feature = "engine")]
247mod events;
248#[cfg(feature = "engine")]
249mod filesystem;
250#[cfg(feature = "engine")]
251mod groups;
252#[cfg(feature = "engine")]
253mod hierarchy;
254#[cfg(feature = "engine")]
255mod input;
256#[cfg(feature = "engine")]
257mod inspect;
258#[cfg(feature = "engine")]
259mod lighting;
260#[cfg(feature = "engine")]
261mod materials;
262#[cfg(feature = "engine")]
263mod mesh;
264#[cfg(feature = "engine")]
265mod messaging;
266#[cfg(feature = "engine")]
267mod morph;
268#[cfg(feature = "navmesh")]
269mod navigation;
270#[cfg(all(feature = "net", not(target_arch = "wasm32")))]
271mod net;
272#[cfg(all(target_arch = "wasm32", feature = "offscreen"))]
273pub mod offscreen;
274#[cfg(feature = "engine")]
275mod palette;
276#[cfg(feature = "physics")]
277mod physics;
278#[cfg(feature = "picking")]
279mod picking;
280#[cfg(feature = "engine")]
281mod placement;
282#[cfg(feature = "engine")]
283mod prefab;
284#[cfg(feature = "engine")]
285mod reflect;
286#[cfg(feature = "engine")]
287mod render;
288#[cfg(feature = "engine")]
289mod runner;
290#[cfg(feature = "engine")]
291mod scene;
292#[cfg(feature = "scripting")]
293mod scripting;
294#[cfg(feature = "engine")]
295mod serialize;
296#[cfg(any(feature = "terrain", feature = "grass"))]
297mod terrain;
298#[cfg(feature = "engine")]
299mod text;
300#[cfg(feature = "engine")]
301mod ui;
302#[cfg(feature = "engine")]
303mod undo;
304#[cfg(feature = "leptos")]
305pub mod web;
306#[cfg(feature = "engine")]
307mod window;
308#[cfg(any(feature = "leptos", feature = "offscreen"))]
309pub mod wire;
310#[cfg(feature = "picking")]
311mod world_ui;
312
313/// Everything in one import.
314///
315/// ```ignore
316/// use nightshade_api::prelude::*;
317/// ```
318///
319/// Pulls in the full api surface plus the engine types it hands you:
320/// [`World`](crate::prelude::World), [`Entity`](crate::prelude::Entity), the math types, [`KeyCode`](crate::prelude::KeyCode), and [`MouseButton`](crate::prelude::MouseButton).
321#[cfg(feature = "engine")]
322pub mod prelude {
323    pub use crate::animate::*;
324    #[cfg(not(target_arch = "wasm32"))]
325    pub use crate::app::{App, Window, frame, open, open_with, render_image};
326    pub use crate::appearance::*;
327    #[cfg(feature = "audio")]
328    pub use crate::audio::*;
329    pub use crate::bounds::*;
330    pub use crate::camera::*;
331    #[cfg(feature = "physics")]
332    pub use crate::character::*;
333    pub use crate::clock::*;
334    pub use crate::cloth::*;
335    pub use crate::command::*;
336    pub use crate::cutscene::*;
337    pub use crate::decals::*;
338    pub use crate::draw::*;
339    pub use crate::effects::*;
340    pub use crate::environment::*;
341    pub use crate::events::*;
342    pub use crate::filesystem::*;
343    pub use crate::groups::*;
344    pub use crate::hierarchy::*;
345    pub use crate::input::*;
346    pub use crate::inspect::*;
347    pub use crate::lighting::*;
348    pub use crate::materials::*;
349    pub use crate::mesh::*;
350    pub use crate::messaging::*;
351    pub use crate::morph::*;
352    #[cfg(feature = "navmesh")]
353    pub use crate::navigation::*;
354    #[cfg(all(feature = "net", not(target_arch = "wasm32")))]
355    pub use crate::net::*;
356    pub use crate::palette::*;
357    #[cfg(feature = "physics")]
358    pub use crate::physics::*;
359    #[cfg(feature = "picking")]
360    pub use crate::picking::*;
361    pub use crate::placement::*;
362    pub use crate::prefab::*;
363    pub use crate::reflect::*;
364    pub use crate::render::*;
365    pub use crate::run;
366    pub use crate::runner::{run, run_scene, systems};
367    pub use crate::scene::*;
368    #[cfg(feature = "scripting")]
369    pub use crate::scripting::*;
370    pub use crate::serialize::*;
371    #[cfg(any(feature = "terrain", feature = "grass"))]
372    pub use crate::terrain::*;
373    pub use crate::text::*;
374    pub use crate::ui::*;
375    pub use crate::undo::*;
376    pub use crate::window::*;
377    #[cfg(feature = "picking")]
378    pub use crate::world_ui::*;
379    pub use nightshade::ecs::material::components::Material;
380    pub use nightshade::ecs::particles::components::ParticleEmitter;
381    pub use nightshade::ecs::sync::EngineEntity;
382    #[cfg(feature = "navmesh")]
383    pub use nightshade::prelude::RecastNavMeshConfig;
384    pub use nightshade::prelude::freecs;
385    #[cfg(feature = "egui")]
386    pub use nightshade::prelude::{egui, egui_context};
387
388    pub use nightshade::ecs::graphics::resources::DepthOfField;
389    #[cfg(feature = "physics")]
390    pub use nightshade::ecs::physics::joints::{
391        FixedJoint, JointAxisDirection, JointHandle, RevoluteJoint, RopeJoint, SpringJoint,
392    };
393    #[cfg(feature = "physics")]
394    pub use nightshade::prelude::{CollisionEvent, RaycastHit};
395    pub use nightshade::prelude::{
396        EasingFunction, Entity, Fog, InstanceTransform, KeyCode, Line, MouseButton, ShadingMode,
397        SplitDirection, TextAlignment, Vec2, Vec3, Vec4, VerticalAlignment, ViewportShading, World,
398        nalgebra_glm, vec2, vec3, vec4,
399    };
400}