Pārlūkot izejas kodu

Move animation out

master
Noelle pirms 4 gadiem
vecāks
revīzija
9ae6ad7f54
2 mainītis faili ar 35 papildinājumiem un 1 dzēšanām
  1. 33
    0
      src/animator.rs
  2. 2
    1
      src/main.rs

+ 33
- 0
src/animator.rs Parādīt failu

@@ -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 Parādīt failu

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


Notiek ielāde…
Atcelt
Saglabāt