survivor

graphical action game for the linux terminal
git clone https://git.ce9e.org/survivor.git

commit
7272fa5f74139e34396e90781c79f312f87e209b
parent
c5ce2692a4d7ec156f920aa1a9910f4114ba47a7
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-03-26 18:45
refactor Dir to not include Stop

Diffstat

M src/game.rs 15 +++++++--------
M src/main.rs 10 +++++-----

2 files changed, 12 insertions, 13 deletions


diff --git a/src/game.rs b/src/game.rs

@@ -20,7 +20,6 @@ pub enum Dir {
   20    20     Right,
   21    21     Down,
   22    22     Left,
   23    -1     Stop,
   24    23 }
   25    24 
   26    25 pub struct Pos {
@@ -30,7 +29,7 @@ pub struct Pos {
   30    29 
   31    30 pub struct Player {
   32    31     pub p: Pos,
   33    -1     pub dir: Dir,
   -1    32     pub dir: Option<Dir>,
   34    33     pub face: Dir,
   35    34     pub speed: f32,
   36    35     pub size: f32,
@@ -50,7 +49,7 @@ impl Player {
   50    49     pub fn new() -> Self {
   51    50         return Self {
   52    51             p: Pos { x: 0.0, y: 0.0 },
   53    -1             dir: Dir::Stop,
   -1    52             dir: None,
   54    53             face: Dir::Right,
   55    54             speed: 30.0,
   56    55             size: 9.0,
@@ -114,11 +113,11 @@ impl Game {
  114   113 
  115   114     fn move_player(&mut self, dt: f32) {
  116   115         match self.player.dir {
  117    -1             Dir::Up => self.player.p.y -= self.player.speed * dt,
  118    -1             Dir::Right => self.player.p.x += self.player.speed * dt,
  119    -1             Dir::Down => self.player.p.y += self.player.speed * dt,
  120    -1             Dir::Left => self.player.p.x -= self.player.speed * dt,
  121    -1             Dir::Stop => {}
   -1   116             Some(Dir::Up) => self.player.p.y -= self.player.speed * dt,
   -1   117             Some(Dir::Right) => self.player.p.x += self.player.speed * dt,
   -1   118             Some(Dir::Down) => self.player.p.y += self.player.speed * dt,
   -1   119             Some(Dir::Left) => self.player.p.x -= self.player.speed * dt,
   -1   120             None => {},
  122   121         };
  123   122     }
  124   123 

diff --git a/src/main.rs b/src/main.rs

@@ -66,17 +66,17 @@ fn main() {
   66    66 
   67    67         while let Some(c) = input.getch() {
   68    68             match c {
   69    -1                 b'w' | b'A' => game.player.dir = game::Dir::Up,
   -1    69                 b'w' | b'A' => game.player.dir = Some(game::Dir::Up),
   70    70                 b'a' | b'D' => {
   71    -1                     game.player.dir = game::Dir::Left;
   -1    71                     game.player.dir = Some(game::Dir::Left);
   72    72                     game.player.face = game::Dir::Left
   73    73                 }
   74    -1                 b's' | b'B' => game.player.dir = game::Dir::Down,
   -1    74                 b's' | b'B' => game.player.dir = Some(game::Dir::Down),
   75    75                 b'd' | b'C' => {
   76    -1                     game.player.dir = game::Dir::Right;
   -1    76                     game.player.dir = Some(game::Dir::Right);
   77    77                     game.player.face = game::Dir::Right
   78    78                 }
   79    -1                 b' ' => game.player.dir = game::Dir::Stop,
   -1    79                 b' ' => game.player.dir = None,
   80    80                 b'q' => quit(0),
   81    81                 _ => {}
   82    82             }