added before_render and after_render to renderable trait

master
Rostyslav Hnatyshyn 9 months ago
parent 4f467758d8
commit 213407d89a
  1. 47
      src/lib/entity.rs
  2. 6
      src/lib/screen.rs
  3. 5
      src/main.rs

@ -1,7 +1,9 @@
use crate::lib::ai::AI;
use crate::Point;
use crate::Screen;
use std::collections::HashMap;
use crate::lib::ai::AI;
use ncurses::*;
use downcast_rs::{impl_downcast, Downcast};
@ -25,9 +27,16 @@ impl Ant {
impl Entity for Ant {}
impl Renderable for Ant {
fn render(&self) -> &str {
fn representation(&self) -> &str {
"o"
}
fn before_render(&self) {
attron(A_BOLD());
}
fn after_render(&self) {
attroff(A_BOLD());
}
}
pub struct Queen {
@ -37,14 +46,27 @@ pub struct Queen {
}
impl Renderable for Queen {
fn render(&self) -> &str {
fn representation(&self) -> &str {
"q"
}
fn before_render(&self) {
attron(A_BOLD());
}
fn after_render(&self) {
attroff(A_BOLD());
}
}
pub trait Renderable {
fn render(&self) -> &str {
"z"
pub trait Renderable: AI {
fn representation(&self) -> &str;
fn before_render(&self) {}
fn after_render(&self) {}
fn render(&self, b: &Screen) {
self.before_render();
b.render(&self.get_position(), self.representation());
self.after_render();
}
}
@ -67,9 +89,17 @@ pub struct Egg {
}
impl Renderable for Egg {
fn render(&self) -> &str {
fn representation(&self) -> &str {
"e"
}
fn before_render(&self) {
attron(A_BOLD());
}
fn after_render(&self) {
attroff(A_BOLD());
}
}
impl Egg {
@ -123,4 +153,3 @@ impl Entities {
id
}
}

@ -6,6 +6,8 @@ pub struct Screen {
max_y: i32,
}
use ncurses::*;
impl Screen {
pub fn new(max_x: i32, max_y: i32) -> Screen {
Screen {
@ -27,6 +29,10 @@ impl Screen {
.map(|e| e.clone())
.collect()
}
pub fn render(&self, p: &Point, char: &str) {
mvprintw(p.1 + self.center.1, p.0 + self.center.0, char);
}
}
pub enum BoardCommand {

@ -21,12 +21,11 @@ use lib::entity::{Queen, Entities};
fn render(e: &Entities, w: &World, b: &Screen) {
for c in w.cleared.iter() {
mvprintw(c.1 + b.center.1, c.0 + b.center.0, "x");
b.render(c, "x");
}
for a in e.data.values() {
let pos = a.get_position();
mvprintw(pos.1 + b.center.1, pos.0 + b.center.0, a.render());
a.render(b);
}
}

Loading…
Cancel
Save