mirror of
https://github.com/Noratrieb/game-wip-dontplay.git
synced 2026-01-16 04:25:00 +01:00
Properly center screen
This commit is contained in:
parent
a3cb228799
commit
c54af3ee67
4 changed files with 27 additions and 1 deletions
19
src/math.rs
19
src/math.rs
|
|
@ -1,3 +1,5 @@
|
|||
use num_traits::Signed;
|
||||
|
||||
use crate::world::{TilePos, TilePosScalar};
|
||||
|
||||
pub type WorldPosScalar = u32;
|
||||
|
|
@ -50,6 +52,23 @@ pub fn wp_to_tp(wp: WorldPosScalar) -> TilePosScalar {
|
|||
(wp / TILE_SIZE as WorldPosScalar) as TilePosScalar
|
||||
}
|
||||
|
||||
// Get the offset required to center an object of `xw` width inside an object of `yw` width.
|
||||
//
|
||||
// For example, let's say `xw` (+) is 10 and we want to center it inside `yw` (-), which is 20
|
||||
//
|
||||
// ++++++++++ (x uncentered)
|
||||
// -------------------- (y)
|
||||
// ++++++++++ (x centered)
|
||||
//
|
||||
// In this case, we needed to add 5 to x to achieve centering.
|
||||
// This is the offset that this function calculates.
|
||||
//
|
||||
// We can calulate it by subtracting `xw` from `yw` (10), and dividing it by 2.
|
||||
pub fn center_offset<N: From<u8> + Copy + Signed>(xw: N, yw: N) -> N {
|
||||
let diff = yw - xw;
|
||||
diff / N::from(2)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wp_to_tp() {
|
||||
assert_eq!(wp_to_tp(0), 0);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue