Added more gameobject infrastructure

master
Mark 2024-01-08 23:05:07 -08:00
parent 9e0551ae12
commit 1b9e1f2877
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
6 changed files with 121 additions and 13 deletions

View File

@ -0,0 +1,47 @@
use std::collections::HashMap;
use crate::{
handles::GameShipHandle,
ship::Ship,
ship::{OutfitSet, ShipPersonality},
};
use content::ShipHandle;
use galactica_content as content;
/// Keeps track of all objects in the galaxy.
/// This struct does NO physics, it keeps track of data exclusively.
pub struct GameData {
// Universal counter.
// Used to create unique handles for game objects.
index: u64,
ships: HashMap<GameShipHandle, Ship>,
}
impl GameData {
pub fn new() -> Self {
Self {
index: 0,
ships: HashMap::new(),
}
}
/// Spawn a ship
pub fn create_ship(
&mut self,
ct: &content::Content,
ship: ShipHandle,
faction: content::FactionHandle,
personality: ShipPersonality,
outfits: OutfitSet,
) -> GameShipHandle {
let handle = GameShipHandle { index: self.index };
self.index += 1;
self.ships
.insert(handle, Ship::new(ct, ship, faction, personality, outfits));
return handle;
}
pub fn get_ship(&self, handle: GameShipHandle) -> Option<&Ship> {
self.ships.get(&handle)
}
}

View File

@ -0,0 +1,5 @@
/// A lightweight representation of a ship in the galaxy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct GameShipHandle {
pub(crate) index: u64,
}

View File

@ -4,10 +4,11 @@
//! of every ship in the game, but it has no understanding of physics. //! of every ship in the game, but it has no understanding of physics.
//! That is done in `galactica_world`. //! That is done in `galactica_world`.
mod projectile; mod gamedata;
mod ship; mod handles;
pub mod ship;
mod system; mod system;
pub use projectile::Projectile; pub use gamedata::*;
pub use ship::Ship; pub use handles::*;
pub use system::{System, SystemObject}; pub use system::{System, SystemObject};

View File

@ -1,5 +1,7 @@
mod outfitset; mod outfitset;
mod personality;
mod ship; mod ship;
pub use outfitset::*; pub use outfitset::*;
pub use personality::*;
pub use ship::*; pub use ship::*;

View File

@ -0,0 +1,14 @@
/// Computer-controlled ship behavior variants.
/// This is just a list, actual physics-aware
/// behaviors are implemented in [`galactica-behavior`]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShipPersonality {
/// This ship is controlled by a player
Player,
/// Does nothing
Dummy,
/// Points and shoots towards the nearest enemy
Point,
}

View File

@ -1,30 +1,33 @@
use std::time::Instant; use std::time::Instant;
use super::OutfitSet; use super::{OutfitSet, ShipPersonality};
use galactica_content as content; use galactica_content as content;
#[derive(Debug)] #[derive(Debug)]
pub struct Ship { pub struct Ship {
// Metadata values // Metadata values
pub ct_handle: content::ShipHandle, ct_handle: content::ShipHandle,
pub faction: content::FactionHandle, faction: content::FactionHandle,
pub outfits: OutfitSet, outfits: OutfitSet,
personality: ShipPersonality,
// State values // State values
// TODO: unified ship stats struct, like space // TODO: unified ship stats struct, like outfit space
pub hull: f32, hull: f32,
pub shields: f32, shields: f32,
// Utility values // Utility values
/// The last time this ship was damaged /// The last time this ship was damaged
pub last_hit: Instant, last_hit: Instant,
} }
impl Ship { impl Ship {
pub fn new( pub(crate) fn new(
ct: &content::Content, ct: &content::Content,
ct_handle: content::ShipHandle, ct_handle: content::ShipHandle,
faction: content::FactionHandle, faction: content::FactionHandle,
personality: ShipPersonality,
outfits: OutfitSet, outfits: OutfitSet,
) -> Self { ) -> Self {
let s = ct.get_ship(ct_handle); let s = ct.get_ship(ct_handle);
@ -33,6 +36,7 @@ impl Ship {
ct_handle, ct_handle,
faction, faction,
outfits, outfits,
personality,
last_hit: Instant::now(), last_hit: Instant::now(),
// Initial stats // Initial stats
@ -77,3 +81,38 @@ impl Ship {
} }
} }
} }
// Misc getters, so internal state is untouchable
impl Ship {
/// Get this ship's current hull.
/// Use content handle to get maximum hull
pub fn get_hull(&self) -> f32 {
return self.hull;
}
/// Get this ship's current shields.
/// Use get_outfits() for maximum shields
pub fn get_shields(&self) -> f32 {
self.shields
}
/// Get all outfits on this ship
pub fn get_outfits(&self) -> &OutfitSet {
&self.outfits
}
/// Get this ship's personality
pub fn get_personality(&self) -> ShipPersonality {
self.personality
}
/// Get this ship's faction
pub fn get_faction(&self) -> content::FactionHandle {
self.faction
}
/// Get this ship's content handle
pub fn get_ship(&self) -> content::ShipHandle {
self.ct_handle
}
}