moved simulate and render to world

master
Rostyslav Hnatyshyn 9 months ago
parent 6d577223d0
commit cb020f73d4
  1. 49
      src/lib/world.rs
  2. 48
      src/main.rs

@ -1,5 +1,6 @@
use crate::Screen; use crate::{Screen, BoardCommand};
use crate::Point; use crate::Point;
use crate::{Entities, Queen};
use std::collections::HashSet; use std::collections::HashSet;
#[derive(Clone)] #[derive(Clone)]
@ -29,3 +30,49 @@ impl World {
.collect() .collect()
} }
} }
pub fn render(e: &Entities, w: &World, b: &Screen) {
for c in w.cleared.iter() {
b.render(c, "x");
}
for a in e.data.values() {
a.render(b);
}
}
pub fn simulate(e: &mut Entities, w: &mut World, b: &mut Screen) {
let cmds: Vec<BoardCommand> = e
.data
.values_mut()
.map(|a| a.step(b, &w))
.collect();
for cmd in cmds {
match cmd {
BoardCommand::Move(old_pos, pos) => {
// still makes no difference
// occupied needs to be updated as soon as the move is made...
w.occupied.remove(&old_pos);
w.occupied.insert(pos);
}
BoardCommand::Dig(pos) => {
w.clear(pos);
}
BoardCommand::LayEgg(pos, id) => {
e.add_egg(pos.0, pos.1, id);
}
BoardCommand::Hatch(egg_id, queen_id) => {
let egg = e.data.remove(&egg_id);
let pos = egg.unwrap().get_position();
let q = e.data.get_mut(&queen_id).unwrap();
let queen: &mut Queen = q.downcast_mut::<Queen>().unwrap();
queen.egg_count -= 1;
e.add_ant(pos.0, pos.1);
}
BoardCommand::Noop => {}
}
}
}

@ -16,55 +16,9 @@ mod lib {
use lib::point::Point; use lib::point::Point;
use lib::screen::{init_screen, BoardCommand, Screen}; use lib::screen::{init_screen, BoardCommand, Screen};
use lib::world::World; use lib::world::{World, simulate, render};
use lib::entity::{Queen, Entities}; use lib::entity::{Queen, Entities};
fn render(e: &Entities, w: &World, b: &Screen) {
for c in w.cleared.iter() {
b.render(c, "x");
}
for a in e.data.values() {
a.render(b);
}
}
fn simulate(e: &mut Entities, w: &mut World, b: &mut Screen) {
let cmds: Vec<BoardCommand> = e
.data
.values_mut()
.map(|a| a.step(b, &w))
.collect();
for cmd in cmds {
match cmd {
BoardCommand::Move(old_pos, pos) => {
// still makes no difference
// occupied needs to be updated as soon as the move is made...
w.occupied.remove(&old_pos);
w.occupied.insert(pos);
}
BoardCommand::Dig(pos) => {
w.clear(pos);
}
BoardCommand::LayEgg(pos, id) => {
e.add_egg(pos.0, pos.1, id);
}
BoardCommand::Hatch(egg_id, queen_id) => {
let egg = e.data.remove(&egg_id);
let pos = egg.unwrap().get_position();
let q = e.data.get_mut(&queen_id).unwrap();
let queen: &mut Queen = q.downcast_mut::<Queen>().unwrap();
queen.egg_count -= 1;
e.add_ant(pos.0, pos.1);
}
BoardCommand::Noop => {}
}
}
}
fn main() { fn main() {
let mut board = init_screen(); let mut board = init_screen();
let mut world = World::new(); let mut world = World::new();

Loading…
Cancel
Save