diff --git a/src/entity/pacman.rs b/src/entity/pacman.rs index 9b4d5a7..b5be2e8 100644 --- a/src/entity/pacman.rs +++ b/src/entity/pacman.rs @@ -1,13 +1,13 @@ -use glam::Vec2; +use glam::{UVec2, Vec2}; use crate::constants::BOARD_PIXEL_OFFSET; use crate::entity::direction::Direction; use crate::entity::graph::{Graph, NodeId, Position, Traverser}; +use crate::helpers::centered_with_size; use crate::texture::animated::AnimatedTexture; use crate::texture::directional::DirectionalAnimatedTexture; use crate::texture::sprite::SpriteAtlas; use sdl2::keyboard::Keycode; -use sdl2::rect::Rect; use sdl2::render::{Canvas, RenderTarget}; use std::collections::HashMap; @@ -71,15 +71,14 @@ impl Pacman { Position::BetweenNodes { from, to, traversed } => { let from_pos = graph.get_node(from).unwrap().position; let to_pos = graph.get_node(to).unwrap().position; - let weight = from_pos.distance(to_pos); - from_pos.lerp(to_pos, traversed / weight) + from_pos.lerp(to_pos, traversed / from_pos.distance(to_pos)) } } } pub fn render(&self, canvas: &mut Canvas, atlas: &mut SpriteAtlas, graph: &Graph) { let pixel_pos = self.get_pixel_pos(graph).round().as_ivec2() + BOARD_PIXEL_OFFSET.as_ivec2(); - let dest = Rect::new(pixel_pos.x - 8, pixel_pos.y - 8, 16, 16); + let dest = centered_with_size(pixel_pos, UVec2::new(16, 16)); let is_stopped = self.traverser.position.is_stopped(); if is_stopped { diff --git a/src/helpers.rs b/src/helpers.rs new file mode 100644 index 0000000..92a19c6 --- /dev/null +++ b/src/helpers.rs @@ -0,0 +1,11 @@ +use glam::{IVec2, UVec2}; +use sdl2::rect::Rect; + +pub fn centered_with_size(pixel_pos: IVec2, size: UVec2) -> Rect { + Rect::new( + pixel_pos.x - size.x as i32 / 2, + pixel_pos.y - size.y as i32 / 2, + size.x, + size.y, + ) +} diff --git a/src/lib.rs b/src/lib.rs index 8bd58ae..1dc07f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,5 +7,6 @@ pub mod constants; pub mod emscripten; pub mod entity; pub mod game; +pub mod helpers; pub mod map; pub mod texture; diff --git a/src/main.rs b/src/main.rs index b32fa74..b33b802 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,6 +56,7 @@ mod constants; mod emscripten; mod entity; mod game; +mod helpers; mod map; mod texture;