refactoring 1

This commit is contained in:
Niko Abeler 2023-01-01 21:11:27 +01:00
parent 68c12c7f5c
commit 26700b9277
4 changed files with 28 additions and 62 deletions

View File

@ -1,7 +1,7 @@
use crossterm::event::{read, Event, KeyCode};
use tui::{backend::Backend, Frame, layout::Layout};
use crate::{state::GameState, person::Person, entity::Entity};
use crate::{game::Game, person::Person, entity::Entity};
/**
* |........................|
@ -19,7 +19,7 @@ enum AppStatus {
}
pub struct App<'a> {
state: GameState,
game: Game,
guest_list: Vec<tui::widgets::ListItem<'a>>,
guest_list_state: tui::widgets::ListState,
status: AppStatus,
@ -27,9 +27,9 @@ pub struct App<'a> {
}
impl<'a> App<'a> {
pub fn new(state: GameState) -> App<'a> {
pub fn new(state: Game) -> App<'a> {
App {
state: state,
game: state,
guest_list: vec![],
guest_list_state: tui::widgets::ListState::default(),
status: AppStatus::Initial,
@ -41,7 +41,7 @@ impl<'a> App<'a> {
match &self.status {
AppStatus::Initial => {
// determine guests
self.guest_list = self.state.guests().iter().map(|guest| {
self.guest_list = self.game.state.guests().iter().map(|guest| {
tui::widgets::ListItem::new(guest.name.clone())
}).collect();
self.guest_list_state = tui::widgets::ListState::default();
@ -67,13 +67,13 @@ impl<'a> App<'a> {
},
KeyCode::Enter => {
let selected = self.guest_list_state.selected().unwrap();
let guest = &self.state.guests()[selected];
let guest = &self.game.state.guests()[selected];
self.conversation = Vec::new();
self.greet_guest(guest);
self.status = AppStatus::TalkToGuest(guest.id());
},
KeyCode::Char('.') => {
self.state.step();
self.game.step();
self.status = AppStatus::Initial;
},
KeyCode::Esc => {
@ -166,7 +166,7 @@ impl<'a> App<'a> {
f.render_widget(controls, chunks[1]);
}
fn draw_talk_to_guest<B: Backend>(&self, f: &mut Frame<B>, guest: Option<u32>) {
let guest = self.state.get_person(guest.unwrap()).unwrap();
let guest = self.game.state.get_person(guest.unwrap()).unwrap();
let key_style = tui::style::Style::default()
.add_modifier(tui::style::Modifier::BOLD)
@ -205,7 +205,7 @@ impl<'a> App<'a> {
}
fn ask_business(&mut self, guest: Option<u32>) {
let guest = self.state.get_person(guest.unwrap()).unwrap();
let guest = self.game.state.get_person(guest.unwrap()).unwrap();
self.conversation.push(
tui::text::Spans::from(vec![
tui::text::Span::styled("What's your business?\n", tui::style::Style::default().fg(tui::style::Color::Yellow))
@ -213,7 +213,7 @@ impl<'a> App<'a> {
);
self.conversation.push(
tui::text::Spans::from(vec![
tui::text::Span::raw(guest.say_agenda(& self.state)),
tui::text::Span::raw(guest.say_agenda(& self.game.state)),
tui::text::Span::raw("\n"),
])
);

View File

@ -6,6 +6,7 @@ mod generators;
mod person;
mod app;
mod entity;
mod game;
use app::App;
use noise::{Perlin, ScalePoint, Add, NoiseFn, ScaleBias};
@ -126,17 +127,17 @@ fn draw_state<B: Backend>(f: &mut Frame<B>, state: &state::GameState) {
}
fn main() -> Result<(), io::Error> {
// build game state
let mut world_state = state::GameState::new(build_world());
world_state.step_n(36000);
world_state.time = world_state.time.add_years(100);
let mut rng = rand::thread_rng();
for _ in 0..N_TOWNS {
world_state.step_n(rng.gen_range(0..360*3));
world_state.time = world_state.time.add_days(rng.gen_range(0..360*3));
world_state.found_town();
}
world_state.build_tavern();
// insert state into game
let game = game::Game::new(world_state);
// setup terminal
@ -146,7 +147,7 @@ fn main() -> Result<(), io::Error> {
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
let mut app = App::new(world_state);
let mut app = App::new(game);
loop {
terminal.draw(|f| app.draw(f))?;
if !app.step() {

View File

@ -59,28 +59,6 @@ impl GameState {
self.events.push(Box::new(event));
}
pub fn step(&mut self) {
let ids: Vec<u32> = self.people.keys().map(|id| *id).collect();
for id in ids {
let mut person = self.people.get_mut(&id).unwrap().clone();
person.step(self);
self.people.insert(id, person);
}
self.time.time += 1;
}
pub fn step_n(&mut self, n: u32) {
for _ in 0..n {
self.step();
}
}
pub fn step_year(&mut self) {
for _ in 0..360 {
self.step();
}
}
/**
* Getters
@ -180,32 +158,11 @@ mod tests {
use super::*;
#[test]
fn test_step() {
let mut state = GameState::new(World::new(100));
state.step();
assert_eq!(state.time.time, 1);
}
#[test]
fn test_step_n() {
let mut state = GameState::new(World::new(100));
state.step_n(10);
assert_eq!(state.time.time, 10);
}
#[test]
fn test_step_year() {
let mut state = GameState::new(World::new(100));
state.step_year();
assert_eq!(state.time.time, 360);
}
#[test]
fn test_found_town() {
let mut state = GameState::new(World::new(1));
state.world.map[0][0].terrain = Terrain::Flats;
state.time = Time { time: 1e6 as u32 };
state.time = Time { time: 1e6 as i32 };
state.found_town();
assert_ne!(state.people.len(), 0);
}

View File

@ -2,7 +2,7 @@ use std::fmt;
#[derive(Clone, Copy)]
pub struct Time{
pub time: u32,
pub time: i32,
}
impl fmt::Display for Time {
@ -16,8 +16,16 @@ impl fmt::Display for Time {
}
impl Time {
pub fn substract_years(&self, years: u32) -> Time {
pub fn substract_years(&self, years: i32) -> Time {
Time { time: self.time - years * 360 }
}
pub(crate) fn add_days(&self, gen_range: i32) -> Time {
Time { time: self.time + gen_range as i32 }
}
pub(crate) fn add_years(&self, arg: i32) -> Time {
Time { time: self.time + arg as i32 * 360 }
}
}