SHARE
TWEET
Input handler
a guest
Nov 7th, 2018
48
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- extern crate tcod;
- use tcod::input::Key;
- use tcod::input::KeyCode::*;
- use self::tcod::console::Root;
- use ::actors::Object;
- pub enum PlayerAction<'a> {
- TookTurn,
- Moved(MoveCommand<'a>),
- DidntTakeTurn,
- Exit,
- }
- trait Executes {
- fn execute(self);
- }
- pub struct MoveCommand<'a> {
- pub entity: Option<&'a mut Object>,
- dx: i32,
- dy: i32
- }
- impl<'a> MoveCommand<'a> {
- pub fn new(dx: i32, dy: i32) -> MoveCommand<'a> {
- MoveCommand {
- entity: None,
- dx, dy
- }
- }
- }
- impl<'a> Executes for MoveCommand<'a> {
- fn execute(self) {
- if let Some(entity) = self.entity {
- entity.move_by(self.dx, self.dy)
- };
- }
- }
- pub fn handle_keys<'a>(root: &mut Root) -> PlayerAction {
- let key = root.wait_for_keypress(true);
- match key {
- Key { code: Enter, alt: true, .. } => {
- let fullscreen = root.is_fullscreen();
- root.set_fullscreen(!fullscreen);
- PlayerAction::DidntTakeTurn
- },
- Key { code: Escape, .. } => {
- PlayerAction::Exit
- },
- Key { code: Up, .. } => {
- PlayerAction::Moved(MoveCommand::new(0, -1))
- }
- Key { code: Down, .. } => {
- PlayerAction::Moved(MoveCommand::new(0, 1))
- }
- Key { code: Left, .. } => {
- PlayerAction::Moved(MoveCommand::new(-1, 0))
- }
- Key { code: Right, .. } => {
- PlayerAction::Moved(MoveCommand::new(1, 0))
- }
- _ => {
- PlayerAction::DidntTakeTurn
- }
- }
- }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

