浏览代码

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


正在加载...
取消
保存