rusty_engine/
lib.rs

1//! Rusty Engine is a simple, 2D game engine for those who are learning Rust. Create simple game
2//! prototypes using straightforward Rust code without any advanced game engine concepts! It works
3//! on macOS, Linux, and Windows. Rusty Engine is a simplification wrapper over
4//! [Bevy](https://bevyengine.org/), which I encourage you to use directly for more serious game
5//! engine needs.
6//!
7//! # Quick Start Example
8//!
9//! You start by importing `rusty_engine::prelude::*`, define your own `GameState` struct,
10//! create a [`Game`](crate::game::Game), set up your game, and then call
11//! [`Game::run`](crate::game::Game::run).
12//!
13//! ```no_run
14//! use rusty_engine::prelude::*;
15//!
16//! // Define a struct to hold custom data for your game (it can be a lot more complicated than this one!)
17//! #[derive(Resource)]
18//! struct GameState {
19//!     health: i32,
20//! }
21//!
22//! fn main() {
23//!     // Create a game
24//!     let mut game = Game::new();
25//!     // Set up your game. `Game` exposes all of the methods and fields of `Engine`.
26//!     let sprite = game.add_sprite("player", SpritePreset::RacingCarBlue);
27//!     sprite.scale = 2.0;
28//!     game.audio_manager.play_music(MusicPreset::Classy8Bit, 0.1);
29//!     // Add one or more functions with logic for your game. When the game is run, the logic
30//!     // functions will run in the order they were added.
31//!     game.add_logic(game_logic);
32//!     // Run the game, with an initial state
33//!     let initial_game_state = GameState { health: 100 };
34//!     game.run(initial_game_state);
35//! }
36//!
37//! // Your game logic functions can be named anything, but the first parameter is always a
38//! // `&mut Engine`, and the second parameter is a mutable reference to your custom game
39//! // state struct (`&mut GameState` in this case).
40//! //
41//! // This function will be run once each frame.
42//! fn game_logic(engine: &mut Engine, game_state: &mut GameState) {
43//!     // The `Engine` contains all sorts of built-in goodies.
44//!     // Get access to the player sprite...
45//!     let player = engine.sprites.get_mut("player").unwrap();
46//!     // Rotate the player...
47//!     player.rotation += std::f32::consts::PI * engine.delta_f32;
48//!     // Damage the player if it is out of bounds...
49//!     if player.translation.x > 100.0 {
50//!         game_state.health -= 1;
51//!     }
52//! }
53//! ```
54//!
55//! # Asset Licenses
56//!
57//! All assets included with this game engine have the appropriate license described and linked to
58//! in a `README.md` file in the same directory as the source files. In most cases, the license is
59//! [CC0 1.0 Universal](https://creativecommons.org/publicdomain/zero/1.0/)--meaning you may do
60//! whatever you wish with the asset.
61//!
62//! One notable exception is some of the music files, which are under a different license and
63//! include specific attribution requirements that must be met in order to be used legally when
64//! distributed. Please see
65//! [this `README.md` file](https://github.com/CleanCut/rusty_engine/tree/main/assets/audio/music)
66//! for more information.
67//!
68pub mod audio;
69pub mod game;
70pub mod keyboard;
71pub mod mouse;
72pub mod physics;
73pub mod sprite;
74pub mod text;
75
76// Public prelude
77pub mod prelude {
78    pub use crate::{audio::*, game::*, keyboard::*, mouse::*, physics::*, sprite::*, text::*};
79    pub use crate::{
80        DOWN, EAST, LEFT, NORTH, NORTH_EAST, NORTH_WEST, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, UP,
81        WEST,
82    };
83    pub use bevy::ecs as bevy_ecs;
84    pub use bevy::{
85        self,
86        prelude::{Resource, Time, Timer, TimerMode, Vec2},
87    };
88}
89
90// Used in our public constants
91use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, PI};
92
93// Screen directions
94/// The rotation (in radians) for a sprite to face right
95pub const RIGHT: f32 = 0.0;
96/// The rotation (in radians) for a sprite to face left
97pub const LEFT: f32 = PI;
98/// The rotation (in radians) for a sprite to face up
99pub const UP: f32 = FRAC_PI_2;
100/// The rotation (in radians) for a sprite to face down
101pub const DOWN: f32 = PI + FRAC_PI_2;
102
103// Compass directions
104/// The rotation (in radians) for a sprite to face north
105pub const NORTH: f32 = FRAC_PI_2;
106/// The rotation (in radians) for a sprite to face north east
107pub const NORTH_EAST: f32 = FRAC_PI_4;
108/// The rotation (in radians) for a sprite to face east
109pub const EAST: f32 = 0.0;
110/// The rotation (in radians) for a sprite to face south east
111pub const SOUTH_EAST: f32 = PI + FRAC_PI_2 + FRAC_PI_4;
112/// The rotation (in radians) for a sprite to face south
113pub const SOUTH: f32 = PI + FRAC_PI_2;
114/// The rotation (in radians) for a sprite to face south west
115pub const SOUTH_WEST: f32 = PI + FRAC_PI_4;
116/// The rotation (in radians) for a sprite to face west
117pub const WEST: f32 = PI;
118/// The rotation (in radians) for a sprite to face north west
119pub const NORTH_WEST: f32 = FRAC_PI_2 + FRAC_PI_4;