1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::io::{self, Write};
use crate::entities::entity::*;

pub struct GameMap {
    width: u8,
    height: u8,
    delimiter: char,
}

impl GameMap{
    pub fn new(width: u8, height: u8, delimiter: char) -> GameMap {
        GameMap{width, height, delimiter }
    }
    pub fn setsize(&mut self, width: u8, height: u8) {
        self.width = width;
        self.height = height;
    }
    pub fn setdelimiter(&mut self, delimiter: char) {
        self.delimiter = delimiter;
    }
    pub fn create(&self) {
        print!("\x1B[2J\x1B[1;1H");
        io::stdout().flush().unwrap();

        // Top lane

        for i in 0..(self.width + 1) {
            print!("{}", self.delimiter);
        }
        print!("\n");
        io::stdout().flush().unwrap();

        // Borders

        for i in 0..(&self.height+1) {
            print!("{}", self.delimiter);
            for j  in 0..(&self.width - 1) {
                print!(" ");
            }
            print!("{}", self.delimiter);
            print!("\n");
            io::stdout().flush().unwrap();

        }

        // Bottom lane

        for i in 0..(self.width + 1) {
            print!("{}", self.delimiter);
        }
        print!("\n");
        io::stdout().flush().unwrap();
    }
    pub fn drawentities(&self, entities: &Vec<Entity>) {

        // Security check

        for entity in entities {
            if entity.get_pos().0 >= self.width || entity.get_pos().1 >= self.height || entity.get_pos().0 <= 0 || entity.get_pos().1 <= 0 {
                return
            }
        }

        print!("\x1B[2J\x1B[1;1H");
        io::stdout().flush().unwrap();

        // Top lane

        for i in 0..(self.width + 1) {
            print!("{}", self.delimiter);
        }
        print!("\n");
        io::stdout().flush().unwrap();
        let mut nonefound = true;

        // Borders

        for i in 0..(&self.height+1) {
            print!("{}", self.delimiter);
            for j  in 0..(&self.width - 1) {
                for entity in entities {
                    if entity.get_pos() == (j, i) {
                        print!("{}", entity.symbol());
                        nonefound = false;
                    }
                }
                if nonefound {
                    print!(" ");
                }
                nonefound = true;
            }
            print!("{}", self.delimiter);
            print!("\n");
            io::stdout().flush().unwrap();

        }

        // Bottom lane

        for i in 0..(self.width + 1) {
            print!("{}", self.delimiter);
        }
        print!("\n");
        io::stdout().flush().unwrap();
    }
}