Browse Source

Add physics

master
Noelle 4 years ago
parent
commit
2c872a1591
2 changed files with 31 additions and 0 deletions
  1. 1
    0
      src/main.rs
  2. 30
    0
      src/physics.rs

+ 1
- 0
src/main.rs View File

mod components; mod components;
mod physics;


use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::event::Event; use sdl2::event::Event;

+ 30
- 0
src/physics.rs View File

use specs::prelude::*;

use crate::components::*;

struct Physics;

impl<'a> System<'a> for Physics {
type SystemData = (WriteStorage<'a, Position>, ReadStorage<'a, Velocity>);

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

for (pos, vel) in (&mut data.0, &data.1).join() {
match vel.direction {
Left => {
pos.0 = pos.0.offset(-vel.speed, 0);
},
Right => {
pos.0 = pos.0.offset(vel.speed, 0);
},
Up => {
pos.0 = pos.0.offset(0, -vel.speed);
},
Down => {
pos.0 = pos.0.offset(0, vel.speed);
},
}
}
}
}

Loading…
Cancel
Save