Add GameEvent
This commit is contained in:
parent
7ef25034fd
commit
1b71e4c62f
@ -29,9 +29,9 @@ Single-char
|
||||
║
|
||||
☰ 🮴 Documents ■ (with background)
|
||||
|
||||
🮴 Downloads ⬕ blah.json
|
||||
🮴 Downloads ⬕ blah.json
|
||||
|
||||
🮴 Applications 🮴 Zomboid
|
||||
🮴 Applications 🮴 Zomboid
|
||||
|
||||
|
||||
|
||||
|
||||
68
src/game.rs
68
src/game.rs
@ -10,6 +10,34 @@ pub struct GameModel {
|
||||
room: Room,
|
||||
}
|
||||
|
||||
pub enum GameEvent {
|
||||
Quit,
|
||||
Nop,
|
||||
MovePlayer(V2<isize>),
|
||||
}
|
||||
|
||||
impl GameEvent {
|
||||
fn from_crossterm(event: Event) -> Self {
|
||||
if let Event::Key(key_event) = event {
|
||||
use KeyCode::Char;
|
||||
match key_event.code {
|
||||
Char('q') => return GameEvent::Quit,
|
||||
Char('j') => return GameEvent::MovePlayer(V2::new(0, 1)),
|
||||
Char('k') => return GameEvent::MovePlayer(V2::new(0, -1)),
|
||||
Char('h') => return GameEvent::MovePlayer(V2::new(-1, 0)),
|
||||
Char('l') => return GameEvent::MovePlayer(V2::new(1, 0)),
|
||||
Char('J') => return GameEvent::MovePlayer(V2::new(0, 2)),
|
||||
Char('K') => return GameEvent::MovePlayer(V2::new(0, -2)),
|
||||
Char('H') => return GameEvent::MovePlayer(V2::new(-2, 0)),
|
||||
Char('L') => return GameEvent::MovePlayer(V2::new(2, 0)),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
GameEvent::Nop
|
||||
}
|
||||
}
|
||||
|
||||
struct Room {
|
||||
size: V2<usize>,
|
||||
}
|
||||
@ -25,39 +53,14 @@ impl GameModel {
|
||||
}
|
||||
|
||||
pub fn update(self, event: Event) -> Option<Self> {
|
||||
match event {
|
||||
Event::Key(key_event) => match key_event.code {
|
||||
KeyCode::Char('q') => return None,
|
||||
KeyCode::Char('j') => {
|
||||
return Some(Self {
|
||||
player_pos: self.player_pos + V2::new(0, 1),
|
||||
..self
|
||||
})
|
||||
},
|
||||
KeyCode::Char('k') => {
|
||||
return Some(Self {
|
||||
player_pos: self.player_pos + V2::new(0, -1),
|
||||
..self
|
||||
})
|
||||
},
|
||||
KeyCode::Char('h') => {
|
||||
return Some(Self {
|
||||
player_pos: self.player_pos + V2::new(-1, 0),
|
||||
..self
|
||||
})
|
||||
},
|
||||
KeyCode::Char('l') => {
|
||||
return Some(Self {
|
||||
player_pos: self.player_pos + V2::new(1, 0),
|
||||
..self
|
||||
})
|
||||
},
|
||||
_ => (),
|
||||
},
|
||||
_ => (),
|
||||
match GameEvent::from_crossterm(event) {
|
||||
GameEvent::Quit => None,
|
||||
GameEvent::Nop => Some(self),
|
||||
GameEvent::MovePlayer(direction) => Some(Self {
|
||||
player_pos: self.player_pos + direction,
|
||||
..self
|
||||
}),
|
||||
}
|
||||
|
||||
Some(self)
|
||||
}
|
||||
|
||||
pub fn render(&self, frame: &mut Frame) {
|
||||
@ -117,6 +120,7 @@ impl Tile {
|
||||
cell.set_symbol("⚱").set_fg(Color::LightRed);
|
||||
}
|
||||
Tile::Frog => {
|
||||
// Alternative symbol: Ö̶͈
|
||||
cell.set_symbol("ä̃").set_fg(Color::Green);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user