added before_render and after_render to renderable trait
This commit is contained in:
+39
-10
@@ -1,7 +1,9 @@
|
|||||||
|
|
||||||
use crate::Point;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use crate::lib::ai::AI;
|
use crate::lib::ai::AI;
|
||||||
|
use crate::Point;
|
||||||
|
use crate::Screen;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use ncurses::*;
|
||||||
|
|
||||||
use downcast_rs::{impl_downcast, Downcast};
|
use downcast_rs::{impl_downcast, Downcast};
|
||||||
|
|
||||||
@@ -25,9 +27,16 @@ impl Ant {
|
|||||||
impl Entity for Ant {}
|
impl Entity for Ant {}
|
||||||
|
|
||||||
impl Renderable for Ant {
|
impl Renderable for Ant {
|
||||||
fn render(&self) -> &str {
|
fn representation(&self) -> &str {
|
||||||
"o"
|
"o"
|
||||||
}
|
}
|
||||||
|
fn before_render(&self) {
|
||||||
|
attron(A_BOLD());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn after_render(&self) {
|
||||||
|
attroff(A_BOLD());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Queen {
|
pub struct Queen {
|
||||||
@@ -37,14 +46,27 @@ pub struct Queen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Renderable for Queen {
|
impl Renderable for Queen {
|
||||||
fn render(&self) -> &str {
|
fn representation(&self) -> &str {
|
||||||
"q"
|
"q"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn before_render(&self) {
|
||||||
|
attron(A_BOLD());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn after_render(&self) {
|
||||||
|
attroff(A_BOLD());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Renderable {
|
pub trait Renderable: AI {
|
||||||
fn render(&self) -> &str {
|
fn representation(&self) -> &str;
|
||||||
"z"
|
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 {
|
impl Renderable for Egg {
|
||||||
fn render(&self) -> &str {
|
fn representation(&self) -> &str {
|
||||||
"e"
|
"e"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn before_render(&self) {
|
||||||
|
attron(A_BOLD());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn after_render(&self) {
|
||||||
|
attroff(A_BOLD());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Egg {
|
impl Egg {
|
||||||
@@ -123,4 +153,3 @@ impl Entities {
|
|||||||
id
|
id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ pub struct Screen {
|
|||||||
max_y: i32,
|
max_y: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use ncurses::*;
|
||||||
|
|
||||||
impl Screen {
|
impl Screen {
|
||||||
pub fn new(max_x: i32, max_y: i32) -> Screen {
|
pub fn new(max_x: i32, max_y: i32) -> Screen {
|
||||||
Screen {
|
Screen {
|
||||||
@@ -27,6 +29,10 @@ impl Screen {
|
|||||||
.map(|e| e.clone())
|
.map(|e| e.clone())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn render(&self, p: &Point, char: &str) {
|
||||||
|
mvprintw(p.1 + self.center.1, p.0 + self.center.0, char);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum BoardCommand {
|
pub enum BoardCommand {
|
||||||
|
|||||||
+2
-3
@@ -21,12 +21,11 @@ use lib::entity::{Queen, Entities};
|
|||||||
|
|
||||||
fn render(e: &Entities, w: &World, b: &Screen) {
|
fn render(e: &Entities, w: &World, b: &Screen) {
|
||||||
for c in w.cleared.iter() {
|
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() {
|
for a in e.data.values() {
|
||||||
let pos = a.get_position();
|
a.render(b);
|
||||||
mvprintw(pos.1 + b.center.1, pos.0 + b.center.0, a.render());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user