moved blobs of text into text_display mod

This commit is contained in:
Christopher
2023-03-01 19:05:38 +13:00
parent 1732d95032
commit ec3b0697bb
4 changed files with 59 additions and 37 deletions

View File

@@ -1,4 +1,4 @@
use crate::model::{Galaxy, Pos, SectorStatus, COURSES, Quadrant}; use crate::{model::{Galaxy, Pos, SectorStatus, COURSES, Quadrant, EndPosition}, text_display};
pub fn short_range_scan(model: &Galaxy) { pub fn short_range_scan(model: &Galaxy) {
let quadrant = &model.quadrants[model.enterprise.quadrant.as_index()]; let quadrant = &model.quadrants[model.enterprise.quadrant.as_index()];
@@ -39,11 +39,7 @@ pub fn move_enterprise(course: u8, warp_speed: f32, galaxy: &mut Galaxy) {
let end = find_end_quadrant_sector(galaxy.enterprise.quadrant, galaxy.enterprise.sector, course, warp_speed); let end = find_end_quadrant_sector(galaxy.enterprise.quadrant, galaxy.enterprise.sector, course, warp_speed);
if end.hit_edge { if end.hit_edge {
println!("Lt. Uhura report message from Starfleet Command: text_display::hit_edge(&end);
'Permission to attempt crossing of galactic perimeter
is hereby *Denied*. Shut down your engines.'
Chief Engineer Scott reports, 'Warp engines shut down
at sector {} of quadrant {}.'", end.quadrant, end.sector);
} }
galaxy.enterprise.quadrant = end.quadrant; galaxy.enterprise.quadrant = end.quadrant;
@@ -54,12 +50,6 @@ pub fn move_enterprise(course: u8, warp_speed: f32, galaxy: &mut Galaxy) {
short_range_scan(&galaxy) short_range_scan(&galaxy)
} }
struct EndPosition {
quadrant: Pos,
sector: Pos,
hit_edge: bool
}
fn find_end_quadrant_sector(start_quadrant: Pos, start_sector: Pos, course: u8, warp_speed: f32) -> EndPosition { fn find_end_quadrant_sector(start_quadrant: Pos, start_sector: Pos, course: u8, warp_speed: f32) -> EndPosition {
let (dx, dy): (i8, i8) = COURSES[(course - 1) as usize]; let (dx, dy): (i8, i8) = COURSES[(course - 1) as usize];

View File

@@ -6,6 +6,7 @@ use crate::model::Condition;
mod model; mod model;
mod commands; mod commands;
mod text_display;
fn main() { fn main() {
ctrlc::set_handler(move || { exit(0) }) ctrlc::set_handler(move || { exit(0) })
@@ -19,31 +20,27 @@ fn main() {
match prompt("Command?").to_uppercase().as_str() { match prompt("Command?").to_uppercase().as_str() {
"SRS" => commands::short_range_scan(&galaxy), "SRS" => commands::short_range_scan(&galaxy),
"NAV" => gather_dir_and_speed_then_move(&mut galaxy), "NAV" => gather_dir_and_speed_then_move(&mut galaxy),
_ => print_command_help() _ => text_display::print_command_help()
} }
if galaxy.enterprise.condition == Condition::Destroyed { // todo: also check if stranded if galaxy.enterprise.condition == Condition::Destroyed { // todo: also check if stranded
println!("Is is stardate {}. text_display::end_game_failure(&galaxy);
There were {} Klingon battle cruisers left at
the end of your mission.
", galaxy.stardate, galaxy.remaining_klingons());
break; break;
} }
} }
} }
fn gather_dir_and_speed_then_move(galaxy: &mut Galaxy) { fn gather_dir_and_speed_then_move(galaxy: &mut Galaxy) {
const BAD_NAV: &str = " Lt. Sulu reports, 'Incorrect course data, sir!'";
let course = prompt_value::<u8>("Course (1-9)?", 1, 9); let course = prompt_value::<u8>("Course (1-9)?", 1, 9);
if course.is_none() { if course.is_none() {
println!("{}", BAD_NAV); text_display::bad_nav();
return; return;
} }
let speed = prompt_value::<f32>("Warp Factor (0-8)?", 0.0, 8.0); let speed = prompt_value::<f32>("Warp Factor (0-8)?", 0.0, 8.0);
if speed.is_none() { if speed.is_none() {
println!("{}", BAD_NAV); text_display::bad_nav();
return; return;
} }
@@ -75,18 +72,3 @@ fn prompt_value<T: FromStr + PartialOrd>(prompt_text: &str, min: T, max: T) -> O
_ => None _ => None
} }
} }
fn print_command_help() {
println!("Enter one of the following:
NAV (To set course)
SRS (For short range sensor scan)
LRS (For long range sensor scan)
PHA (To fire phasers)
TOR (To fire photon torpedoes)
SHE (To raise or lower shields)
DAM (For damage control reports)
COM (To call on library-computer)
XXX (To resign your command)
")
}

View File

@@ -2,6 +2,8 @@ use std::{ops::{Mul, Add}, fmt::Display};
use rand::Rng; use rand::Rng;
use crate::text_display;
pub struct Galaxy { pub struct Galaxy {
pub stardate: f32, pub stardate: f32,
pub quadrants: Vec<Quadrant>, pub quadrants: Vec<Quadrant>,
@@ -46,12 +48,12 @@ impl Enterprise {
return; return;
} }
println!("{hit_strength} unit hit on Enterprise from sector {sector}"); text_display::enterprise_hit(&hit_strength, &sector);
// absorb into shields // absorb into shields
if self.shields <= 0 { if self.shields <= 0 {
println!("The Enterprise has been destroyed. The Federation will be conquered."); text_display::enterprise_destroyed();
self.condition = Condition::Destroyed; self.condition = Condition::Destroyed;
} }
@@ -66,6 +68,12 @@ pub enum Condition {
Destroyed, Destroyed,
} }
pub struct EndPosition {
pub quadrant: Pos,
pub sector: Pos,
pub hit_edge: bool
}
#[derive(PartialEq, Clone, Copy, Debug)] #[derive(PartialEq, Clone, Copy, Debug)]
pub struct Pos(pub u8, pub u8); pub struct Pos(pub u8, pub u8);

View File

@@ -0,0 +1,42 @@
use crate::model::{Galaxy, Pos, EndPosition};
pub fn print_command_help() {
println!("Enter one of the following:
NAV (To set course)
SRS (For short range sensor scan)
LRS (For long range sensor scan)
PHA (To fire phasers)
TOR (To fire photon torpedoes)
SHE (To raise or lower shields)
DAM (For damage control reports)
COM (To call on library-computer)
XXX (To resign your command)
")
}
pub fn end_game_failure(galaxy: &Galaxy) {
println!("Is is stardate {}.
There were {} Klingon battle cruisers left at
the end of your mission.
", galaxy.stardate, galaxy.remaining_klingons());
}
pub fn enterprise_destroyed() {
println!("The Enterprise has been destroyed. The Federation will be conquered.");
}
pub fn bad_nav() {
println!(" Lt. Sulu reports, 'Incorrect course data, sir!'")
}
pub fn enterprise_hit(hit_strength: &u16, from_sector: &Pos) {
println!("{hit_strength} unit hit on Enterprise from sector {from_sector}");
}
pub fn hit_edge(end: &EndPosition) {
println!("Lt. Uhura report message from Starfleet Command:
'Permission to attempt crossing of galactic perimeter
is hereby *Denied*. Shut down your engines.'
Chief Engineer Scott reports, 'Warp engines shut down
at sector {} of quadrant {}.'", end.quadrant, end.sector);
}