From 2ec11dfd6cefc6d34141bd379bfe9e231ec9d73a Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Fri, 23 Jul 2021 15:30:25 +0200 Subject: [PATCH] Extract draw_world --- src/lib.rs | 24 ++++++++++++++++++++++++ src/main.rs | 23 ++--------------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c9f7445..6e4f78e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub mod view; pub mod world; use agent::{Agent, SimpleAgent}; +use std::error::Error; use std::io; use std::io::Read; use std::process::exit; @@ -41,3 +42,26 @@ pub fn get_view() -> DefaultView { }); view } + +pub fn draw_world( + world: &ActualWorld, + gen: usize, + view: &DefaultView, +) -> Result> { + (*view.lock().unwrap()).as_mut().unwrap().draw( + gen, + world + .state + .agent_positions + .get(&world.state.tagged) + .map(|pos| (pos.x, pos.y)) + .unwrap(), + world + .state + .agent_positions + .iter() + .map(|(_id, pos)| (pos.x, pos.y)) + .collect::>() + .as_ref(), + ) +} diff --git a/src/main.rs b/src/main.rs index 84c9d82..777bc82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use gntag::{get_view, get_world, DefaultView}; +use gntag::{draw_world, get_view, get_world, DefaultView}; fn main() { let view = get_view(); @@ -18,26 +18,7 @@ fn run_simulation(view: &DefaultView) { let mut gen = 0; loop { - let resized = (*view.lock().unwrap()) - .as_mut() - .unwrap() - .draw( - gen, - world - .state - .agent_positions - .get(&world.state.tagged) - .map(|pos| (pos.x, pos.y)) - .unwrap(), - world - .state - .agent_positions - .iter() - .map(|(_id, pos)| (pos.x, pos.y)) - .collect::>() - .as_ref(), - ) - .unwrap(); + let resized = draw_world(&world, gen, view).unwrap(); if resized { return; };