aboutsummaryrefslogtreecommitdiffstats
path: root/test/rl.rs
blob: f2ad637fea904c29dddac52b4fb53adb45453adb (plain) (blame)
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
extern mod term;
use core::io::ReaderUtil;

fn term_app (body: &fn ()) {
    term::info::init();
    do term::ios::preserve {
        do term::info::with_alternate_screen {
            body()
        }
    }
}

fn draw_map (rows: uint, cols: uint) {
    for uint::range(0, rows) |i| {
        term::info::move(0, i);
        io::print(str::repeat(".", cols));
    }
}

fn draw_character (x: uint, y: uint) {
    term::info::move(x, y);
    io::print("@");
    term::info::move(x, y);
}

fn draw_ground (x: uint, y: uint) {
    term::info::move(x, y);
    io::print(".");
}

fn main () {
    let (rows, cols) = term::ios::size();

    do term_app {
        term::ios::cbreak();
        term::ios::echo(false);
        term::info::clear();

        draw_map(rows, cols);

        let mut (x, y) = (0u, 0u);
        let mut cursor = true;
        loop {
            draw_character(x, y);
            match io::stdin().read_char() {
                'q' => { break }
                'h' if x > 0        => { draw_ground(x, y); x -= 1 }
                'j' if y < rows - 1 => { draw_ground(x, y); y += 1 }
                'k' if y > 0        => { draw_ground(x, y); y -= 1 }
                'l' if x < cols - 1 => { draw_ground(x, y); x += 1 }
                ' ' => { term::info::cursor(cursor); cursor = !cursor }
                _   => { }
            }
        }
    }
}