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

View File

@ -27,11 +27,11 @@ use crate::{
#[derive(Debug, Clone, Copy)]
struct Camera {
// Camera center
/// Camera center
pos: Point2<Pfloat>,
// Camera zoom
// (How many game units tall is the viewport?)
/// Camera zoom
/// (How many game units tall is the viewport?)
zoom: Pfloat,
}
@ -40,29 +40,35 @@ trait Spriteable {
}
struct Sprite {
// Name of the sprite to draw
/// Name of the sprite to draw
name: String,
// This object's position, in world coordinates.
/// This object's position, in world coordinates.
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,
// This sprite's rotation
// (relative to north, measured ccw)
/// This sprite's rotation
/// (relative to north, measured ccw)
angle: Deg<Pfloat>,
// Parallax factor.
// More positive => farther away
// More negative => closer
// Zero: no parallax.
/// Parallax factor.
/// More positive => farther away
/// More negative => closer
/// Zero: no parallax.
parallax: Pfloat,
}
impl Sprite {
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> {
let v = camera.pos.to_vec() - self.pos.to_vec();
return self.pos + (v * self.parallax * Self::PARALLAX_STRENGTH);

View File

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

View File

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

View File

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

View File

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