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 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;

View File

@ -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,

View File

@ -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 });

View File

@ -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,
}
}

View File

@ -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<Window>,
@ -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,
)?;