survivor

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

commit
d375fc4d2a3024273aa296ab6bd60e0a46673096
parent
79f389db09b4a9793760777e2355f80d73ad4e6a
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-03-27 06:42
different movement functions

Diffstat

M src/game.rs 7 +------
M src/weapons.rs 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++

2 files changed, 55 insertions, 6 deletions


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

@@ -177,12 +177,7 @@ impl Game {
  177   177     fn move_projectiles(&mut self, dt: f32) {
  178   178         for weapon in self.player.weapons.iter_mut() {
  179   179             for projectile in weapon.projectiles.iter_mut() {
  180    -1                 match projectile.dir {
  181    -1                     Dir::Up => projectile.p.y -= weapon.speed * dt,
  182    -1                     Dir::Right => projectile.p.x += weapon.speed * dt,
  183    -1                     Dir::Down => projectile.p.y += weapon.speed * dt,
  184    -1                     Dir::Left => projectile.p.x -= weapon.speed * dt,
  185    -1                 }
   -1   180                 (weapon.t._move)(projectile, &self.player.p, weapon.speed, dt);
  186   181             }
  187   182         }
  188   183     }

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

@@ -7,6 +7,7 @@ pub struct WeaponType {
    7     7     pub base_cooldown: f32,
    8     8     pub size: f32,
    9     9     pub sprite: &'static sprites::Sprite,
   -1    10     pub _move: fn(&mut Projectile, &Pos, speed: f32, dt: f32) -> (),
   10    11 }
   11    12 
   12    13 pub struct Projectile {
@@ -36,12 +37,62 @@ impl Weapon {
   36    37     }
   37    38 }
   38    39 
   -1    40 pub fn move_straight(projectile: &mut Projectile, center: &Pos, speed: f32, dt: f32) {
   -1    41     match projectile.dir {
   -1    42         Dir::Up => projectile.p.y -= speed * dt,
   -1    43         Dir::Right => projectile.p.x += speed * dt,
   -1    44         Dir::Down => projectile.p.y += speed * dt,
   -1    45         Dir::Left => projectile.p.x -= speed * dt,
   -1    46     }
   -1    47 }
   -1    48 
   -1    49 pub fn move_diagonal(projectile: &mut Projectile, center: &Pos, speed: f32, dt: f32) {
   -1    50     let dx = projectile.p.x - center.x;
   -1    51     let dy = projectile.p.y - center.y;
   -1    52     let r = f32::sqrt(dx * dx + dy * dy);
   -1    53 
   -1    54     let r2 = r + speed * dt;
   -1    55     projectile.p.x = center.x + dx / r * r2;
   -1    56     projectile.p.y = center.y + dy / r * r2;
   -1    57     projectile.dir = if dx < 0.0 { Dir::Left } else { Dir::Right };
   -1    58 }
   -1    59 
   -1    60 pub fn move_parabola(projectile: &mut Projectile, center: &Pos, speed: f32, dt: f32) {
   -1    61     let t = (projectile.p.x - center.x).abs() / speed;
   -1    62     projectile.p.y += speed * (4.0 * t - 1.0) * dt;
   -1    63     if projectile.p.x < center.x {
   -1    64         projectile.p.x -= speed * dt;
   -1    65         projectile.dir = Dir::Left;
   -1    66     } else {
   -1    67         projectile.p.x += speed * dt;
   -1    68         projectile.dir = Dir::Right;
   -1    69     }
   -1    70 }
   -1    71 
   -1    72 pub fn move_spiral(projectile: &mut Projectile, center: &Pos, speed: f32, dt: f32) {
   -1    73     let dx = projectile.p.x - center.x;
   -1    74     let dy = projectile.p.y - center.y;
   -1    75     let r = f32::sqrt(dx * dx + dy * dy);
   -1    76     let angle = dy.atan2(dx);
   -1    77 
   -1    78     let r2 = r + 20.0 * dt;
   -1    79     let angle2 = angle + speed * dt / r.max(1.0);
   -1    80 
   -1    81     let (sin, cos) = angle2.sin_cos();
   -1    82 
   -1    83     projectile.p.x = center.x + cos * r2;
   -1    84     projectile.p.y = center.y + sin * r2;
   -1    85 
   -1    86     projectile.dir = if projectile.p.y > center.y { Dir::Left } else { Dir::Right };
   -1    87 }
   -1    88 
   39    89 pub const AXE: WeaponType = WeaponType {
   40    90     base_speed: 150.0,
   41    91     base_damage: 50.0,
   42    92     base_cooldown: 10.0,
   43    93     size: 7.0,
   44    94     sprite: &sprites::AXE,
   -1    95     _move: move_parabola,
   45    96 };
   46    97 
   47    98 pub const KNIFE: WeaponType = WeaponType {
@@ -50,6 +101,7 @@ pub const KNIFE: WeaponType = WeaponType {
   50   101     base_cooldown: 4.0,
   51   102     size: 6.0,
   52   103     sprite: &sprites::KNIFE,
   -1   104     _move: move_straight,
   53   105 };
   54   106 
   55   107 pub const STAR: WeaponType = WeaponType {
@@ -58,6 +110,7 @@ pub const STAR: WeaponType = WeaponType {
   58   110     base_cooldown: 3.0,
   59   111     size: 6.0,
   60   112     sprite: &sprites::STAR,
   -1   113     _move: move_diagonal,
   61   114 };
   62   115 
   63   116 pub const WIND: WeaponType = WeaponType {
@@ -66,4 +119,5 @@ pub const WIND: WeaponType = WeaponType {
   66   119     base_cooldown: 9.0,
   67   120     size: 8.0,
   68   121     sprite: &sprites::WIND,
   -1   122     _move: move_spiral,
   69   123 };