Compare commits

..

2 Commits

Author SHA1 Message Date
Mark 0de278cb03
Added outfit space calculations 2023-12-30 21:05:06 -08:00
Mark 19fd1a91f2
Reworked outfits 2023-12-30 20:27:53 -08:00
15 changed files with 343 additions and 147 deletions

View File

@ -1,4 +0,0 @@
[engine."plasma"]
thrust = 100
flare.sprite_texture = "flare::ion"

View File

@ -1,5 +1,7 @@
[gun."blaster"]
space.weapon = 10
# Angle of fire cone
# Smaller angle = more accurate
spread = 2

7
content/outfits.toml Normal file
View File

@ -0,0 +1,7 @@
[outfit."plasma engines"]
space.engine = 20
engine.thrust = 100
engine.flare_texture = "flare::ion"
steering.power = 20

View File

@ -6,6 +6,11 @@ hull = 200
linear_drag = 0.2
angular_drag = 0.2
space.outfit = 200
space.engine = 50
space.weapon = 50
engines = [{ x = 0.0, y = -1.05, size = 50.0 }]
guns = [{ x = 0.0, y = 1 }, { x = 0.1, y = 0.80 }, { x = -0.1, y = 0.80 }]

View File

@ -18,21 +18,23 @@ use toml;
use walkdir::WalkDir;
pub use handle::{FactionHandle, TextureHandle};
pub use part::{Engine, EnginePoint, Faction, Gun, GunPoint, Relationship, Ship, System, Texture};
pub use part::{
EnginePoint, Faction, Gun, GunPoint, Outfit, OutfitSpace, Relationship, Ship, System, Texture,
};
mod syntax {
use anyhow::{bail, Result};
use serde::Deserialize;
use std::collections::HashMap;
use crate::part::{engine, faction, gun, ship, system, texture};
use crate::part::{faction, gun, outfit, ship, system, texture};
#[derive(Debug, Deserialize)]
pub struct Root {
pub gun: Option<HashMap<String, gun::syntax::Gun>>,
pub ship: Option<HashMap<String, ship::syntax::Ship>>,
pub system: Option<HashMap<String, system::syntax::System>>,
pub engine: Option<HashMap<String, engine::syntax::Engine>>,
pub outfit: Option<HashMap<String, outfit::syntax::Outfit>>,
pub texture: Option<HashMap<String, texture::syntax::Texture>>,
pub faction: Option<HashMap<String, faction::syntax::Faction>>,
}
@ -43,7 +45,7 @@ mod syntax {
gun: None,
ship: None,
system: None,
engine: None,
outfit: None,
texture: None,
faction: None,
}
@ -97,11 +99,11 @@ mod syntax {
}
}
if let Some(a) = other.engine {
if self.engine.is_none() {
self.engine = Some(a);
if let Some(a) = other.outfit {
if self.outfit.is_none() {
self.outfit = Some(a);
} else {
let sg = self.engine.as_mut().unwrap();
let sg = self.outfit.as_mut().unwrap();
for (k, v) in a {
if sg.contains_key(&k) {
bail!("Repeated engine name {k}");
@ -165,11 +167,11 @@ pub struct Content {
/// Ship bodies
pub ships: Vec<part::ship::Ship>,
/// Gun outfits
/// Ship guns
pub guns: Vec<part::gun::Gun>,
/// Engine outfits
pub engines: Vec<part::engine::Engine>,
/// Outfits
pub outfits: Vec<part::outfit::Outfit>,
/// Textures
pub textures: Vec<part::texture::Texture>,
@ -256,7 +258,7 @@ impl Content {
systems: Vec::new(),
ships: Vec::new(),
guns: Vec::new(),
engines: Vec::new(),
outfits: Vec::new(),
textures: Vec::new(),
factions: Vec::new(),
texture_index: HashMap::new(),
@ -275,8 +277,8 @@ impl Content {
if root.gun.is_some() {
part::gun::Gun::build(root.gun.take().unwrap(), &mut content)?;
}
if root.engine.is_some() {
part::engine::Engine::build(root.engine.take().unwrap(), &mut content)?;
if root.outfit.is_some() {
part::outfit::Outfit::build(root.outfit.take().unwrap(), &mut content)?;
}
if root.system.is_some() {
part::system::System::build(root.system.take().unwrap(), &mut content)?;

View File

@ -1,62 +0,0 @@
use std::collections::HashMap;
use anyhow::{bail, Result};
use crate::{handle::TextureHandle, Content};
pub(crate) mod syntax {
use serde::Deserialize;
// Raw serde syntax structs.
// These are never seen by code outside this crate.
#[derive(Debug, Deserialize)]
pub struct Engine {
pub thrust: f32,
pub flare: Flare,
}
#[derive(Debug, Deserialize)]
pub struct Flare {
pub sprite_texture: String,
}
}
/// Represents an (foward) engine outfit that may be attached to a ship.
#[derive(Debug, Clone)]
pub struct Engine {
/// The name of this outfit
pub name: String,
/// How much thrust this engine produces
pub thrust: f32,
/// The flare sprite this engine creates.
/// Its location and size is determined by a ship's
/// engine points.
pub flare_sprite_texture: TextureHandle,
}
impl crate::Build for Engine {
type InputSyntax = HashMap<String, syntax::Engine>;
fn build(engine: Self::InputSyntax, ct: &mut Content) -> Result<()> {
for (engine_name, engine) in engine {
let th = match ct.texture_index.get(&engine.flare.sprite_texture) {
None => bail!(
"In engine `{}`: texture `{}` doesn't exist",
engine_name,
engine.flare.sprite_texture
),
Some(t) => *t,
};
ct.engines.push(Self {
name: engine_name,
thrust: engine.thrust,
flare_sprite_texture: th,
});
}
return Ok(());
}
}

View File

@ -5,7 +5,10 @@ use cgmath::Deg;
use crate::{handle::TextureHandle, Content};
use super::OutfitSpace;
pub(crate) mod syntax {
use super::super::shared;
use serde::Deserialize;
// Raw serde syntax structs.
// These are never seen by code outside this crate.
@ -16,6 +19,7 @@ pub(crate) mod syntax {
pub spread: f32,
pub rate: f32,
pub rate_rng: f32,
pub space: shared::syntax::OutfitSpace,
}
#[derive(Debug, Deserialize)]
@ -53,6 +57,9 @@ pub struct Gun {
/// Random variation of projectile delay, in seconds.
/// Each shot waits (rate += rate_rng).
pub rate_rng: f32,
/// How much space this gun uses
pub space: OutfitSpace,
}
/// Represents a projectile that a [`Gun`] produces.
@ -98,6 +105,7 @@ impl crate::Build for Gun {
ct.guns.push(Self {
name: gun_name,
space: gun.space.into(),
spread: Deg(gun.spread),
rate: gun.rate,
rate_rng: gun.rate_rng,

View File

@ -1,15 +1,17 @@
//! Content parts
pub mod engine;
pub mod faction;
pub mod gun;
pub mod outfit;
mod shared;
pub mod ship;
pub mod system;
pub mod texture;
pub use engine::Engine;
pub use faction::{Faction, Relationship};
pub use gun::{Gun, Projectile};
pub use outfit::Outfit;
pub use shared::OutfitSpace;
pub use ship::{EnginePoint, GunPoint, Ship};
pub use system::{Object, System};
pub use texture::Texture;

View File

@ -0,0 +1,92 @@
use std::collections::HashMap;
use anyhow::{bail, Result};
use crate::{handle::TextureHandle, Content};
use super::OutfitSpace;
pub(crate) mod syntax {
use super::super::shared;
use serde::Deserialize;
// Raw serde syntax structs.
// These are never seen by code outside this crate.
#[derive(Debug, Deserialize)]
pub struct Outfit {
pub engine: Option<Engine>,
pub steering: Option<Steering>,
pub space: shared::syntax::OutfitSpace,
}
#[derive(Debug, Deserialize)]
pub struct Engine {
pub thrust: f32,
pub flare_texture: String,
}
#[derive(Debug, Deserialize)]
pub struct Steering {
pub power: f32,
}
}
/// Represents an outfit that may be attached to a ship.
#[derive(Debug, Clone)]
pub struct Outfit {
/// The name of this outfit
pub name: String,
/// How much engine thrust this outfit produces
pub engine_thrust: f32,
/// How much steering power this outfit provids
pub steer_power: f32,
/// The engine flare sprite this outfit creates.
/// Its location and size is determined by a ship's
/// engine points.
pub engine_flare_texture: Option<TextureHandle>,
/// How much space this outfit requires
pub space: OutfitSpace,
}
impl crate::Build for Outfit {
type InputSyntax = HashMap<String, syntax::Outfit>;
fn build(outfits: Self::InputSyntax, ct: &mut Content) -> Result<()> {
for (outfit_name, outfit) in outfits {
let mut o = Self {
name: outfit_name.clone(),
engine_thrust: 0.0,
steer_power: 0.0,
engine_flare_texture: None,
space: OutfitSpace::from(outfit.space),
};
// Engine stats
if let Some(engine) = outfit.engine {
let th = match ct.texture_index.get(&engine.flare_texture) {
None => bail!(
"In outfit `{}`: texture `{}` doesn't exist",
outfit_name,
engine.flare_texture
),
Some(t) => *t,
};
o.engine_thrust = engine.thrust;
o.engine_flare_texture = Some(th);
}
// Steering stats
if let Some(steer) = outfit.steering {
o.steer_power = steer.power;
}
ct.outfits.push(o);
}
return Ok(());
}
}

View File

@ -0,0 +1,67 @@
pub(crate) mod syntax {
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct OutfitSpace {
pub outfit: Option<u32>,
pub weapon: Option<u32>,
pub engine: Option<u32>,
}
}
/// Represents outfit space, either that available in a ship
/// or that used by an outfit.
#[derive(Debug, Clone, Copy)]
pub struct OutfitSpace {
/// Total available outfit space.
/// This should be greater than weapon and engine.
pub outfit: u32,
/// Space for weapons
pub weapon: u32,
/// Space for engine
pub engine: u32,
}
impl OutfitSpace {
/// Make a new, zero OutfitSpace
pub fn new() -> Self {
Self {
outfit: 0,
weapon: 0,
engine: 0,
}
}
/// Does this outfit contain `smaller`?
pub fn can_contain(&self, smaller: Self) -> bool {
self.outfit >= (smaller.outfit + smaller.weapon + smaller.engine)
&& self.weapon >= smaller.weapon
&& self.engine >= smaller.engine
}
/// Free outfit space
pub fn free(&mut self, rhs: &Self) {
self.outfit += rhs.outfit + rhs.weapon + rhs.engine;
self.weapon += rhs.weapon;
self.engine += rhs.engine;
}
/// Occupy outfit space
pub fn occupy(&mut self, rhs: &Self) {
self.outfit -= rhs.outfit + rhs.weapon + rhs.engine;
self.weapon -= rhs.weapon;
self.engine -= rhs.engine;
}
}
impl From<syntax::OutfitSpace> for OutfitSpace {
fn from(value: syntax::OutfitSpace) -> Self {
Self {
outfit: value.outfit.unwrap_or(0),
engine: value.engine.unwrap_or(0),
weapon: value.weapon.unwrap_or(0),
}
}
}

View File

@ -6,8 +6,12 @@ use nalgebra::{point, Point};
use crate::{handle::TextureHandle, Content};
use super::OutfitSpace;
pub(crate) mod syntax {
use super::super::shared;
use serde::Deserialize;
// Raw serde syntax structs.
// These are never seen by code outside this crate.
@ -22,6 +26,7 @@ pub(crate) mod syntax {
pub collision: Collision,
pub angular_drag: f32,
pub linear_drag: f32,
pub space: shared::syntax::OutfitSpace,
}
#[derive(Debug, Deserialize)]
@ -86,6 +91,9 @@ pub struct Ship {
/// Reduction in velocity over time
pub linear_drag: f32,
/// Outfit space in this ship
pub space: OutfitSpace,
}
/// Collision shape for this ship
@ -138,6 +146,7 @@ impl crate::Build for Ship {
name: ship_name,
sprite_texture: th,
mass: ship.mass,
space: OutfitSpace::from(ship.space),
angular_drag: ship.angular_drag,
linear_drag: ship.linear_drag,
size,

View File

@ -31,29 +31,31 @@ impl Game {
pub fn new(ct: Content) -> Self {
let mut physics = Physics::new();
let mut o1 = outfits::ShipOutfits::new(&ct.ships[0]);
o1.add(ct.outfits[0].clone());
o1.add_gun(ct.guns[0].clone());
o1.add_gun(ct.guns[0].clone());
let h1 = physics.add_ship(
&ct.ships[0],
vec![
outfits::ShipOutfit::Gun(outfits::ShipGun::new(&ct.guns[0], 1)),
outfits::ShipOutfit::Gun(outfits::ShipGun::new(&ct.guns[0], 2)),
outfits::ShipOutfit::Engine(ct.engines[0].clone()),
],
o1,
Point2 { x: 0.0, y: 0.0 },
FactionHandle { index: 0 },
);
let h2 = physics.add_ship(
&ct.ships[0],
vec![],
outfits::ShipOutfits::new(&ct.ships[0]),
Point2 { x: 300.0, y: 300.0 },
FactionHandle { index: 0 },
);
let mut o2 = outfits::ShipOutfits::new(&ct.ships[0]);
o2.add(ct.outfits[0].clone());
o2.add_gun(ct.guns[0].clone());
let h3 = physics.add_ship(
&ct.ships[0],
vec![outfits::ShipOutfit::Gun(outfits::ShipGun::new(
&ct.guns[0],
0,
))],
o2,
Point2 {
x: -300.0,
y: 300.0,

View File

@ -1,8 +1,10 @@
use cgmath::{Deg, Point3};
use content::{OutfitSpace, TextureHandle};
use crate::{content, render::SubSprite};
/// Represents a gun attached to a specific ship at a certain gunpoint.
#[derive(Debug)]
pub struct ShipGun {
pub kind: content::Gun,
pub cooldown: f32,
@ -10,39 +12,66 @@ pub struct ShipGun {
}
impl ShipGun {
pub fn new(kind: &content::Gun, point: usize) -> Self {
pub fn new(kind: content::Gun, point: usize) -> Self {
Self {
kind: kind.clone(),
kind: kind,
point,
cooldown: 0.0,
}
}
}
/// Represents a specific outfit attached to a specific ship
pub enum ShipOutfit {
Gun(ShipGun),
Engine(content::Engine),
/// This struct keeps track of the combined stats of a set of outfits.
/// It does NOT check for sanity (e.g, removing an outfit that was never added)
/// That is handled by ShipOutfits.
#[derive(Debug)]
pub struct OutfitStatSum {
pub engine_thrust: f32,
pub steer_power: f32,
pub engine_flare_textures: Vec<TextureHandle>,
}
impl ShipOutfit {
pub fn gun(&mut self) -> Option<&mut ShipGun> {
match self {
Self::Gun(g) => Some(g),
_ => None,
impl OutfitStatSum {
pub fn new() -> Self {
Self {
engine_thrust: 0.0,
steer_power: 0.0,
engine_flare_textures: Vec::new(),
}
}
pub fn engine(&self) -> Option<&content::Engine> {
match self {
Self::Engine(e) => Some(e),
_ => None,
pub fn add(&mut self, o: &content::Outfit) {
self.engine_thrust += o.engine_thrust;
if let Some(t) = o.engine_flare_texture {
self.engine_flare_textures.push(t);
};
self.steer_power += o.steer_power;
}
pub fn remove(&mut self, o: &content::Outfit) {
self.engine_thrust -= o.engine_thrust;
if let Some(t) = o.engine_flare_texture {
self.engine_flare_textures.remove(
self.engine_flare_textures
.iter()
.position(|x| *x == t)
.unwrap(),
);
}
self.steer_power -= o.steer_power;
}
}
/// Represents all the outfits attached to a ship.
/// Keeps track of the sum of their stats, so it mustn't be re-computed every frame.
#[derive(Debug)]
pub struct ShipOutfits {
outfits: Vec<ShipOutfit>,
pub stats: OutfitStatSum,
pub total_space: OutfitSpace,
available_space: OutfitSpace,
outfits: Vec<content::Outfit>,
guns: Vec<ShipGun>,
enginepoints: Vec<content::EnginePoint>,
gunpoints: Vec<content::GunPoint>,
@ -54,66 +83,107 @@ pub struct ShipOutfits {
impl<'a> ShipOutfits {
pub fn new(content: &content::Ship) -> Self {
Self {
stats: OutfitStatSum::new(),
outfits: Vec::new(),
guns: Vec::new(),
available_space: content.space.clone(),
total_space: content.space.clone(),
enginepoints: content.engines.clone(),
gunpoints: content.guns.clone(),
engine_flare_sprites: vec![],
}
}
pub fn add(&mut self, o: ShipOutfit) {
/// Does this outfit set contain the specified outfit?
pub fn contains_outfit(&self, o: &content::Outfit) -> bool {
match self.outfits.iter().position(|x| x.name == o.name) {
Some(_) => true,
None => false,
}
}
/// Add an outfit to this ship.
/// Returns true on success, and false on failure
/// TODO: failure reason enum
pub fn add(&mut self, o: content::Outfit) -> bool {
if !self.available_space.can_contain(o.space) {
return false;
}
self.available_space.occupy(&o.space);
self.stats.add(&o);
self.outfits.push(o);
self.update_engine_flares();
return true;
}
/// TODO: is outfit in set?
pub fn remove(&mut self, o: content::Outfit) {
let i = match self.outfits.iter().position(|x| x.name == o.name) {
Some(i) => i,
None => panic!("Removed non-existing outfit"),
};
self.available_space.free(&o.space);
self.outfits.remove(i);
self.stats.remove(&o);
self.update_engine_flares();
}
/// Add a gun to this outfit set.
/// This automatically attaches the gun to the first available gunpoint,
/// and returns false (applying no changes) if no points are available.
pub fn add_gun(&mut self, gun: content::Gun) -> bool {
// Find first unused point
let mut p = None;
'outer: for i in 0..self.gunpoints.len() {
for g in &self.guns {
if g.point == i {
continue 'outer;
}
}
p = Some(i);
break;
}
// All points are taken
if p.is_none() {
return false;
}
let sg = ShipGun::new(gun, p.unwrap());
self.guns.push(sg);
return true;
}
pub fn iter_guns(&mut self) -> impl Iterator<Item = &mut ShipGun> {
self.outfits
.iter_mut()
.map(|x| x.gun())
.filter(|x| x.is_some())
.map(|x| x.unwrap())
self.guns.iter_mut()
}
pub fn iter_guns_points(&mut self) -> impl Iterator<Item = (&mut ShipGun, &content::GunPoint)> {
self.outfits
self.guns
.iter_mut()
.map(|x| x.gun())
.filter(|x| x.is_some())
.map(|x| x.unwrap())
.map(|x| (&self.gunpoints[x.point], x))
.map(|(a, b)| (b, a))
}
pub fn iter_engines(&self) -> impl Iterator<Item = &content::Engine> {
self.outfits
.iter()
.map(|x| x.engine())
.filter(|x| x.is_some())
.map(|x| x.unwrap())
}
pub fn iter_enginepoints(&self) -> impl Iterator<Item = &content::EnginePoint> {
self.enginepoints.iter()
}
pub fn update_engine_flares(&mut self) {
// TODO: better way to pick flare texture
self.engine_flare_sprites.clear();
let t = if let Some(e) = self.iter_engines().next() {
e.flare_sprite_texture
let t = if let Some(e) = self.stats.engine_flare_textures.iter().next() {
e
} else {
return;
};
self.engine_flare_sprites = self
.iter_enginepoints()
.enginepoints
.iter()
.map(|p| SubSprite {
pos: Point3 {
x: p.pos.x,
y: p.pos.y,
z: 1.0,
},
texture: t,
texture: *t,
angle: Deg(0.0),
size: p.size,
})

View File

@ -52,18 +52,13 @@ pub struct Ship {
impl Ship {
pub fn new(
c: &content::Ship,
outfits: Vec<outfits::ShipOutfit>,
outfits: outfits::ShipOutfits,
physics_handle: ShipHandle,
faction: FactionHandle,
) -> Self {
let mut o = outfits::ShipOutfits::new(c);
for x in outfits.into_iter() {
o.add(x)
}
Ship {
physics_handle,
outfits: o,
outfits,
sprite_texture: c.sprite_texture,
size: c.size,
hull: c.hull,
@ -135,17 +130,18 @@ impl Ship {
let engine_force = ship_rot * t;
if self.controls.thrust {
for e in self.outfits.iter_engines() {
r.apply_impulse(vector![engine_force.x, engine_force.y] * e.thrust, true);
}
r.apply_impulse(
vector![engine_force.x, engine_force.y] * self.outfits.stats.engine_thrust,
true,
);
}
if self.controls.right {
r.apply_torque_impulse(-1000.0 * t, true);
r.apply_torque_impulse(self.outfits.stats.steer_power * -100.0 * t, true);
}
if self.controls.left {
r.apply_torque_impulse(1000.0 * t, true);
r.apply_torque_impulse(self.outfits.stats.steer_power * 100.0 * t, true);
}
let p = if self.controls.guns {

View File

@ -82,7 +82,7 @@ impl Physics {
pub fn add_ship(
&mut self,
ct: &content::Ship,
outfits: Vec<outfits::ShipOutfit>,
outfits: outfits::ShipOutfits,
position: Point2<f32>,
faction: FactionHandle,
) -> ShipHandle {