moved nav function bulk to commands module

This commit is contained in:
Christopher
2023-03-01 09:16:00 +13:00
parent 80ac05e005
commit ab26776d61
3 changed files with 53 additions and 54 deletions

View File

@@ -1,4 +1,4 @@
use crate::model::{Galaxy, Pos, SectorStatus};
use crate::model::{Galaxy, Pos, SectorStatus, COURSES};
pub fn short_range_scan(model: &Galaxy) {
let quadrant = &model.quadrants[model.enterprise.sector.as_index()];
@@ -22,3 +22,49 @@ pub fn short_range_scan(model: &Galaxy) {
}
println!("{:-^33}", "");
}
pub fn move_enterprise(course: u8, warp_speed: f32, galaxy: &mut Galaxy) {
let distance = (warp_speed * 8.0) as i8;
let galaxy_pos = galaxy.enterprise.quadrant * 8u8 + galaxy.enterprise.sector;
let (dx, dy): (i8, i8) = COURSES[(course - 1) as usize];
let mut nx = (galaxy_pos.0 as i8) + dx * distance;
let mut ny = (galaxy_pos.1 as i8) + dy * distance;
let mut hit_edge = false;
if nx < 0 {
nx = 0;
hit_edge = true;
}
if ny < 0 {
ny = 0;
hit_edge = true;
}
if nx >= 64 {
ny = 63;
hit_edge = true;
}
if nx >= 64 {
ny = 63;
hit_edge = true;
}
let new_quadrant = Pos((nx / 8) as u8, (ny / 8) as u8);
let new_sector = Pos((nx % 8) as u8, (ny % 8) as u8);
if hit_edge {
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 {}.'", new_quadrant, new_sector);
}
galaxy.enterprise.quadrant = new_quadrant;
galaxy.enterprise.sector = new_sector;
// if new_quadrant isnt old quadrant print intro
short_range_scan(&galaxy)
}

View File

@@ -1,8 +1,6 @@
use std::{io::{stdin, stdout, Write}, process::exit, str::FromStr};
use model::{Galaxy, Pos};
use crate::model::DIRECTIONS;
use model::Galaxy;
mod model;
mod commands;
@@ -12,7 +10,7 @@ fn main() {
.expect("Error setting Ctrl-C handler");
let mut galaxy = Galaxy::generate_new();
// init ops, starting state and notes
// init options, starting state and notes
commands::short_range_scan(&galaxy);
loop {
@@ -21,18 +19,14 @@ fn main() {
"NAV" => gather_dir_and_speed_then_move(&mut galaxy),
_ => print_command_help()
}
// process the next command, based on it render something or update the galaxy or whatever
// this would be: read command, and based on it run dedicated function
// the function might get passed a mutable reference to the galaxy
}
}
fn gather_dir_and_speed_then_move(galaxy: &mut Galaxy) {
const BAD_NAV: &str = " Lt. Sulu reports, 'Incorrect course data, sir!'";
let dir = prompt_value::<u8>("Course (1-9)?", 1, 9);
if dir.is_none() {
let course = prompt_value::<u8>("Course (1-9)?", 1, 9);
if course.is_none() {
println!("{}", BAD_NAV);
return;
}
@@ -43,48 +37,7 @@ fn gather_dir_and_speed_then_move(galaxy: &mut Galaxy) {
return;
}
let distance = (speed.unwrap() * 8.0) as i8;
let galaxy_pos = galaxy.enterprise.quadrant * 8u8 + galaxy.enterprise.sector;
let (dx, dy): (i8, i8) = DIRECTIONS[(dir.unwrap() - 1) as usize];
let mut nx = (galaxy_pos.0 as i8) + dx * distance;
let mut ny = (galaxy_pos.1 as i8) + dy * distance;
let mut hit_edge = false;
if nx < 0 {
nx = 0;
hit_edge = true;
}
if ny < 0 {
ny = 0;
hit_edge = true;
}
if nx >= 64 {
ny = 63;
hit_edge = true;
}
if nx >= 64 {
ny = 63;
hit_edge = true;
}
let new_quadrant = Pos((nx / 8) as u8, (ny / 8) as u8);
let new_sector = Pos((nx % 8) as u8, (ny % 8) as u8);
if hit_edge {
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 {}.'", new_quadrant, new_sector);
}
galaxy.enterprise.quadrant = new_quadrant;
galaxy.enterprise.sector = new_sector;
// if new_quadrant isnt old quadrant print intro
commands::short_range_scan(&galaxy)
commands::move_enterprise(course.unwrap(), speed.unwrap(), galaxy);
}
fn prompt(prompt_text: &str) -> String {

View File

@@ -38,7 +38,7 @@ impl Display for Pos {
}
}
pub const DIRECTIONS : [(i8, i8); 8] = [
pub const COURSES : [(i8, i8); 8] = [
(1, 0),
(1, -1),
(0, -1),