From 6631096acfb02c7453a605bc26b541477e8b3cf3 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 20 Dec 2023 20:53:23 -0800 Subject: [PATCH] More cleanup --- src/main.rs | 10 +++++----- src/physics/body.rs | 4 ++-- src/physics/position.rs | 6 ++++++ src/ship.rs | 5 +++-- src/{spriteatlas.rs => sprite.rs} | 33 ++++++++++--------------------- 5 files changed, 26 insertions(+), 32 deletions(-) rename src/{spriteatlas.rs => sprite.rs} (81%) diff --git a/src/main.rs b/src/main.rs index d64f736..bbb7aa6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,15 +6,15 @@ mod doodad; mod inputstatus; mod physics; mod ship; -mod spriteatlas; +mod sprite; use crate::{ doodad::Doodad, inputstatus::InputStatus, physics::PhysVec, physics::Position, ship::Ship, - spriteatlas::SpriteAtlas, + sprite::SpriteAtlas, }; trait Drawable { - /// Return the world position of this object + /// Return the world position of the center of this object. fn position(&self) -> PhysVec; /// Get the position of this drawable on the screen @@ -80,7 +80,7 @@ impl System { } s.bodies.push(Box::new(Doodad { - pos: Position::new_polar(PhysVec { x: 0.0, y: 0.0 }, 0.0, 0.0), + pos: Position::new_cartesian(0.0, 0.0), sprite: "a0.png".to_owned(), scale: 1, angle: 0.0, @@ -142,7 +142,7 @@ fn main() -> Result<(), String> { let mut i = InputStatus::new(); let mut c = Camera::new(); - let mut s = Ship::new(ShipKind::Gypsum); + let mut s = Ship::new(ShipKind::Gypsum, Position::new_cartesian(0.0, 0.0)); //let mut last_frame_time = 0f64; let mut frame_start; diff --git a/src/physics/body.rs b/src/physics/body.rs index 842b465..e61e965 100644 --- a/src/physics/body.rs +++ b/src/physics/body.rs @@ -9,9 +9,9 @@ pub struct PhysBody { } impl PhysBody { - pub fn new() -> Self { + pub fn new(pos: PhysVec) -> Self { return PhysBody { - pos: PhysVec { x: 200.0, y: 200.0 }, + pos, vel: PhysVec { x: 0.0, y: 0.0 }, mass: 1.0, angle: 0.0, diff --git a/src/physics/position.rs b/src/physics/position.rs index 358715b..cc3d185 100644 --- a/src/physics/position.rs +++ b/src/physics/position.rs @@ -7,6 +7,12 @@ pub enum Position { Polar(PhysPol), } +/// A convenient wrapper around PhysVec and PhysPol +/// that allows us to position game objects (like doodads) +/// with whatever scheme we want. +/// +/// This should ONLY be used for game objects, physics +/// calculations should always use PhysVec. impl Position { pub fn new_cartesian(x: f64, y: f64) -> Self { return Self::Cartesian(PhysVec { x, y }); diff --git a/src/ship.rs b/src/ship.rs index 4d618d4..e0fd194 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -3,6 +3,7 @@ use sdl2::video::Window; use crate::physics::PhysBody; use crate::physics::PhysVec; +use crate::physics::Position; use crate::Camera; use crate::Drawable; use crate::ShipKind; @@ -14,9 +15,9 @@ pub struct Ship { } impl Ship { - pub fn new(kind: ShipKind) -> Self { + pub fn new(kind: ShipKind, pos: Position) -> Self { Ship { - body: PhysBody::new(), + body: PhysBody::new(pos.into()), kind, } } diff --git a/src/spriteatlas.rs b/src/sprite.rs similarity index 81% rename from src/spriteatlas.rs rename to src/sprite.rs index 8a27a28..d1dfa7d 100644 --- a/src/spriteatlas.rs +++ b/src/sprite.rs @@ -19,8 +19,8 @@ pub struct Sprite<'a> { impl<'a> Sprite<'a> { /// Draw this sprite on the screen. /// - /// Position represents on-screen position, - /// NOT world position. + /// Position represents the center of the sprite + /// on-screen position, NOT in the world. pub fn draw( &self, canvas: &mut Canvas, @@ -29,10 +29,12 @@ impl<'a> Sprite<'a> { scale: f64, ) -> Result<(), String> { let win_size = PhysVec::from(canvas.window().size()); - let width = self.rect.width(); - let height = self.rect.height(); let scale = scale * self.scale; + // Post-scale dimensions on the screen + let width = self.rect.width() as f64 * scale; + let height = self.rect.height() as f64 * scale; + // Don't draw if we're not on the screen. // An offset is included to ensure we're completely // off the screen. We add the whole width intentionally. @@ -44,31 +46,16 @@ impl<'a> Sprite<'a> { return Ok(()); } - let mut dest = Rect::new( - 0, - 0, - (width as f64 * scale) as u32, - (height as f64 * scale) as u32, - ); - dest.center_on(Point::new( - (width as f64 * scale / 2.0) as i32, - (height as f64 * scale / 2.0) as i32, - )); - - // set the current frame for time - dest.set_x(position.x.round() as i32); - dest.set_y(position.y.round() as i32); + let mut dest = Rect::new(0, 0, width as u32, height as u32); + dest.center_on(Point::new((position.x) as i32, (position.y) as i32)); // copy the frame to the canvas canvas.copy_ex( &self.texture, Some(self.rect), Some(dest), - angle, // angle - Point::new( - (width as f64 * scale / 2.0) as i32, - (height as f64 * scale / 2.0) as i32, - ), // center + angle, // angle + Point::new((width / 2.0) as i32, (height / 2.0) as i32), // Rotation center false, false, )?;