Bläddra i källkod

Move animation out

master
Noelle 4 år sedan
förälder
incheckning
9ae6ad7f54
2 ändrade filer med 35 tillägg och 1 borttagningar
  1. 33
    0
      src/animator.rs
  2. 2
    1
      src/main.rs

+ 33
- 0
src/animator.rs Visa fil

@@ -0,0 +1,33 @@
use specs::prelude::*;

use crate::components::*;

struct Animator;

impl<'a> System<'a> for Animator {
type SystemData = (
WriteStorage<'a, MovementAnimation>,
WriteStorage<'a, Sprite>,
ReadStorage<'a, Velocity>,
);

fn run(&mut self, mut data: Self::SystemData) {
use self::Direction::*;

for (anim, sprite, vel) in (&mut data.0, &mut data.1, &data.2).join() {
if vel.speed == 0 {
continue;
}

let frames = match vel.direction {
Left => &anim.left_frames,
Right => &anim.right_frames,
Up => &anim.up_frames,
Down => &anim.down_frames,
};

anim.current_frame = (anim.current_frame + 1) % frames.len();
*sprite = frames[anim.current_frame].clone();
}
}
}

+ 2
- 1
src/main.rs Visa fil

@@ -1,5 +1,6 @@
mod components;
mod physics;
mod animator;

use sdl2::pixels::Color;
use sdl2::event::Event;
@@ -171,7 +172,7 @@ fn main() -> Result<(), String> {
}

i = (i + 1) % 255;
update_player(&mut player);
// TODO: Do with specs!

render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, &player)?;


Laddar…
Avbryt
Spara