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.

62 lines
1.6 KiB

3 years ago
  1. use crate::agent::{Agent, AgentId, Direction, Move, Position, WorldView};
  2. use std::collections::HashMap;
  3. pub struct ActualWorld {
  4. size: Position,
  5. pub agent_positions: HashMap<AgentId, Position>,
  6. pub agents: HashMap<AgentId, Box<dyn Agent>>,
  7. pub tagged: AgentId,
  8. pub tagged_by: Option<AgentId>,
  9. }
  10. impl ActualWorld {
  11. pub fn do_step(&mut self) {
  12. let moves: Vec<_> = self
  13. .agents
  14. .iter()
  15. .map(|(id, agent)| {
  16. let pos = self.agent_positions.get(id).unwrap();
  17. (
  18. *id,
  19. agent.next_move(WorldView {
  20. self_tagged: *id == self.tagged,
  21. bounds_distance: (pos.y, self.size.x - pos.x, self.size.y - pos.y, pos.x),
  22. other_agents: self
  23. .agent_positions
  24. .iter()
  25. .map(|(id, other_pos)| {
  26. (
  27. Direction {
  28. x: other_pos.x - pos.x,
  29. y: other_pos.y - pos.y,
  30. },
  31. *id,
  32. )
  33. })
  34. .collect(),
  35. }),
  36. )
  37. })
  38. .collect();
  39. for (id, mv) in moves {
  40. match mv {
  41. Move::Noop => {}
  42. Move::TryTag(other_id) => {
  43. // FIXME check distance
  44. // FIXME check tagged_by
  45. self.agents.get_mut(&other_id).unwrap().get_tagged(other_id);
  46. // FIXME update tagged
  47. // FIXME update tagged_by
  48. }
  49. Move::TryMove(dir) => {
  50. // FIXME check out of bounds
  51. // FIXME matches!
  52. self.agent_positions.entry(id).and_modify(|e| {
  53. e.x += dir.x;
  54. e.y += dir.y;
  55. });
  56. }
  57. }
  58. }
  59. }
  60. }