Minor cleanup

master
Mark 2023-12-23 07:21:14 -08:00
parent cac65cbb73
commit 429c0665d1
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
6 changed files with 31 additions and 17 deletions

View File

@ -6,6 +6,7 @@ pub struct Doodad {
pub sprite: String, pub sprite: String,
pub pos: Point2<Pfloat>, pub pos: Point2<Pfloat>,
pub parallax: Pfloat, pub parallax: Pfloat,
pub height: Pfloat,
} }
impl Spriteable for Doodad { impl Spriteable for Doodad {
@ -15,6 +16,7 @@ impl Spriteable for Doodad {
name: self.sprite.clone(), name: self.sprite.clone(),
angle: Deg { 0: 0.0 }, angle: Deg { 0: 0.0 },
scale: 1.0, scale: 1.0,
height: self.height,
parallax: self.parallax, parallax: self.parallax,
}; };
} }

View File

@ -27,11 +27,11 @@ use crate::{
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
struct Camera { struct Camera {
// Camera center /// Camera center
pos: Point2<Pfloat>, pos: Point2<Pfloat>,
// Camera zoom /// Camera zoom
// (How many game units tall is the viewport?) /// (How many game units tall is the viewport?)
zoom: Pfloat, zoom: Pfloat,
} }
@ -40,29 +40,35 @@ trait Spriteable {
} }
struct Sprite { struct Sprite {
// Name of the sprite to draw /// Name of the sprite to draw
name: String, name: String,
// This object's position, in world coordinates. /// This object's position, in world coordinates.
pos: Point2<Pfloat>, pos: Point2<Pfloat>,
/// The size of this sprite,
/// given as height in world units.
height: Pfloat,
/// Scale factor.
/// if this is 1, sprite height is exactly self.height.
scale: Pfloat, scale: Pfloat,
// This sprite's rotation /// This sprite's rotation
// (relative to north, measured ccw) /// (relative to north, measured ccw)
angle: Deg<Pfloat>, angle: Deg<Pfloat>,
// Parallax factor. /// Parallax factor.
// More positive => farther away /// More positive => farther away
// More negative => closer /// More negative => closer
// Zero: no parallax. /// Zero: no parallax.
parallax: Pfloat, parallax: Pfloat,
} }
impl Sprite { impl Sprite {
const PARALLAX_STRENGTH: Pfloat = 0.1; const PARALLAX_STRENGTH: Pfloat = 0.1;
// Returns post-parallax position in game coordinates. /// Returns post-parallax position in game coordinates.
pub fn post_parallax_position(&self, camera: Camera) -> Point2<f32> { pub fn post_parallax_position(&self, camera: Camera) -> Point2<f32> {
let v = camera.pos.to_vec() - self.pos.to_vec(); let v = camera.pos.to_vec() - self.pos.to_vec();
return self.pos + (v * self.parallax * Self::PARALLAX_STRENGTH); return self.pos + (v * self.parallax * Self::PARALLAX_STRENGTH);

View File

@ -269,7 +269,7 @@ impl GPUState {
// but we omit the division. // but we omit the division.
// This gives us a small margin, and lets us re-use the value // This gives us a small margin, and lets us re-use the value
// without an extra multiply. // without an extra multiply.
let height = texture.height * s.scale; let height = s.height * s.scale;
let width = height * texture.aspect; let width = height * texture.aspect;
// Don't draw (or compute matrices for) // Don't draw (or compute matrices for)

View File

@ -16,7 +16,6 @@ pub struct TextureArray {
pub struct Texture { pub struct Texture {
pub index: u32, // Index in texture array pub index: u32, // Index in texture array
pub aspect: f32, // width / height pub aspect: f32, // width / height
pub height: f32, // Height in game units
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -29,7 +28,6 @@ struct Textures {
struct Tex { struct Tex {
name: String, name: String,
path: String, path: String,
height: f32,
} }
impl Tex { impl Tex {
@ -68,7 +66,6 @@ impl TextureArray {
let raw = texture_index.error.read(device, queue)?; let raw = texture_index.error.read(device, queue)?;
let error_texture = Texture { let error_texture = Texture {
index: 0, index: 0,
height: texture_index.error.height,
aspect: raw.dimensions.0 as f32 / raw.dimensions.1 as f32, aspect: raw.dimensions.0 as f32 / raw.dimensions.1 as f32,
}; };
texture_data.push(raw); texture_data.push(raw);
@ -79,7 +76,6 @@ impl TextureArray {
let raw = t.read(device, queue)?; let raw = t.read(device, queue)?;
let tx = Texture { let tx = Texture {
index: i, index: i,
height: t.height,
aspect: raw.dimensions.0 as f32 / raw.dimensions.1 as f32, aspect: raw.dimensions.0 as f32 / raw.dimensions.1 as f32,
}; };
textures.insert(t.name.clone(), tx); textures.insert(t.name.clone(), tx);

View File

@ -12,6 +12,12 @@ impl ShipKind {
Self::Gypsum => "gypsum", Self::Gypsum => "gypsum",
} }
} }
fn height(&self) -> Pfloat {
match self {
Self::Gypsum => 100.0,
}
}
} }
pub struct Ship { pub struct Ship {
@ -36,6 +42,7 @@ impl Spriteable for Ship {
angle: self.body.angle, angle: self.body.angle,
scale: 1.0, scale: 1.0,
parallax: 0.0, parallax: 0.0,
height: self.kind.height(),
}; };
} }
} }

View File

@ -13,6 +13,7 @@ impl System {
pos: (0.0, 0.0).into(), pos: (0.0, 0.0).into(),
sprite: "a0".to_owned(), sprite: "a0".to_owned(),
parallax: 3.0, parallax: 3.0,
height: 80.0,
}); });
s.bodies.push(Doodad { s.bodies.push(Doodad {
@ -24,6 +25,7 @@ impl System {
.to_cartesian(), .to_cartesian(),
sprite: "earth".to_owned(), sprite: "earth".to_owned(),
parallax: 1.0, parallax: 1.0,
height: 120.0,
}); });
s.bodies.push(Doodad { s.bodies.push(Doodad {
@ -35,6 +37,7 @@ impl System {
.to_cartesian(), .to_cartesian(),
sprite: "small".to_owned(), sprite: "small".to_owned(),
parallax: -1.0, parallax: -1.0,
height: 50.0,
}); });
return s; return s;