More cleanup

master
Mark 2023-12-20 20:53:23 -08:00
parent 144fe78b85
commit 6631096acf
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
5 changed files with 26 additions and 32 deletions

View File

@ -6,15 +6,15 @@ mod doodad;
mod inputstatus; mod inputstatus;
mod physics; mod physics;
mod ship; mod ship;
mod spriteatlas; mod sprite;
use crate::{ use crate::{
doodad::Doodad, inputstatus::InputStatus, physics::PhysVec, physics::Position, ship::Ship, doodad::Doodad, inputstatus::InputStatus, physics::PhysVec, physics::Position, ship::Ship,
spriteatlas::SpriteAtlas, sprite::SpriteAtlas,
}; };
trait Drawable { trait Drawable {
/// Return the world position of this object /// Return the world position of the center of this object.
fn position(&self) -> PhysVec; fn position(&self) -> PhysVec;
/// Get the position of this drawable on the screen /// Get the position of this drawable on the screen
@ -80,7 +80,7 @@ impl System {
} }
s.bodies.push(Box::new(Doodad { 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(), sprite: "a0.png".to_owned(),
scale: 1, scale: 1,
angle: 0.0, angle: 0.0,
@ -142,7 +142,7 @@ fn main() -> Result<(), String> {
let mut i = InputStatus::new(); let mut i = InputStatus::new();
let mut c = Camera::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 last_frame_time = 0f64;
let mut frame_start; let mut frame_start;

View File

@ -9,9 +9,9 @@ pub struct PhysBody {
} }
impl PhysBody { impl PhysBody {
pub fn new() -> Self { pub fn new(pos: PhysVec) -> Self {
return PhysBody { return PhysBody {
pos: PhysVec { x: 200.0, y: 200.0 }, pos,
vel: PhysVec { x: 0.0, y: 0.0 }, vel: PhysVec { x: 0.0, y: 0.0 },
mass: 1.0, mass: 1.0,
angle: 0.0, angle: 0.0,

View File

@ -7,6 +7,12 @@ pub enum Position {
Polar(PhysPol), 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 { impl Position {
pub fn new_cartesian(x: f64, y: f64) -> Self { pub fn new_cartesian(x: f64, y: f64) -> Self {
return Self::Cartesian(PhysVec { x, y }); return Self::Cartesian(PhysVec { x, y });

View File

@ -3,6 +3,7 @@ use sdl2::video::Window;
use crate::physics::PhysBody; use crate::physics::PhysBody;
use crate::physics::PhysVec; use crate::physics::PhysVec;
use crate::physics::Position;
use crate::Camera; use crate::Camera;
use crate::Drawable; use crate::Drawable;
use crate::ShipKind; use crate::ShipKind;
@ -14,9 +15,9 @@ pub struct Ship {
} }
impl Ship { impl Ship {
pub fn new(kind: ShipKind) -> Self { pub fn new(kind: ShipKind, pos: Position) -> Self {
Ship { Ship {
body: PhysBody::new(), body: PhysBody::new(pos.into()),
kind, kind,
} }
} }

View File

@ -19,8 +19,8 @@ pub struct Sprite<'a> {
impl<'a> Sprite<'a> { impl<'a> Sprite<'a> {
/// Draw this sprite on the screen. /// Draw this sprite on the screen.
/// ///
/// Position represents on-screen position, /// Position represents the center of the sprite
/// NOT world position. /// on-screen position, NOT in the world.
pub fn draw( pub fn draw(
&self, &self,
canvas: &mut Canvas<Window>, canvas: &mut Canvas<Window>,
@ -29,10 +29,12 @@ impl<'a> Sprite<'a> {
scale: f64, scale: f64,
) -> Result<(), String> { ) -> Result<(), String> {
let win_size = PhysVec::from(canvas.window().size()); let win_size = PhysVec::from(canvas.window().size());
let width = self.rect.width();
let height = self.rect.height();
let scale = scale * self.scale; 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. // Don't draw if we're not on the screen.
// An offset is included to ensure we're completely // An offset is included to ensure we're completely
// off the screen. We add the whole width intentionally. // off the screen. We add the whole width intentionally.
@ -44,20 +46,8 @@ impl<'a> Sprite<'a> {
return Ok(()); return Ok(());
} }
let mut dest = Rect::new( let mut dest = Rect::new(0, 0, width as u32, height as u32);
0, dest.center_on(Point::new((position.x) as i32, (position.y) as i32));
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);
// copy the frame to the canvas // copy the frame to the canvas
canvas.copy_ex( canvas.copy_ex(
@ -65,10 +55,7 @@ impl<'a> Sprite<'a> {
Some(self.rect), Some(self.rect),
Some(dest), Some(dest),
angle, // angle angle, // angle
Point::new( Point::new((width / 2.0) as i32, (height / 2.0) as i32), // Rotation center
(width as f64 * scale / 2.0) as i32,
(height as f64 * scale / 2.0) as i32,
), // center
false, false,
false, false,
)?; )?;