fix foodgen to use correct dim, add limit on food

sideways
Rostyslav Hnatyshyn 9 months ago
parent b718397682
commit 1e3032c51b
  1. 14
      src/lib/entity.rs
  2. 2
      src/lib/screen.rs

@ -203,20 +203,21 @@ impl Renderable for Food {
// no position, does not get rendered yet acts per turn // no position, does not get rendered yet acts per turn
#[derive(Clone)] #[derive(Clone)]
pub struct FoodGenerator { pub struct FoodGenerator {
counter: u32, timer: u32,
pos: Point, pos: Point,
id: u32 id: u32,
counter: u32
} }
impl FoodGenerator { impl FoodGenerator {
pub fn new() -> FoodGenerator { pub fn new() -> FoodGenerator {
FoodGenerator { counter: 0, id: 0, pos: Point(0,0) } FoodGenerator { timer: 0, id: 0, pos: Point(0,0), counter: 0 }
} }
} }
impl AI for FoodGenerator { impl AI for FoodGenerator {
fn step(&mut self, b: &Screen, w: &mut World) -> BoardCommand { fn step(&mut self, b: &Screen, w: &mut World) -> BoardCommand {
if self.counter % 600 == 0 { if self.timer % 600 == 0 && self.counter < 10 {
// generate random coords, if valid, spawn // generate random coords, if valid, spawn
// if not, try again next step // if not, try again next step
let (min, max) = b.get_dimensions(); let (min, max) = b.get_dimensions();
@ -224,10 +225,11 @@ impl AI for FoodGenerator {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let r_x = rng.gen_range(min.0..max.0 + 1); let r_x = rng.gen_range(min.0..max.0 + 1);
let r_y = rng.gen_range(min.1..max.1 + 1); let r_y = rng.gen_range(min.1..max.1 + 1);
self.counter += 1;
return BoardCommand::SpawnFood(Point(r_x, r_y)); return BoardCommand::SpawnFood(Point(r_x, r_y));
} }
self.counter += 1; self.timer += 1;
BoardCommand::Noop BoardCommand::Noop
} }
} }

@ -49,7 +49,7 @@ impl Screen {
} }
pub fn get_dimensions(&self) -> (Point, Point) { pub fn get_dimensions(&self) -> (Point, Point) {
(Point(-self.max_x, -self.max_y), Point(self.max_x, self.max_y)) (Point(0,0), Point(self.max_x, self.max_y))
} }
} }

Loading…
Cancel
Save