survivor

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

commit
b116722f7c8ca1cd8bddf100cda0d501c2c69edf
parent
8e27f4c6ccee7b0dd09b84fa5a2e26367e19bc6e
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-03-26 18:53
rotate sprites

Diffstat

M src/game.rs 10 +++++-----
M src/win.rs 43 +++++++++++++++++++++++++++++++++++++++++--

2 files changed, 46 insertions, 7 deletions


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

@@ -325,7 +325,7 @@ impl Game {
  325   325         );
  326   326 
  327   327         for diamond in self.diamonds.iter() {
  328    -1             win.sprite(diamond.x + dx, diamond.y + dy, &sprites::DIAMOND, false);
   -1   328             win.sprite(diamond.x + dx, diamond.y + dy, &sprites::DIAMOND, Dir::Right);
  329   329         }
  330   330 
  331   331         let mut player_rendered = false;
@@ -336,7 +336,7 @@ impl Game {
  336   336                     width / 2.0,
  337   337                     height / 2.0,
  338   338                     &sprites::PLAYER,
  339    -1                     self.player.face == Dir::Left,
   -1   339                     self.player.face,
  340   340                 );
  341   341                 player_rendered = true;
  342   342             }
@@ -345,7 +345,7 @@ impl Game {
  345   345                 enemy.p.x + dx,
  346   346                 enemy.p.y + dy,
  347   347                 enemy.t.sprite,
  348    -1                 enemy.p.x > self.player.p.x,
   -1   348                 if enemy.p.x > self.player.p.x { Dir::Left } else { Dir::Right },
  349   349             );
  350   350         }
  351   351         if !player_rendered {
@@ -353,7 +353,7 @@ impl Game {
  353   353                 width / 2.0,
  354   354                 height / 2.0,
  355   355                 &sprites::PLAYER,
  356    -1                 self.player.face == Dir::Left,
   -1   356                 self.player.face,
  357   357             );
  358   358         }
  359   359 
@@ -362,7 +362,7 @@ impl Game {
  362   362                 projectile.p.x + dx,
  363   363                 projectile.p.y + dy,
  364   364                 projectile.t.sprite,
  365    -1                 false,
   -1   365                 projectile.dir,
  366   366             );
  367   367         }
  368   368     }

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

@@ -1,5 +1,6 @@
    1     1 use crate::sprites;
    2     2 use crate::term::Screen;
   -1     3 use crate::game::Dir;
    3     4 
    4     5 const ASPECT_RATIO: f32 = 1.4;
    5     6 
@@ -40,9 +41,9 @@ impl Window<'_> {
   40    41         }
   41    42     }
   42    43 
   43    -1     pub fn sprite(&mut self, cx: f32, cy: f32, sprite: &sprites::Sprite, invert: bool) {
   -1    44     pub fn _sprite(&mut self, cx: f32, cy: f32, sprite: &sprites::Sprite, invert: bool) {
   44    45         let x0 = convert_x(cx) - sprites::WIDTH as i64 / 2;
   45    -1         let y0 = convert_y(cy) + sprites::WIDTH as i64 / 2 - sprites::HEIGHT as i64;
   -1    46         let y0 = convert_y(cy) - (sprites::HEIGHT as i64 - sprites::WIDTH as i64 / 2);
   46    47 
   47    48         for dy in 0..sprites::HEIGHT {
   48    49             let y = y0 + dy as i64;
@@ -69,6 +70,44 @@ impl Window<'_> {
   69    70         }
   70    71     }
   71    72 
   -1    73     pub fn _sprite_tilted(&mut self, cx: f32, cy: f32, sprite: &sprites::Sprite, invert: bool) {
   -1    74         let x0 = convert_x(cx) - sprites::WIDTH as i64 / 2;
   -1    75         let y0 = convert_y(cy) - sprites::WIDTH as i64 / 2;
   -1    76 
   -1    77         for dy in 0..sprites::WIDTH {
   -1    78             let y = y0 + dy as i64;
   -1    79             if y < 0 {
   -1    80                 continue;
   -1    81             }
   -1    82             if y >= self.height as i64 {
   -1    83                 break;
   -1    84             }
   -1    85             for dx in 0..sprites::HEIGHT {
   -1    86                 let x = x0 + dx as i64;
   -1    87                 if x < 0 {
   -1    88                     continue;
   -1    89                 }
   -1    90                 if x >= self.width as i64 {
   -1    91                     break;
   -1    92                 }
   -1    93                 let cy = if invert {sprites::WIDTH - dy - 1 } else { dy };
   -1    94                 let c = sprite[dx][cy];
   -1    95                 if c != sprite[0][0] {
   -1    96                     self.set(x as usize, y as usize, c);
   -1    97                 }
   -1    98             }
   -1    99         }
   -1   100     }
   -1   101 
   -1   102     pub fn sprite(&mut self, cx: f32, cy: f32, sprite: &sprites::Sprite, face: Dir) {
   -1   103         match face {
   -1   104             Dir::Up => self._sprite_tilted(cx, cy, sprite, true),
   -1   105             Dir::Right => self._sprite(cx, cy, sprite, false),
   -1   106             Dir::Down => self._sprite_tilted(cx, cy, sprite, false),
   -1   107             Dir::Left => self._sprite(cx, cy, sprite, true),
   -1   108         }
   -1   109     }
   -1   110 
   72   111     pub fn circle(&mut self, cx: f32, cy: f32, r: f32, color: [u8; 3]) {
   73   112         let r2 = r * r;
   74   113