renamed text_display to view, and moved srs into it

This commit is contained in:
Christopher
2023-03-01 19:07:46 +13:00
parent ec3b0697bb
commit efba942396
5 changed files with 89 additions and 89 deletions

View File

@@ -1,45 +1,11 @@
use crate::{model::{Galaxy, Pos, SectorStatus, COURSES, Quadrant, EndPosition}, text_display};
pub fn short_range_scan(model: &Galaxy) {
let quadrant = &model.quadrants[model.enterprise.quadrant.as_index()];
let data : [String; 8] = [
format!("Stardate {}", model.stardate),
format!("Condition {:?}", model.enterprise.condition),
format!("Quadrant {}", model.enterprise.quadrant),
format!("Sector {}", model.enterprise.sector),
format!("Photon torpedoes {}", model.enterprise.photon_torpedoes),
format!("Total energy {}", model.enterprise.total_energy),
format!("Shields {}", model.enterprise.shields),
format!("Klingons remaining {}", model.remaining_klingons()),
];
println!("{:-^33}", "");
for y in 0..=7 {
for x in 0..=7 {
let pos = Pos(x, y);
if &pos == &model.enterprise.sector {
print!("<*> ")
} else {
match quadrant.sector_status(&pos) {
SectorStatus::Star => print!(" * "),
SectorStatus::StarBase => print!(">!< "),
SectorStatus::Klingon => print!("+K+ "),
_ => print!(" "),
}
}
}
println!("{:>9}{}", "", data[y as usize])
}
println!("{:-^33}", "");
}
use crate::{model::{Galaxy, Pos, COURSES, EndPosition}, view};
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);
if end.hit_edge {
text_display::hit_edge(&end);
view::hit_edge(&end);
}
galaxy.enterprise.quadrant = end.quadrant;
@@ -47,7 +13,7 @@ pub fn move_enterprise(course: u8, warp_speed: f32, galaxy: &mut Galaxy) {
// if new_quadrant isnt old quadrant print intro
short_range_scan(&galaxy)
view::short_range_scan(&galaxy)
}
fn find_end_quadrant_sector(start_quadrant: Pos, start_sector: Pos, course: u8, warp_speed: f32) -> EndPosition {

View File

@@ -6,7 +6,7 @@ use crate::model::Condition;
mod model;
mod commands;
mod text_display;
mod view;
fn main() {
ctrlc::set_handler(move || { exit(0) })
@@ -14,17 +14,17 @@ fn main() {
let mut galaxy = Galaxy::generate_new();
// todo: init options, starting state and notes
commands::short_range_scan(&galaxy);
view::short_range_scan(&galaxy);
loop {
match prompt("Command?").to_uppercase().as_str() {
"SRS" => commands::short_range_scan(&galaxy),
"SRS" => view::short_range_scan(&galaxy),
"NAV" => gather_dir_and_speed_then_move(&mut galaxy),
_ => text_display::print_command_help()
_ => view::print_command_help()
}
if galaxy.enterprise.condition == Condition::Destroyed { // todo: also check if stranded
text_display::end_game_failure(&galaxy);
view::end_game_failure(&galaxy);
break;
}
}
@@ -34,13 +34,13 @@ fn gather_dir_and_speed_then_move(galaxy: &mut Galaxy) {
let course = prompt_value::<u8>("Course (1-9)?", 1, 9);
if course.is_none() {
text_display::bad_nav();
view::bad_nav();
return;
}
let speed = prompt_value::<f32>("Warp Factor (0-8)?", 0.0, 8.0);
if speed.is_none() {
text_display::bad_nav();
view::bad_nav();
return;
}

View File

@@ -2,7 +2,7 @@ use std::{ops::{Mul, Add}, fmt::Display};
use rand::Rng;
use crate::text_display;
use crate::view;
pub struct Galaxy {
pub stardate: f32,
@@ -48,12 +48,12 @@ impl Enterprise {
return;
}
text_display::enterprise_hit(&hit_strength, &sector);
view::enterprise_hit(&hit_strength, &sector);
// absorb into shields
if self.shields <= 0 {
text_display::enterprise_destroyed();
view::enterprise_destroyed();
self.condition = Condition::Destroyed;
}

View File

@@ -1,42 +0,0 @@
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);
}

View File

@@ -0,0 +1,76 @@
use crate::model::{Galaxy, Pos, EndPosition, SectorStatus};
pub fn short_range_scan(model: &Galaxy) {
let quadrant = &model.quadrants[model.enterprise.quadrant.as_index()];
let data : [String; 8] = [
format!("Stardate {}", model.stardate),
format!("Condition {:?}", model.enterprise.condition),
format!("Quadrant {}", model.enterprise.quadrant),
format!("Sector {}", model.enterprise.sector),
format!("Photon torpedoes {}", model.enterprise.photon_torpedoes),
format!("Total energy {}", model.enterprise.total_energy),
format!("Shields {}", model.enterprise.shields),
format!("Klingons remaining {}", model.remaining_klingons()),
];
println!("{:-^33}", "");
for y in 0..=7 {
for x in 0..=7 {
let pos = Pos(x, y);
if &pos == &model.enterprise.sector {
print!("<*> ")
} else {
match quadrant.sector_status(&pos) {
SectorStatus::Star => print!(" * "),
SectorStatus::StarBase => print!(">!< "),
SectorStatus::Klingon => print!("+K+ "),
_ => print!(" "),
}
}
}
println!("{:>9}{}", "", data[y as usize])
}
println!("{:-^33}", "");
}
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);
}