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.49"
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//! ## Examples
190//!
191//! The `examples/` directory is the tour. Run one with
192//! `just run-example solar_system` from the repo root, or
193//! `cargo run -r -p nightshade-api --example solar_system`. Every example also
194//! runs in the browser with `just run-example-wasm solar_system`, which serves
195//! it through trunk.
196
197pub use nightshade;
198
199mod animate;
200#[cfg(not(target_arch = "wasm32"))]
201mod app;
202mod appearance;
203#[cfg(feature = "audio")]
204mod audio;
205mod bounds;
206mod camera;
207#[cfg(feature = "physics")]
208mod character;
209mod clock;
210mod cloth;
211mod command;
212mod cutscene;
213mod decals;
214mod draw;
215#[cfg(feature = "scripting")]
216mod dynamic_de;
217#[cfg(feature = "protocol")]
218pub mod editor;
219mod effects;
220mod environment;
221mod events;
222mod filesystem;
223mod groups;
224mod hierarchy;
225mod input;
226mod inspect;
227mod lighting;
228mod materials;
229mod mesh;
230mod messaging;
231mod morph;
232#[cfg(feature = "navmesh")]
233mod navigation;
234#[cfg(all(feature = "net", not(target_arch = "wasm32")))]
235mod net;
236mod palette;
237#[cfg(feature = "physics")]
238mod physics;
239#[cfg(feature = "picking")]
240mod picking;
241mod placement;
242mod prefab;
243mod reflect;
244mod render;
245mod runner;
246mod scene;
247#[cfg(feature = "scripting")]
248mod scripting;
249mod serialize;
250#[cfg(any(feature = "terrain", feature = "grass"))]
251mod terrain;
252mod text;
253mod ui;
254mod undo;
255mod window;
256#[cfg(feature = "picking")]
257mod world_ui;
258
259/// Everything in one import.
260///
261/// ```ignore
262/// use nightshade_api::prelude::*;
263/// ```
264///
265/// Pulls in the full api surface plus the engine types it hands you:
266/// [`World`](crate::prelude::World), [`Entity`](crate::prelude::Entity), the math types, [`KeyCode`](crate::prelude::KeyCode), and [`MouseButton`](crate::prelude::MouseButton).
267pub mod prelude {
268    pub use crate::animate::*;
269    #[cfg(not(target_arch = "wasm32"))]
270    pub use crate::app::{App, Window, frame, open, open_with, render_image};
271    pub use crate::appearance::*;
272    #[cfg(feature = "audio")]
273    pub use crate::audio::*;
274    pub use crate::bounds::*;
275    pub use crate::camera::*;
276    #[cfg(feature = "physics")]
277    pub use crate::character::*;
278    pub use crate::clock::*;
279    pub use crate::cloth::*;
280    pub use crate::command::*;
281    pub use crate::cutscene::*;
282    pub use crate::decals::*;
283    pub use crate::draw::*;
284    pub use crate::effects::*;
285    pub use crate::environment::*;
286    pub use crate::events::*;
287    pub use crate::filesystem::*;
288    pub use crate::groups::*;
289    pub use crate::hierarchy::*;
290    pub use crate::input::*;
291    pub use crate::inspect::*;
292    pub use crate::lighting::*;
293    pub use crate::materials::*;
294    pub use crate::mesh::*;
295    pub use crate::messaging::*;
296    pub use crate::morph::*;
297    #[cfg(feature = "navmesh")]
298    pub use crate::navigation::*;
299    #[cfg(all(feature = "net", not(target_arch = "wasm32")))]
300    pub use crate::net::*;
301    pub use crate::palette::*;
302    #[cfg(feature = "physics")]
303    pub use crate::physics::*;
304    #[cfg(feature = "picking")]
305    pub use crate::picking::*;
306    pub use crate::placement::*;
307    pub use crate::prefab::*;
308    pub use crate::reflect::*;
309    pub use crate::render::*;
310    pub use crate::run;
311    pub use crate::runner::{run, run_scene, systems};
312    pub use crate::scene::*;
313    #[cfg(feature = "scripting")]
314    pub use crate::scripting::*;
315    pub use crate::serialize::*;
316    #[cfg(any(feature = "terrain", feature = "grass"))]
317    pub use crate::terrain::*;
318    pub use crate::text::*;
319    pub use crate::ui::*;
320    pub use crate::undo::*;
321    pub use crate::window::*;
322    #[cfg(feature = "picking")]
323    pub use crate::world_ui::*;
324    pub use nightshade::ecs::material::components::Material;
325    pub use nightshade::ecs::particles::components::ParticleEmitter;
326    pub use nightshade::ecs::sync::EngineEntity;
327    #[cfg(feature = "navmesh")]
328    pub use nightshade::prelude::RecastNavMeshConfig;
329    pub use nightshade::prelude::freecs;
330    #[cfg(feature = "egui")]
331    pub use nightshade::prelude::{egui, egui_context};
332
333    pub use nightshade::ecs::graphics::resources::DepthOfField;
334    #[cfg(feature = "physics")]
335    pub use nightshade::ecs::physics::joints::{
336        FixedJoint, JointAxisDirection, JointHandle, RevoluteJoint, RopeJoint, SpringJoint,
337    };
338    #[cfg(feature = "physics")]
339    pub use nightshade::prelude::{CollisionEvent, RaycastHit};
340    pub use nightshade::prelude::{
341        EasingFunction, Entity, Fog, InstanceTransform, KeyCode, Line, MouseButton, ShadingMode,
342        SplitDirection, TextAlignment, Vec2, Vec3, Vec4, VerticalAlignment, ViewportShading, World,
343        nalgebra_glm, vec2, vec3, vec4,
344    };
345}