Compare commits
No commits in common. "fdce0a7d3b03ded25b997f6a0273626ee915b58e" and "17aa76ac61baaade0e98805d6bc9132f04a0994a" have entirely different histories.
fdce0a7d3b
...
17aa76ac61
|
@ -1,5 +1,7 @@
|
|||
pub const ZOOM_MIN: f32 = 200.0;
|
||||
pub const ZOOM_MAX: f32 = 2000.0;
|
||||
use crate::physics::Pfloat;
|
||||
|
||||
pub const ZOOM_MIN: Pfloat = 200.0;
|
||||
pub const ZOOM_MAX: Pfloat = 2000.0;
|
||||
|
||||
/// Z-axis range for starfield stars
|
||||
/// This does not affect scale.
|
||||
|
|
|
@ -2,10 +2,11 @@ use anyhow::{bail, Context, Result};
|
|||
use cgmath::{Deg, Point3};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use crate::physics::Polar;
|
||||
use crate::physics::{Pfloat, Polar};
|
||||
|
||||
/// Toml file syntax
|
||||
pub(in crate::content) mod toml {
|
||||
use super::Pfloat;
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
|
||||
|
@ -25,10 +26,10 @@ pub(in crate::content) mod toml {
|
|||
pub sprite: String,
|
||||
pub position: Position,
|
||||
|
||||
pub size: f32,
|
||||
pub size: Pfloat,
|
||||
|
||||
pub radius: Option<f32>,
|
||||
pub angle: Option<f32>,
|
||||
pub radius: Option<Pfloat>,
|
||||
pub angle: Option<Pfloat>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
@ -41,16 +42,16 @@ pub(in crate::content) mod toml {
|
|||
#[derive(Debug, Deserialize)]
|
||||
pub struct PolarCoords {
|
||||
pub center: CoordinatesTwo,
|
||||
pub radius: f32,
|
||||
pub angle: f32,
|
||||
pub z: f32,
|
||||
pub radius: Pfloat,
|
||||
pub angle: Pfloat,
|
||||
pub z: Pfloat,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum CoordinatesTwo {
|
||||
Label(String),
|
||||
Coords([f32; 2]),
|
||||
Coords([Pfloat; 2]),
|
||||
}
|
||||
|
||||
impl ToString for CoordinatesTwo {
|
||||
|
@ -66,7 +67,7 @@ pub(in crate::content) mod toml {
|
|||
#[serde(untagged)]
|
||||
pub enum CoordinatesThree {
|
||||
Label(String),
|
||||
Coords([f32; 3]),
|
||||
Coords([Pfloat; 3]),
|
||||
}
|
||||
|
||||
impl ToString for CoordinatesThree {
|
||||
|
@ -89,8 +90,8 @@ pub struct System {
|
|||
pub struct Object {
|
||||
pub sprite: String,
|
||||
pub position: Point3<f32>,
|
||||
pub size: f32,
|
||||
pub angle: Deg<f32>,
|
||||
pub size: Pfloat,
|
||||
pub angle: Deg<Pfloat>,
|
||||
}
|
||||
|
||||
// Helper function for resolve_position, never called on its own.
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
use cgmath::Point2;
|
||||
|
||||
use crate::physics::Pfloat;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Camera {
|
||||
/// Camera center
|
||||
pub pos: Point2<f32>,
|
||||
pub pos: Point2<Pfloat>,
|
||||
|
||||
/// Camera zoom
|
||||
/// (How many game units tall is the viewport?)
|
||||
pub zoom: f32,
|
||||
pub zoom: Pfloat,
|
||||
}
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
use cgmath::{Deg, Point3};
|
||||
|
||||
use crate::{render::Sprite, render::SpriteTexture, render::Spriteable};
|
||||
use crate::{physics::Pfloat, render::Sprite, render::SpriteTexture, render::Spriteable};
|
||||
|
||||
pub struct SystemObject {
|
||||
pub struct Doodad {
|
||||
pub sprite: SpriteTexture,
|
||||
pub pos: Point3<f32>,
|
||||
pub size: f32,
|
||||
pub angle: Deg<f32>,
|
||||
pub pos: Point3<Pfloat>,
|
||||
pub size: Pfloat,
|
||||
pub angle: Deg<Pfloat>,
|
||||
}
|
||||
|
||||
impl Spriteable for SystemObject {
|
||||
impl Spriteable for Doodad {
|
||||
fn get_sprite(&self) -> Sprite {
|
||||
return Sprite {
|
||||
texture: self.sprite.clone(),
|
|
@ -3,7 +3,7 @@ use std::time::Instant;
|
|||
use winit::event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode};
|
||||
|
||||
use super::{ship::ShipKind, Camera, InputStatus, Ship, System};
|
||||
use crate::{consts, content::Content, render::Sprite, render::Spriteable};
|
||||
use crate::{consts, content::Content, physics::Pfloat, render::Sprite, render::Spriteable};
|
||||
|
||||
pub struct Game {
|
||||
pub input: InputStatus,
|
||||
|
@ -40,18 +40,18 @@ impl Game {
|
|||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
let t: f32 = self.last_update.elapsed().as_secs_f32();
|
||||
let t: Pfloat = self.last_update.elapsed().as_secs_f32();
|
||||
|
||||
if self.input.key_thrust {
|
||||
self.player.physicsbody.thrust(50.0 * t);
|
||||
self.player.body.thrust(50.0 * t);
|
||||
}
|
||||
|
||||
if self.input.key_right {
|
||||
self.player.physicsbody.rot(Deg(35.0) * t);
|
||||
self.player.body.rot(Deg(35.0) * t);
|
||||
}
|
||||
|
||||
if self.input.key_left {
|
||||
self.player.physicsbody.rot(Deg(-35.0) * t);
|
||||
self.player.body.rot(Deg(-35.0) * t);
|
||||
}
|
||||
|
||||
if self.input.v_scroll != 0.0 {
|
||||
|
@ -60,8 +60,8 @@ impl Game {
|
|||
self.input.v_scroll = 0.0;
|
||||
}
|
||||
|
||||
self.player.physicsbody.tick(t);
|
||||
self.camera.pos = self.player.physicsbody.pos;
|
||||
self.player.body.tick(t);
|
||||
self.camera.pos = self.player.body.pos;
|
||||
|
||||
self.last_update = Instant::now();
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
mod camera;
|
||||
mod doodad;
|
||||
mod game;
|
||||
mod inputstatus;
|
||||
mod ship;
|
||||
mod system;
|
||||
mod systemobject;
|
||||
|
||||
pub use camera::Camera;
|
||||
pub use doodad::Doodad;
|
||||
pub use game::Game;
|
||||
pub use inputstatus::InputStatus;
|
||||
pub use ship::Ship;
|
||||
pub use system::System;
|
||||
pub use systemobject::SystemObject;
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
use cgmath::Point2;
|
||||
|
||||
use crate::{physics::PhysicsBody, render::Sprite, render::SpriteTexture, render::Spriteable};
|
||||
use crate::{
|
||||
physics::Pfloat, physics::PhysBody, render::Sprite, render::SpriteTexture, render::Spriteable,
|
||||
};
|
||||
|
||||
pub enum ShipKind {
|
||||
Gypsum,
|
||||
|
@ -15,7 +17,7 @@ impl ShipKind {
|
|||
return SpriteTexture(name.to_owned());
|
||||
}
|
||||
|
||||
fn size(&self) -> f32 {
|
||||
fn size(&self) -> Pfloat {
|
||||
match self {
|
||||
Self::Gypsum => 100.0,
|
||||
}
|
||||
|
@ -23,14 +25,14 @@ impl ShipKind {
|
|||
}
|
||||
|
||||
pub struct Ship {
|
||||
pub physicsbody: PhysicsBody,
|
||||
pub body: PhysBody,
|
||||
kind: ShipKind,
|
||||
}
|
||||
|
||||
impl Ship {
|
||||
pub fn new(kind: ShipKind, pos: Point2<f32>) -> Self {
|
||||
pub fn new(kind: ShipKind, pos: Point2<Pfloat>) -> Self {
|
||||
Ship {
|
||||
physicsbody: PhysicsBody::new(pos),
|
||||
body: PhysBody::new(pos),
|
||||
kind,
|
||||
}
|
||||
}
|
||||
|
@ -39,9 +41,9 @@ impl Ship {
|
|||
impl Spriteable for Ship {
|
||||
fn get_sprite(&self) -> Sprite {
|
||||
return Sprite {
|
||||
pos: (self.physicsbody.pos.x, self.physicsbody.pos.y, 1.0).into(),
|
||||
pos: (self.body.pos.x, self.body.pos.y, 1.0).into(),
|
||||
texture: self.kind.sprite(),
|
||||
angle: self.physicsbody.angle,
|
||||
angle: self.body.angle,
|
||||
scale: 1.0,
|
||||
size: self.kind.size(),
|
||||
};
|
||||
|
|
|
@ -1,26 +1,28 @@
|
|||
use cgmath::{Point3, Vector2};
|
||||
use rand::{self, Rng};
|
||||
|
||||
use super::SystemObject;
|
||||
use crate::{consts, content, render::Sprite, render::SpriteTexture, render::Spriteable};
|
||||
use super::Doodad;
|
||||
use crate::{
|
||||
consts, content, physics::Pfloat, render::Sprite, render::SpriteTexture, render::Spriteable,
|
||||
};
|
||||
|
||||
pub struct StarfieldStar {
|
||||
/// Star coordinates, in world space.
|
||||
/// These are relative to the center of a starfield tile.
|
||||
pub pos: Point3<f32>,
|
||||
pub pos: Point3<Pfloat>,
|
||||
|
||||
/// Height in game units.
|
||||
/// Will be scaled for zoom, but not for distance.
|
||||
pub size: f32,
|
||||
pub size: Pfloat,
|
||||
|
||||
/// Color/brightness variation. Random between 0 and 1.
|
||||
/// Used in starfield shader.
|
||||
pub tint: Vector2<f32>,
|
||||
pub tint: Vector2<Pfloat>,
|
||||
}
|
||||
|
||||
pub struct System {
|
||||
pub name: String,
|
||||
bodies: Vec<SystemObject>,
|
||||
bodies: Vec<Doodad>,
|
||||
pub starfield: Vec<StarfieldStar>,
|
||||
}
|
||||
|
||||
|
@ -48,7 +50,7 @@ impl System {
|
|||
};
|
||||
|
||||
for o in &ct.objects {
|
||||
s.bodies.push(SystemObject {
|
||||
s.bodies.push(Doodad {
|
||||
pos: o.position,
|
||||
sprite: SpriteTexture(o.sprite.to_owned()),
|
||||
size: o.size,
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
use super::Pfloat;
|
||||
use cgmath::{Angle, Deg, Point2, Vector2};
|
||||
|
||||
pub struct PhysicsBody {
|
||||
pub pos: Point2<f32>,
|
||||
pub vel: Vector2<f32>,
|
||||
pub mass: f32,
|
||||
pub angle: Deg<f32>,
|
||||
pub struct PhysBody {
|
||||
pub pos: Point2<Pfloat>,
|
||||
pub vel: Vector2<Pfloat>,
|
||||
pub mass: Pfloat,
|
||||
pub angle: Deg<Pfloat>,
|
||||
}
|
||||
|
||||
impl PhysicsBody {
|
||||
pub fn new(pos: Point2<f32>) -> Self {
|
||||
return PhysicsBody {
|
||||
impl PhysBody {
|
||||
pub fn new(pos: Point2<Pfloat>) -> Self {
|
||||
return PhysBody {
|
||||
pos,
|
||||
vel: (0.0, 0.0).into(),
|
||||
mass: 0.3,
|
||||
|
@ -18,17 +19,17 @@ impl PhysicsBody {
|
|||
}
|
||||
|
||||
/// Calculate the position of this body after t seconds.
|
||||
pub fn tick(&mut self, t: f32) {
|
||||
pub fn tick(&mut self, t: Pfloat) {
|
||||
self.pos += self.vel * t;
|
||||
}
|
||||
|
||||
/// Apply an instantaneous force to this object
|
||||
pub fn force(&mut self, v: Vector2<f32>) {
|
||||
pub fn force(&mut self, v: Vector2<Pfloat>) {
|
||||
self.vel += v / self.mass;
|
||||
}
|
||||
|
||||
/// Apply a force in the direction this object is pointing.
|
||||
pub fn thrust(&mut self, f: f32) {
|
||||
pub fn thrust(&mut self, f: Pfloat) {
|
||||
let l = Vector2 {
|
||||
x: -self.angle.sin(),
|
||||
y: self.angle.cos(),
|
||||
|
@ -37,7 +38,7 @@ impl PhysicsBody {
|
|||
}
|
||||
|
||||
// Rotate this object
|
||||
pub fn rot(&mut self, a: Deg<f32>) {
|
||||
pub fn rot(&mut self, a: Deg<Pfloat>) {
|
||||
self.angle -= a;
|
||||
|
||||
// Wrap angles
|
||||
|
|
|
@ -2,5 +2,7 @@ mod body;
|
|||
mod polar;
|
||||
|
||||
// What kind of float shoud we use for physics?
|
||||
pub use body::PhysicsBody;
|
||||
pub type Pfloat = f32;
|
||||
|
||||
pub use body::PhysBody;
|
||||
pub use polar::Polar;
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
use super::Pfloat;
|
||||
use cgmath::{Angle, Deg, Point2, Vector2};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Polar {
|
||||
pub center: Point2<f32>,
|
||||
pub radius: f32,
|
||||
pub angle: Deg<f32>,
|
||||
pub center: Point2<Pfloat>,
|
||||
pub radius: Pfloat,
|
||||
pub angle: Deg<Pfloat>,
|
||||
}
|
||||
|
||||
impl Polar {
|
||||
pub fn to_cartesian(self) -> Point2<f32> {
|
||||
pub fn to_cartesian(self) -> Point2<Pfloat> {
|
||||
let v = Vector2 {
|
||||
x: self.radius * self.angle.sin(),
|
||||
y: self.radius * self.angle.cos(),
|
||||
|
|
|
@ -16,7 +16,7 @@ use super::{
|
|||
VertexBuffer,
|
||||
},
|
||||
};
|
||||
use crate::{consts, game::Game};
|
||||
use crate::{consts, game::Game, physics::Pfloat};
|
||||
|
||||
pub struct GPUState {
|
||||
device: wgpu::Device,
|
||||
|
@ -211,7 +211,7 @@ impl GPUState {
|
|||
for s in game.get_sprites() {
|
||||
// Compute post-parallax position and distance-adjusted scale.
|
||||
// We do this here so we can check if a sprite is on the screen.
|
||||
let pos: Point2<f32> = {
|
||||
let pos: Point2<Pfloat> = {
|
||||
(Point2 {
|
||||
x: s.pos.x,
|
||||
y: s.pos.y,
|
||||
|
|
|
@ -1,25 +1,26 @@
|
|||
use cgmath::{Deg, Point3};
|
||||
|
||||
use super::SpriteTexture;
|
||||
use crate::physics::Pfloat;
|
||||
|
||||
pub struct Sprite {
|
||||
/// Name of the sprite to draw
|
||||
pub texture: SpriteTexture,
|
||||
|
||||
/// This object's position, in world coordinates.
|
||||
pub pos: Point3<f32>,
|
||||
pub pos: Point3<Pfloat>,
|
||||
|
||||
/// The size of this sprite,
|
||||
/// given as height in world units.
|
||||
pub size: f32,
|
||||
pub size: Pfloat,
|
||||
|
||||
/// Scale factor.
|
||||
/// if this is 1, sprite height is exactly self.size.
|
||||
pub scale: f32,
|
||||
pub scale: Pfloat,
|
||||
|
||||
/// This sprite's rotation
|
||||
/// (relative to north, measured ccw)
|
||||
pub angle: Deg<f32>,
|
||||
pub angle: Deg<Pfloat>,
|
||||
}
|
||||
|
||||
pub trait Spriteable {
|
||||
|
|
Loading…
Reference in New Issue