queen can now infinitely lay eggs
This commit is contained in:
@@ -6,5 +6,6 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
downcast-rs = "1.2.0"
|
||||||
ncurses = "5.101.0"
|
ncurses = "5.101.0"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
|
|||||||
+19
-9
@@ -1,4 +1,5 @@
|
|||||||
extern crate ncurses;
|
extern crate ncurses;
|
||||||
|
extern crate downcast_rs;
|
||||||
|
|
||||||
use ncurses::*;
|
use ncurses::*;
|
||||||
use rand::seq::SliceRandom;
|
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 {
|
struct Colony {
|
||||||
ants: HashMap<u32, Box<dyn Entity>>,
|
ants: HashMap<u32, Box<dyn Entity>>,
|
||||||
@@ -149,6 +153,7 @@ impl Queen {
|
|||||||
struct Egg {
|
struct Egg {
|
||||||
pos: Point,
|
pos: Point,
|
||||||
counter: u8,
|
counter: u8,
|
||||||
|
queen_id: u32
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Renderable for Egg {
|
impl Renderable for Egg {
|
||||||
@@ -158,10 +163,11 @@ impl Renderable for Egg {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Egg {
|
impl Egg {
|
||||||
fn new(x: i32, y: i32) -> Egg {
|
fn new(x: i32, y: i32, queen_id: u32) -> Egg {
|
||||||
Egg {
|
Egg {
|
||||||
pos: Point(x, y),
|
pos: Point(x, y),
|
||||||
counter: 10,
|
counter: 10,
|
||||||
|
queen_id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -172,7 +178,7 @@ impl AI for Egg {
|
|||||||
self.counter -= 1;
|
self.counter -= 1;
|
||||||
return BoardCommand::Noop;
|
return BoardCommand::Noop;
|
||||||
} else {
|
} else {
|
||||||
BoardCommand::Hatch(id)
|
BoardCommand::Hatch(id, self.queen_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,8 +189,8 @@ impl AI for Egg {
|
|||||||
|
|
||||||
enum BoardCommand {
|
enum BoardCommand {
|
||||||
Dig(Point),
|
Dig(Point),
|
||||||
LayEgg(Point),
|
LayEgg(Point, u32),
|
||||||
Hatch(u32),
|
Hatch(u32, u32),
|
||||||
Noop,
|
Noop,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,7 +240,7 @@ impl AI for Queen {
|
|||||||
// choose between laying an egg and moving
|
// choose between laying an egg and moving
|
||||||
if self.egg_count < 3 {
|
if self.egg_count < 3 {
|
||||||
self.egg_count += 1;
|
self.egg_count += 1;
|
||||||
return BoardCommand::LayEgg(pos);
|
return BoardCommand::LayEgg(pos, id);
|
||||||
} else {
|
} else {
|
||||||
self.pos = pos;
|
self.pos = pos;
|
||||||
return BoardCommand::Noop;
|
return BoardCommand::Noop;
|
||||||
@@ -283,12 +289,16 @@ fn simulate(c: &mut Colony, w: &mut World, b: &mut Board) {
|
|||||||
BoardCommand::Dig(pos) => {
|
BoardCommand::Dig(pos) => {
|
||||||
w.clear(pos);
|
w.clear(pos);
|
||||||
}
|
}
|
||||||
BoardCommand::LayEgg(pos) => {
|
BoardCommand::LayEgg(pos, id) => {
|
||||||
c.add_entity(Egg::new(pos.0, pos.1));
|
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 egg = c.ants.remove(&egg_id);
|
||||||
let pos = egg.unwrap().get_position();
|
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));
|
c.add_entity(Ant::new(pos.0, pos.1));
|
||||||
}
|
}
|
||||||
BoardCommand::Noop => {}
|
BoardCommand::Noop => {}
|
||||||
|
|||||||
Reference in New Issue
Block a user