A simple actor-based simulation of tag, implemented in rust.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.2 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. pub mod agent;
  2. pub mod view;
  3. pub mod world;
  4. use agent::{Agent, SimpleAgent};
  5. use std::io;
  6. use std::io::Read;
  7. use std::process::exit;
  8. use std::sync::{Arc, Mutex};
  9. use std::thread;
  10. use view::{RawTerminal, TerminalView, TermionBackend};
  11. use world::ActualWorld;
  12. pub fn get_world(width: isize, height: isize, spacing: usize, validating: bool) -> ActualWorld {
  13. let mut agents: Vec<(_, Box<dyn Agent>)> = vec![];
  14. for x in (0..width).step_by(spacing) {
  15. for y in (0..height).step_by(spacing) {
  16. agents.push(((x, y).into(), Box::new(SimpleAgent)));
  17. }
  18. }
  19. ActualWorld::new((width, height).into(), agents, validating)
  20. }
  21. pub type DefaultView = Arc<Mutex<Option<TerminalView<TermionBackend<RawTerminal<io::Stdout>>>>>>;
  22. pub fn get_view() -> DefaultView {
  23. let view = Arc::new(Mutex::new(Some(TerminalView::try_new().unwrap())));
  24. // Exit on q, ESC and Ctrl-C and reset terminal
  25. let view2 = view.clone();
  26. thread::spawn(move || {
  27. let stdin = io::stdin();
  28. for byte in stdin.bytes().flatten() {
  29. if byte == b'q' || byte == 0x1b || byte == 0x03 {
  30. if let Ok(mut view) = view2.lock() {
  31. *view = None; // drop view
  32. println!("\n");
  33. }
  34. exit(0);
  35. }
  36. }
  37. });
  38. view
  39. }