From 9f19becf0c870d4caa4dff89a1762e5aa746d48f Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 16 Feb 2024 18:26:21 -0800 Subject: [PATCH] Added outfit purchase directives --- crates/galactica/src/game.rs | 49 ++++++++++++++++++++------- crates/render/src/ui/api/directive.rs | 21 ++++++++++++ crates/system/src/playerdirective.rs | 26 ++++++++++++++ 3 files changed, 84 insertions(+), 12 deletions(-) diff --git a/crates/galactica/src/game.rs b/crates/galactica/src/game.rs index 578e47a..2e9a5b1 100644 --- a/crates/galactica/src/game.rs +++ b/crates/galactica/src/game.rs @@ -5,6 +5,7 @@ use galactica_system::phys::{PhysImage, PhysSim, PhysSimShipHandle, PhysStepReso use galactica_system::PlayerDirective; use galactica_util::timing::Timing; use nalgebra::Point2; +use std::iter; use std::sync::Arc; use std::time::Instant; @@ -51,16 +52,6 @@ impl<'a> Game { .get(&ContentIndex::new("shield generator")) .unwrap() .clone(), - self.ct - .outfits - .get(&ContentIndex::new("blaster")) - .unwrap() - .clone(), - self.ct - .outfits - .get(&ContentIndex::new("blaster")) - .unwrap() - .clone(), ]); return player; @@ -131,9 +122,43 @@ impl<'a> Game { } } - pub fn apply_directive(&mut self, directive: PlayerDirective, player: &PlayerAgent) { + pub fn apply_directive(&mut self, directive: PlayerDirective, player: &mut PlayerAgent) { match directive { - _ => self.phys_sim.apply_directive(directive, player), + PlayerDirective::None => {} + + PlayerDirective::BuyOutfit { outfit, count } => { + let cost = outfit.cost * count; + if player.credits >= cost { + player.credits -= cost; + let ship = self + .phys_sim + .get_ship_mut(&PhysSimShipHandle(player.ship.unwrap())) + .unwrap(); + ship.add_outfits(iter::repeat(outfit).take(count as usize)) + } + } + + PlayerDirective::SellOutfit { outfit, count } => { + let ship = self + .phys_sim + .get_ship_mut(&PhysSimShipHandle(player.ship.unwrap())) + .unwrap(); + + if ship.get_data().get_outfits().has_outfit(&outfit, count) { + for _ in 0..count { + ship.remove_outfit(&outfit); + } + } + + player.credits += outfit.cost * count; + } + + PlayerDirective::Engine(_) + | PlayerDirective::Guns(_) + | PlayerDirective::Land + | PlayerDirective::TurnLeft(_) + | PlayerDirective::TurnRight(_) + | PlayerDirective::UnLand => self.phys_sim.apply_directive(directive, player), } } diff --git a/crates/render/src/ui/api/directive.rs b/crates/render/src/ui/api/directive.rs index efabdd7..cfdbe88 100644 --- a/crates/render/src/ui/api/directive.rs +++ b/crates/render/src/ui/api/directive.rs @@ -4,6 +4,7 @@ use rhai::{plugin::*, Dynamic, Module}; #[allow(non_snake_case)] #[allow(non_upper_case_globals)] pub mod player_directive_module { + use crate::ui::api::OutfitState; use galactica_system::PlayerDirective; pub const None: PlayerDirective = PlayerDirective::None; @@ -25,4 +26,24 @@ pub mod player_directive_module { pub fn Guns(state: bool) -> PlayerDirective { PlayerDirective::Guns(state) } + + pub fn BuyOutfit(outfit: OutfitState, count: i64) -> PlayerDirective { + if count <= 0 { + return PlayerDirective::None; + } + PlayerDirective::BuyOutfit { + outfit: outfit.get_outfit().unwrap(), + count: count as u32, + } + } + + pub fn SellOutfit(outfit: OutfitState, count: i64) -> PlayerDirective { + if count <= 0 { + return PlayerDirective::None; + } + PlayerDirective::SellOutfit { + outfit: outfit.get_outfit().unwrap(), + count: count as u32, + } + } } diff --git a/crates/system/src/playerdirective.rs b/crates/system/src/playerdirective.rs index 4f93521..7214910 100644 --- a/crates/system/src/playerdirective.rs +++ b/crates/system/src/playerdirective.rs @@ -1,3 +1,7 @@ +use std::sync::Arc; + +use galactica_content::Outfit; + /// An action the player wants to take in the game. #[derive(Debug, Clone)] pub enum PlayerDirective { @@ -21,4 +25,26 @@ pub enum PlayerDirective { /// Take off from the planet we're landed on UnLand, + + /// Buy an outfit. + /// This directive is ignored if the player does not have enough + /// credits to buy the given number of outfits. + BuyOutfit { + /// The outfit to buy + outfit: Arc, + + /// How many of this outfit to buy + count: u32, + }, + + /// Sell an outfit. + /// This directive is ignored if the player does not have enough + /// outfits to sell. + SellOutfit { + /// The outfit to sell + outfit: Arc, + + /// How many of this outfit to sell + count: u32, + }, }