aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 2e45c581f461d12202b0e8bbfd6e1d6c2f3776d3 (plain)
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
extern crate pancurses;
extern crate rand;

extern crate text_io;

mod entities;
mod state;
mod tiling;
mod world;

use entities::{Character, Entity, Player};
use pancurses::{endwin, initscr, noecho, Input};
use state::State;
use std::env;
use world::{Dungeon, DOWN, LEFT, RIGHT, UP};

fn get_player_name() -> String {
    match env::var_os("USER") {
        Some(val) => val.into_string().unwrap(),
        None => String::from("Kshar"),
    }
}

fn main() {
    let window = initscr();

    let mut state = State::new(
        Player::new(get_player_name(), String::from("Warrior"), 30, 10, 10, 20),
        Dungeon::new(
            window.get_max_x() as usize,
            (window.get_max_y() - 2) as usize,
            5,
        ),
    );

    state.init();

    window.keypad(true);
    noecho();

    state.render_level(&window);

    loop {
        // update
        state.render_entities(&window);

        state.render_player(&window);

        // get input and execute it
        match window.getch() {
            Some(Input::Character('?')) => {
                window.addstr("q: quit\n");
            }
            Some(Input::Character('j')) => {
                state.player.move_by(DOWN).unwrap();
                // state.get_player_mut().move_by(DOWN).unwrap();
            }
            Some(Input::Character('k')) => {
                state.get_player_mut().move_by(UP).unwrap();
            }
            Some(Input::Character('h')) => {
                state.get_player_mut().move_by(LEFT).unwrap();
            }
            Some(Input::Character('l')) => {
                state.get_player_mut().move_by(RIGHT).unwrap();
            }
            Some(Input::Character('q')) => break,
            Some(_) => (),
            None => (),
        }
        // actors actions (normally attack / interact if on same location as the character)
    }
    endwin();
}