Add GameEvent
This commit is contained in:
parent
7ef25034fd
commit
1b71e4c62f
@ -29,9 +29,9 @@ Single-char
|
|||||||
║
|
║
|
||||||
☰ 🮴 Documents ■ (with background)
|
☰ 🮴 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,
|
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 {
|
struct Room {
|
||||||
size: V2<usize>,
|
size: V2<usize>,
|
||||||
}
|
}
|
||||||
@ -25,39 +53,14 @@ impl GameModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(self, event: Event) -> Option<Self> {
|
pub fn update(self, event: Event) -> Option<Self> {
|
||||||
match event {
|
match GameEvent::from_crossterm(event) {
|
||||||
Event::Key(key_event) => match key_event.code {
|
GameEvent::Quit => None,
|
||||||
KeyCode::Char('q') => return None,
|
GameEvent::Nop => Some(self),
|
||||||
KeyCode::Char('j') => {
|
GameEvent::MovePlayer(direction) => Some(Self {
|
||||||
return Some(Self {
|
player_pos: self.player_pos + direction,
|
||||||
player_pos: self.player_pos + V2::new(0, 1),
|
..self
|
||||||
..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
|
|
||||||
})
|
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(self)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&self, frame: &mut Frame) {
|
pub fn render(&self, frame: &mut Frame) {
|
||||||
@ -117,6 +120,7 @@ impl Tile {
|
|||||||
cell.set_symbol("⚱").set_fg(Color::LightRed);
|
cell.set_symbol("⚱").set_fg(Color::LightRed);
|
||||||
}
|
}
|
||||||
Tile::Frog => {
|
Tile::Frog => {
|
||||||
|
// Alternative symbol: Ö̶͈
|
||||||
cell.set_symbol("ä̃").set_fg(Color::Green);
|
cell.set_symbol("ä̃").set_fg(Color::Green);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user