12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- use crate::com::{Blocking, Collision, Velocity, Position};
- use crate::game::MyGame;
- use specs::{Join,RunNow};
- struct Intersection;
- impl<'a> specs::System<'a> for Intersection {
- type SystemData = (
- specs::Entities<'a>,
- specs::ReadStorage<'a, Position>,
- specs::ReadStorage<'a, Velocity>,
- specs::ReadStorage<'a, Blocking>,
- specs::WriteStorage<'a, Collision>,
- );
- fn run(&mut self, (entity, position, velocity, blocking, mut collision): Self::SystemData) {
- let mut spacemap = std::collections::HashMap::new();
- for (e, pos, _) in (&entity, &position, &blocking).join() {
- spacemap.insert(pos.to_grid(), e);
- }
- for (pos, vel, col) in (&position, &velocity, &mut collision).join() {
- if let Some(_) = spacemap.get(&pos.moved(vel).to_grid()) {
- col.has_collision = true;
- }
- }
- }
- }
- struct Physics;
- impl <'a> specs::System<'a> for Physics {
- type SystemData = (
- specs::ReadStorage<'a, Velocity>,
- specs::ReadStorage<'a, Collision>,
- specs::WriteStorage<'a, Position>,
- );
- fn run(&mut self, (velocity, collision, mut position): Self::SystemData) {
- for (vel, col, pos) in (&velocity, &collision, &mut position).join() {
- if !col.has_collision {
- pos.x += vel.dx;
- pos.y += vel.dy;
- }
- }
- }
- }
- struct ResetCollision;
- impl<'a> specs::System<'a> for ResetCollision {
- type SystemData =
- specs::WriteStorage<'a, Collision>;
- fn run(&mut self, mut collision: Self::SystemData) {
- for mut e in (&mut collision).join() {
- e.has_collision = false;
- }
- }
- }
- pub fn systems(game: &mut MyGame) {
- Intersection.run_now(&game.world.res);
- Physics.run_now(&game.world.res);
- ResetCollision.run_now(&game.world.res);
- }
|