7 Hour Roguelike, spring 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.5 KiB

use super::*;
use rogue_util::coord::*;
use termion::event::Key;
use std::io;
pub struct Player<R> {
pos: V2i,
hp: usize, // TODO
time: usize,
input: R,
}
impl<R> Player<R> {
pub fn new(p: V2i, r: R) -> Player<R> {
Player {
pos: p,
hp: 100,
time: 0,
input: r,
}
}
}
impl<R> Entity for Player<R>
where
R: Iterator<Item=io::Result<Key>>
{
fn pos(&self) -> V2i { self.pos }
fn set_pos(&mut self, p: V2i) { self.pos = p; }
fn next_time(&self) -> usize { self.time }
fn set_next_time(&mut self, t: usize) { self.time = t; }
fn class(&self) -> usize {
ECLS_LIVING
}
fn char(&self) -> char { '@' }
fn act(&mut self, _obs: &Observation) -> Action {
use termion::event::Key::*;
loop {
match self.input.next().unwrap().unwrap() {
Char('h') | Char('a') | Left => return Action::Move(LinfDir::L),
Char('j') | Char('s') | Down => return Action::Move(LinfDir::D),
Char('k') | Char('w') | Up => return Action::Move(LinfDir::U),
Char('l') | Char('d') | Right => return Action::Move(LinfDir::R),
Char('y') => return Action::Move(LinfDir::UL),
Char('u') => return Action::Move(LinfDir::UR),
Char('b') => return Action::Move(LinfDir::DL),
Char('n') => return Action::Move(LinfDir::DR),
Char('q') => return Action::Quit,
_ => ()
}
}
}
}