queen can now infinitely lay eggs
This commit is contained in:
+19
-9
@@ -1,4 +1,5 @@
|
||||
extern crate ncurses;
|
||||
extern crate downcast_rs;
|
||||
|
||||
use ncurses::*;
|
||||
use rand::seq::SliceRandom;
|
||||
@@ -75,7 +76,10 @@ impl World {
|
||||
}
|
||||
}
|
||||
|
||||
trait Entity: AI + Renderable {}
|
||||
use downcast_rs::{Downcast, impl_downcast};
|
||||
|
||||
trait Entity: AI + Renderable + Downcast {}
|
||||
impl_downcast!(Entity);
|
||||
|
||||
struct Colony {
|
||||
ants: HashMap<u32, Box<dyn Entity>>,
|
||||
@@ -149,6 +153,7 @@ impl Queen {
|
||||
struct Egg {
|
||||
pos: Point,
|
||||
counter: u8,
|
||||
queen_id: u32
|
||||
}
|
||||
|
||||
impl Renderable for Egg {
|
||||
@@ -158,10 +163,11 @@ impl Renderable for Egg {
|
||||
}
|
||||
|
||||
impl Egg {
|
||||
fn new(x: i32, y: i32) -> Egg {
|
||||
fn new(x: i32, y: i32, queen_id: u32) -> Egg {
|
||||
Egg {
|
||||
pos: Point(x, y),
|
||||
counter: 10,
|
||||
queen_id
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -172,7 +178,7 @@ impl AI for Egg {
|
||||
self.counter -= 1;
|
||||
return BoardCommand::Noop;
|
||||
} else {
|
||||
BoardCommand::Hatch(id)
|
||||
BoardCommand::Hatch(id, self.queen_id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,8 +189,8 @@ impl AI for Egg {
|
||||
|
||||
enum BoardCommand {
|
||||
Dig(Point),
|
||||
LayEgg(Point),
|
||||
Hatch(u32),
|
||||
LayEgg(Point, u32),
|
||||
Hatch(u32, u32),
|
||||
Noop,
|
||||
}
|
||||
|
||||
@@ -234,7 +240,7 @@ impl AI for Queen {
|
||||
// choose between laying an egg and moving
|
||||
if self.egg_count < 3 {
|
||||
self.egg_count += 1;
|
||||
return BoardCommand::LayEgg(pos);
|
||||
return BoardCommand::LayEgg(pos, id);
|
||||
} else {
|
||||
self.pos = pos;
|
||||
return BoardCommand::Noop;
|
||||
@@ -283,12 +289,16 @@ fn simulate(c: &mut Colony, w: &mut World, b: &mut Board) {
|
||||
BoardCommand::Dig(pos) => {
|
||||
w.clear(pos);
|
||||
}
|
||||
BoardCommand::LayEgg(pos) => {
|
||||
c.add_entity(Egg::new(pos.0, pos.1));
|
||||
BoardCommand::LayEgg(pos, id) => {
|
||||
c.add_entity(Egg::new(pos.0, pos.1, id));
|
||||
}
|
||||
BoardCommand::Hatch(egg_id) => {
|
||||
BoardCommand::Hatch(egg_id, queen_id) => {
|
||||
let egg = c.ants.remove(&egg_id);
|
||||
let pos = egg.unwrap().get_position();
|
||||
|
||||
let q = c.ants.get_mut(&queen_id).unwrap();
|
||||
let queen: &mut Queen = q.downcast_mut::<Queen>().unwrap();
|
||||
queen.egg_count -= 1;
|
||||
c.add_entity(Ant::new(pos.0, pos.1));
|
||||
}
|
||||
BoardCommand::Noop => {}
|
||||
|
||||
Reference in New Issue
Block a user