Added outfit purchase directives

master
Mark 2024-02-16 18:26:21 -08:00
parent a6bc1a021d
commit 9f19becf0c
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
3 changed files with 84 additions and 12 deletions

View File

@ -5,6 +5,7 @@ use galactica_system::phys::{PhysImage, PhysSim, PhysSimShipHandle, PhysStepReso
use galactica_system::PlayerDirective; use galactica_system::PlayerDirective;
use galactica_util::timing::Timing; use galactica_util::timing::Timing;
use nalgebra::Point2; use nalgebra::Point2;
use std::iter;
use std::sync::Arc; use std::sync::Arc;
use std::time::Instant; use std::time::Instant;
@ -51,16 +52,6 @@ impl<'a> Game {
.get(&ContentIndex::new("shield generator")) .get(&ContentIndex::new("shield generator"))
.unwrap() .unwrap()
.clone(), .clone(),
self.ct
.outfits
.get(&ContentIndex::new("blaster"))
.unwrap()
.clone(),
self.ct
.outfits
.get(&ContentIndex::new("blaster"))
.unwrap()
.clone(),
]); ]);
return player; 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 { 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),
} }
} }

View File

@ -4,6 +4,7 @@ use rhai::{plugin::*, Dynamic, Module};
#[allow(non_snake_case)] #[allow(non_snake_case)]
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
pub mod player_directive_module { pub mod player_directive_module {
use crate::ui::api::OutfitState;
use galactica_system::PlayerDirective; use galactica_system::PlayerDirective;
pub const None: PlayerDirective = PlayerDirective::None; pub const None: PlayerDirective = PlayerDirective::None;
@ -25,4 +26,24 @@ pub mod player_directive_module {
pub fn Guns(state: bool) -> PlayerDirective { pub fn Guns(state: bool) -> PlayerDirective {
PlayerDirective::Guns(state) 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,
}
}
} }

View File

@ -1,3 +1,7 @@
use std::sync::Arc;
use galactica_content::Outfit;
/// An action the player wants to take in the game. /// An action the player wants to take in the game.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum PlayerDirective { pub enum PlayerDirective {
@ -21,4 +25,26 @@ pub enum PlayerDirective {
/// Take off from the planet we're landed on /// Take off from the planet we're landed on
UnLand, 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<Outfit>,
/// 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<Outfit>,
/// How many of this outfit to sell
count: u32,
},
} }