184 lines
4.5 KiB
Rust
Raw Normal View History

2024-01-03 13:19:10 -08:00
use anyhow::{bail, Context, Result};
2023-12-28 17:04:41 -08:00
use cgmath::Deg;
2024-01-03 13:19:10 -08:00
use serde::Deserialize;
use std::collections::HashMap;
2023-12-27 20:13:39 -08:00
use crate::{handle::SpriteHandle, Content};
2024-01-05 12:09:59 -08:00
use crate::{ContentBuildContext, EffectHandle, OutfitSpace};
2023-12-30 21:05:06 -08:00
2023-12-30 16:57:03 -08:00
pub(crate) mod syntax {
2024-01-05 12:09:59 -08:00
use crate::part::effect;
use crate::part::outfitspace;
2023-12-27 20:13:39 -08:00
use serde::Deserialize;
// Raw serde syntax structs.
// These are never seen by code outside this crate.
#[derive(Debug, Deserialize)]
pub struct Gun {
pub projectile: Projectile,
2023-12-28 17:04:41 -08:00
pub rate: f32,
pub rate_rng: f32,
2024-01-05 12:09:59 -08:00
pub space: outfitspace::syntax::OutfitSpace,
2023-12-27 20:13:39 -08:00
}
#[derive(Debug, Deserialize)]
pub struct Projectile {
pub sprite: String,
2023-12-27 20:13:39 -08:00
pub size: f32,
2023-12-28 17:04:41 -08:00
pub size_rng: f32,
2023-12-27 20:13:39 -08:00
pub speed: f32,
2023-12-28 17:04:41 -08:00
pub speed_rng: f32,
2023-12-27 20:13:39 -08:00
pub lifetime: f32,
2023-12-28 17:04:41 -08:00
pub lifetime_rng: f32,
pub damage: f32,
2024-01-01 15:41:47 -08:00
pub angle_rng: f32,
2024-01-05 12:09:59 -08:00
pub impact_effect: Option<effect::syntax::EffectReference>,
pub expire_effect: Option<effect::syntax::EffectReference>,
2024-01-03 13:19:10 -08:00
pub collider: super::ProjectileCollider,
pub force: f32,
2023-12-27 20:13:39 -08:00
}
2024-01-03 13:19:10 -08:00
}
/// Defines a projectile's collider
#[derive(Debug, Deserialize, Clone)]
pub enum ProjectileCollider {
/// A ball collider
#[serde(rename = "ball")]
Ball(BallCollider),
}
2024-01-05 18:04:30 -08:00
/// A simple ball-shaped collider, centered at the object's position
2024-01-03 13:19:10 -08:00
#[derive(Debug, Deserialize, Clone)]
pub struct BallCollider {
2024-01-05 18:04:30 -08:00
/// The radius of this ball
2024-01-03 13:19:10 -08:00
pub radius: f32,
}
2023-12-29 15:46:09 -08:00
/// Represents a gun outfit.
2023-12-27 20:13:39 -08:00
#[derive(Debug, Clone)]
pub struct Gun {
2023-12-29 15:46:09 -08:00
/// The name of this gun
2023-12-27 20:13:39 -08:00
pub name: String,
2023-12-29 15:46:09 -08:00
/// The projectile this gun produces
2023-12-27 20:13:39 -08:00
pub projectile: Projectile,
2023-12-29 15:46:09 -08:00
/// Average delay between projectiles, in seconds.
2023-12-28 17:04:41 -08:00
pub rate: f32,
2023-12-29 15:46:09 -08:00
/// Random variation of projectile delay, in seconds.
/// Each shot waits (rate += rate_rng).
2023-12-28 17:04:41 -08:00
pub rate_rng: f32,
2023-12-30 21:05:06 -08:00
/// How much space this gun uses
pub space: OutfitSpace,
2023-12-27 20:13:39 -08:00
}
2023-12-29 15:46:09 -08:00
/// Represents a projectile that a [`Gun`] produces.
2023-12-27 20:13:39 -08:00
#[derive(Debug, Clone)]
pub struct Projectile {
2023-12-29 15:46:09 -08:00
/// The projectile sprite
pub sprite: SpriteHandle,
2023-12-29 15:46:09 -08:00
/// The average size of this projectile
/// (height in game units)
2023-12-27 20:13:39 -08:00
pub size: f32,
2023-12-29 15:46:09 -08:00
/// Random size variation
2023-12-28 17:04:41 -08:00
pub size_rng: f32,
2023-12-29 15:46:09 -08:00
/// The speed of this projectile, in game units / second
2023-12-27 20:13:39 -08:00
pub speed: f32,
2023-12-29 15:46:09 -08:00
/// Random speed variation
2023-12-28 17:04:41 -08:00
pub speed_rng: f32,
2023-12-29 15:46:09 -08:00
/// The lifespan of this projectile.
/// It will vanish if it lives this long without hitting anything.
2023-12-27 20:13:39 -08:00
pub lifetime: f32,
2023-12-29 15:46:09 -08:00
/// Random lifetime variation
2023-12-28 17:04:41 -08:00
pub lifetime_rng: f32,
2023-12-29 15:46:09 -08:00
/// The damage this projectile does
pub damage: f32,
2024-01-01 15:41:47 -08:00
2024-01-03 13:19:10 -08:00
/// The force this projectile applies
pub force: f32,
2024-01-01 15:41:47 -08:00
/// The angle variation of this projectile.
/// Projectiles can be off center up to
/// `spread / 2` degrees in both directions.
///
/// (Forming a "fire cone" of `spread` degrees)
pub angle_rng: Deg<f32>,
2024-01-03 13:19:10 -08:00
/// The particle this projectile will spawn when it hits something
2024-01-05 12:09:59 -08:00
pub impact_effect: Option<EffectHandle>,
2024-01-03 13:19:10 -08:00
/// The particle this projectile will spawn when it expires
2024-01-05 12:09:59 -08:00
pub expire_effect: Option<EffectHandle>,
2024-01-03 13:19:10 -08:00
/// Collider parameters for this projectile
pub collider: ProjectileCollider,
}
2023-12-30 16:57:03 -08:00
impl crate::Build for Gun {
type InputSyntaxType = HashMap<String, syntax::Gun>;
2024-01-05 12:09:59 -08:00
fn build(
gun: Self::InputSyntaxType,
build_context: &mut ContentBuildContext,
content: &mut Content,
) -> Result<()> {
2023-12-27 20:13:39 -08:00
for (gun_name, gun) in gun {
2024-01-05 12:09:59 -08:00
let projectile_sprite_handle = match content.sprite_index.get(&gun.projectile.sprite) {
None => bail!(
2024-01-05 12:09:59 -08:00
"projectile sprite `{}` doesn't exist in gun `{}`",
gun.projectile.sprite,
gun_name,
),
Some(t) => *t,
};
2024-01-05 12:09:59 -08:00
let impact_effect = match gun.projectile.impact_effect {
Some(e) => Some(
e.to_handle(build_context, content)
.with_context(|| format!("while loading gun `{}`", gun_name))?,
),
None => None,
};
2024-01-03 13:19:10 -08:00
2024-01-05 12:09:59 -08:00
let expire_effect = match gun.projectile.expire_effect {
Some(e) => Some(
e.to_handle(build_context, content)
.with_context(|| format!("while loading gun `{}`", gun_name))?,
),
None => None,
};
2024-01-03 13:19:10 -08:00
2024-01-05 12:09:59 -08:00
content.guns.push(Self {
name: gun_name,
2023-12-30 21:05:06 -08:00
space: gun.space.into(),
2023-12-28 17:04:41 -08:00
rate: gun.rate,
rate_rng: gun.rate_rng,
2023-12-27 20:13:39 -08:00
projectile: Projectile {
2024-01-03 13:19:10 -08:00
force: gun.projectile.force,
sprite: projectile_sprite_handle,
2023-12-27 20:13:39 -08:00
size: gun.projectile.size,
2023-12-28 17:04:41 -08:00
size_rng: gun.projectile.size_rng,
2023-12-27 20:13:39 -08:00
speed: gun.projectile.speed,
2023-12-28 17:04:41 -08:00
speed_rng: gun.projectile.speed_rng,
2023-12-27 20:13:39 -08:00
lifetime: gun.projectile.lifetime,
2023-12-28 17:04:41 -08:00
lifetime_rng: gun.projectile.lifetime_rng,
damage: gun.projectile.damage,
2024-01-01 15:41:47 -08:00
angle_rng: Deg(gun.projectile.angle_rng),
2024-01-05 12:09:59 -08:00
impact_effect,
expire_effect,
2024-01-03 13:19:10 -08:00
collider: gun.projectile.collider,
2023-12-27 20:13:39 -08:00
},
});
}
return Ok(());
2023-12-27 20:13:39 -08:00
}
}