Переглянути джерело

Move animation out

master
Noelle 4 роки тому
джерело
коміт
9ae6ad7f54
2 змінених файлів з 35 додано та 1 видалено
  1. 33
    0
      src/animator.rs
  2. 2
    1
      src/main.rs

+ 33
- 0
src/animator.rs Переглянути файл

@@ -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 Переглянути файл

@@ -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)?;


Завантаження…
Відмінити
Зберегти