From bdcebeafa85c173ec70650b6a46cc85fba962e48 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Wed, 4 Jan 2023 14:54:57 +0100 Subject: [PATCH] cleanup --- src/app.rs | 3 +-- src/{person.rs => creature.rs} | 16 ++++++---------- src/events/person_genesis.rs | 2 +- src/game.rs | 3 +-- src/main.rs | 33 +++------------------------------ src/state.rs | 6 +----- src/world.rs | 1 - 7 files changed, 13 insertions(+), 51 deletions(-) rename src/{person.rs => creature.rs} (83%) diff --git a/src/app.rs b/src/app.rs index 165e657..3669aec 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,8 +1,7 @@ -use std::any::Any; use crossterm::event::{read, Event, KeyCode}; use tui::{backend::Backend, Frame, layout::Layout}; -use crate::{game::Game, person::Creature, entity::{Entity, EntityId}}; +use crate::{game::Game, entity::EntityId}; /** * |........................| diff --git a/src/person.rs b/src/creature.rs similarity index 83% rename from src/person.rs rename to src/creature.rs index 86f4ad9..a9b97a6 100644 --- a/src/person.rs +++ b/src/creature.rs @@ -1,13 +1,11 @@ use std::{rc::Rc, fmt::Display}; use rand::prelude::*; -use crate::{time::Time, world::{Town, World}, generators::PersonNameGenerator, state::{GameState, Action}, entity::{Entity, Location}}; +use crate::{time::Time, world::{World}, generators::PersonNameGenerator, state::{GameState, Action}, entity::{Entity, Location, EntityId}}; #[derive(Clone, Copy)] pub enum Profession { Peasant, - Adventurer, - Blacksmith, } #[derive(Clone, Copy)] @@ -21,18 +19,16 @@ pub struct Creature { pub entity: Entity, pub name: String, pub birth_date: Time, - pub birth_location: Rc, pub profession: Profession, pub agenda: Agenda, } impl Creature { - pub fn new(birth_date: Time, birth_location: Rc, location: Location) -> Creature { + pub fn new(birth_date: Time, location: Location) -> Creature { Creature { entity: Entity { id: 0, loc: location }, name: PersonNameGenerator::name(), birth_date: birth_date, - birth_location: birth_location, profession: Profession::Peasant, agenda: Agenda::Idle(0), } @@ -102,19 +98,19 @@ impl Display for Creature { #[cfg(test)] mod tests { - use crate::world::{World, Structure}; + use crate::world::{World, Structure, Town}; use super::*; #[test] fn test_person_creation() { - let person = Creature::new(Time { time: 0 }, Rc::new(Town::new()), Location{x: 0, y: 0}); + let person = Creature::new(Time { time: 0 }, Location{x: 0, y: 0}); assert_ne!(person.name, ""); } #[test] fn test_traveling() { - let mut person = Creature::new(Time { time: 0 }, Rc::new(Town::new()), Location{x: 0, y: 0}); + let mut person = Creature::new(Time { time: 0 }, Location{x: 0, y: 0}); person.agenda = Agenda::Traveling(Location{x: 10, y: 0}); person.step(&World::new(32)); assert_eq!(person.entity.loc, Location{x: 1, y: 0}); @@ -122,7 +118,7 @@ mod tests { #[test] fn test_start_traveling() { - let mut person = Creature::new(Time { time: 0 }, Rc::new(Town::new()), Location{x: 0, y: 0}); + let mut person = Creature::new(Time { time: 0 }, Location{x: 0, y: 0}); person.agenda = Agenda::Idle(0); let mut world = World::new(32); world.add_structure(Location{x: 10, y: 10}, Structure::Town(Rc::new(Town::new()))); diff --git a/src/events/person_genesis.rs b/src/events/person_genesis.rs index d58fc4e..90a5d8c 100644 --- a/src/events/person_genesis.rs +++ b/src/events/person_genesis.rs @@ -1,6 +1,6 @@ use std::rc::Rc; -use crate::{state::{GameState, Action}, world::Town, person::Creature}; +use crate::{state::{GameState, Action}, world::Town, creature::Creature}; pub struct PersonGenesis { pub town: Rc, diff --git a/src/game.rs b/src/game.rs index 46a2d2d..2377edc 100644 --- a/src/game.rs +++ b/src/game.rs @@ -50,7 +50,7 @@ mod tests { use std::rc::Rc; - use crate::{world::{World, Terrain, Town}, person::{Creature, Agenda}, time::Time, entity::Location}; + use crate::{world::{World, Terrain, Town}, creature::{Creature, Agenda}, time::Time, entity::Location}; use super::*; @@ -72,7 +72,6 @@ mod tests { let mut creature = Creature::new( Time { time: 0 }, - Rc::new(Town::new()), Location { x: 0, y: 0 } ); creature.set_agenda(Agenda::Traveling(Location { x: 2, y: 2 })); diff --git a/src/main.rs b/src/main.rs index 3a8aa53..140da03 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ mod state; mod events; mod time; mod generators; -mod person; +mod creature; mod app; mod entity; mod game; @@ -13,21 +13,15 @@ use noise::{Perlin, ScalePoint, Add, NoiseFn, ScaleBias}; use noise::utils::{NoiseMapBuilder, PlaneMapBuilder}; use image::{RgbImage, Rgb}; use rand::prelude::*; -use tui::Frame; -use tui::backend::Backend; -use tui::style::{Style, Color, Modifier}; -use tui::widgets::List; use world::World; -use std::{io, thread, time::Duration}; +use std::{io}; use tui::{ backend::CrosstermBackend, - widgets::{Widget, Block, Borders}, - layout::{Layout, Constraint, Direction}, Terminal }; use crossterm::{ - event::{read, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, + event::{DisableMouseCapture, EnableMouseCapture}, execute, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; @@ -110,22 +104,6 @@ fn build_world() -> World { world } -fn draw_state(f: &mut Frame, state: &state::GameState) { - let mut items: Vec = vec![]; - - for e in state.events.iter() { - items.push(tui::widgets::ListItem::new(format!("{}", e.description()))); - } - - let main_window = List::new(items) - .block(Block::default().title("List").borders(Borders::ALL)) - .style(Style::default().fg(Color::White)) - .highlight_style(Style::default().add_modifier(Modifier::ITALIC)) - .highlight_symbol(">>"); - - f.render_widget(main_window, f.size()); -} - fn main() -> Result<(), io::Error> { // build game state let mut world_state = state::GameState::new(build_world()); @@ -166,9 +144,4 @@ fn main() -> Result<(), io::Error> { Ok(()) - // for event in &state.events { - // println!("{}", event.description()); - // } - - } \ No newline at end of file diff --git a/src/state.rs b/src/state.rs index f6bfcb0..1f2e8b8 100644 --- a/src/state.rs +++ b/src/state.rs @@ -4,7 +4,7 @@ use std::rc::Rc; use rand::prelude::*; use crate::entity::{Location, EntityId}; -use crate::person::Creature; +use crate::creature::Creature; use crate::time::Time; use crate::world::{World, Terrain, Town, Tavern}; use crate::events::{FoundTown, WorldGenesis, PersonGenesis, TavernBuilt}; @@ -120,7 +120,6 @@ impl GameState { town: town.clone(), person: Creature::new( self.time.substract_years(rng.gen_range(18..30)), - town.clone(), self.world.get_town_location(&town) ), }) @@ -161,9 +160,6 @@ impl GameState { #[cfg(test)] mod tests { - - use crate::person::Agenda; - use super::*; #[test] diff --git a/src/world.rs b/src/world.rs index 6425449..8434ac3 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,5 +1,4 @@ use std::{rc::Rc, collections::HashMap, fmt}; -use rand::prelude::*; use crate::{generators::TownNameGenerator, entity::Location};