rustfmt up in this bizzzzzzzznas
Getty Ritter
6 years ago
| 1 | use specs::{Component, VecStorage, NullStorage}; | |
| 2 | use specs::world::{WorldExt}; | |
| 1 | use specs::world::WorldExt; | |
| 2 | use specs::{Component, NullStorage, VecStorage}; | |
| 3 | 3 | |
| 4 | 4 | /// Register all the components with the world. |
| 5 | 5 | pub fn register(world: &mut specs::World) { |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | pub fn to_grid(&self) -> (i32, i32) { |
| 37 | ((self.x / 24.0) as i32, | |
| 38 | (self.y / 24.0) as i32, | |
| 39 |
|
|
| 37 | ((self.x / 24.0) as i32, (self.y / 24.0) as i32) | |
| 40 | 38 | } |
| 41 | 39 | |
| 42 | 40 | pub fn moved(&self, vel: &Velocity) -> Position { |
| 56 | 54 | pub dx: f32, |
| 57 | 55 | pub dy: f32, |
| 58 | 56 | } |
| 59 | ||
| 60 | 57 | |
| 61 | 58 | /// The `Sprite` components represents the current display location of |
| 62 | 59 | /// a sprite in the spritesheet. |
| 109 | 106 | #[derive(Component, Debug)] |
| 110 | 107 | #[storage(VecStorage)] |
| 111 | 108 | pub struct Collision { |
| 112 |
pub has_collision: bool |
|
| 109 | pub has_collision: bool, | |
| 113 | 110 | } |
| 114 | 111 | |
| 115 | 112 | #[derive(Component)] |
| 119 | 116 | } |
| 120 | 117 | |
| 121 | 118 | impl Blocking { |
| 122 |
pub fn new() -> Blocking { |
|
| 119 | pub fn new() -> Blocking { | |
| 120 | Default::default() | |
| 121 | } | |
| 123 | 122 | } |
| 124 | 123 | |
| 125 | 124 | impl Default for Blocking { |
| 126 | 125 | fn default() -> Blocking { |
| 127 | 126 | Blocking { |
| 128 |
volume: Box::new(ncollide2d::shape::Cuboid::new(nalgebra::Vector2::new( |
|
| 127 | volume: Box::new(ncollide2d::shape::Cuboid::new(nalgebra::Vector2::new( | |
| 128 | 11.0, 11.0, | |
| 129 | ))), | |
| 129 | 130 | } |
| 130 | 131 | } |
| 131 | 132 | } |
| 10 | 10 | pub const WINDOW_WIDTH: f32 = BOARD_WIDTH as f32 * (TILE_SIZE * SCALE); |
| 11 | 11 | pub const WINDOW_HEIGHT: f32 = BOARD_HEIGHT as f32 * (TILE_SIZE * SCALE); |
| 12 | 12 | pub const SCALE: f32 = 3.0; |
| 13 | pub const TILED_TRUE: tiled::PropertyValue = | |
| 14 | tiled::PropertyValue::BoolValue(true); | |
| 13 | pub const TILED_TRUE: tiled::PropertyValue = tiled::PropertyValue::BoolValue(true); |
| 1 |
#[macro_use] |
|
| 1 | #[macro_use] | |
| 2 | extern crate specs_derive; | |
| 2 | 3 | |
| 3 | 4 | use ggez::{ |
| 4 | Context, | |
| 5 | ContextBuilder, | |
| 6 | GameResult, | |
| 7 | 5 | event::{self, EventHandler}, |
| 6 | Context, ContextBuilder, GameResult, | |
| 8 | 7 | }; |
| 9 | 8 | |
| 10 | 9 | mod nc { |
| 14 | 13 | } |
| 15 | 14 | use specs::world::WorldExt; |
| 16 | 15 | |
| 16 | pub mod com; | |
| 17 | 17 | pub mod consts; |
| 18 | pub mod com; | |
| 19 | 18 | pub mod game; |
| 20 | 19 | pub mod res; |
| 21 | 20 | pub mod sys; |
| 84 | 83 | world.insert(sprites); |
| 85 | 84 | world.insert(res::KeySet::new()); |
| 86 | 85 | |
| 87 | let broad_phase: types::BPhase = | |
| 88 | nc::DBVTBroadPhase::new(0.0f32); | |
| 86 | let broad_phase: types::BPhase = nc::DBVTBroadPhase::new(0.0f32); | |
| 89 | 87 | world.insert(broad_phase); |
| 90 | 88 | |
| 91 | let narrow_phase: types::NPhase = | |
| 92 | nc::NarrowPhase::new( | |
| 93 | Box::new(nc::DefaultContactDispatcher::new()), | |
| 94 | Box::new(nc::DefaultProximityDispatcher::new()), | |
| 95 |
|
|
| 89 | let narrow_phase: types::NPhase = nc::NarrowPhase::new( | |
| 90 | Box::new(nc::DefaultContactDispatcher::new()), | |
| 91 | Box::new(nc::DefaultProximityDispatcher::new()), | |
| 92 | ); | |
| 96 | 93 | world.insert(narrow_phase); |
| 97 | 94 | |
| 98 | 95 | let mut my_game = MyGame { world }; |
| 1 | use crate::com::*; | |
| 1 | 2 | use crate::consts; |
| 2 | use crate::com::*; | |
| 3 | 3 | |
| 4 |
use specs::world::{Builder, |
|
| 4 | use specs::world::{Builder, WorldExt}; | |
| 5 | 5 | use std::path::Path; |
| 6 | 6 | |
| 7 | 7 | #[derive(Debug, Default)] |
| 36 | 36 | Decoration, |
| 37 | 37 | } |
| 38 | 38 | |
| 39 |
static DRAW_LAYERS: [DrawLayer; |
|
| 39 | static DRAW_LAYERS: [DrawLayer; 3] = [ | |
| 40 | 40 | DrawLayer::Background, |
| 41 | 41 | DrawLayer::Foreground, |
| 42 | 42 | DrawLayer::Decoration, |
| 44 | 44 | |
| 45 | 45 | pub fn world_from_file<P: AsRef<Path>>(w: &mut specs::World, path: P) { |
| 46 | 46 | let tiled::Map { |
| 47 | layers, | |
| 48 | tilesets, | |
| 49 |
|
|
| 47 | layers, tilesets, .. | |
| 50 | 48 | } = tiled::parse_file(path.as_ref()).unwrap(); |
| 51 | 49 | |
| 52 | 50 | for (phase, layer) in DRAW_LAYERS.iter().zip(layers) { |
| 57 | 55 | let y = y as f32 * consts::TILE_SIZE; |
| 58 | 56 | let u = ((n - 1) % 32) as u8; |
| 59 | 57 | let v = ((n - u as u32 - 1) / 32) as u8; |
| 60 |
let mut e = w |
|
| 58 | let mut e = w | |
| 59 | .create_entity() | |
| 61 | 60 | .with(Position { x, y }) |
| 62 | 61 | .with(Sprite { u, v }); |
| 63 | 62 | e = match phase { |
| 66 | 65 | DrawLayer::Decoration => e.with(Decoration), |
| 67 | 66 | }; |
| 68 | 67 | |
| 69 | let e = if tilesets[0].tiles[n as usize].properties["pass"] == | |
| 70 | tiled::PropertyValue::BoolValue(false) { | |
| 68 | let e = if tilesets[0].tiles[n as usize].properties["pass"] | |
| 69 | == tiled::PropertyValue::BoolValue(false) | |
| 70 | { | |
| 71 | 71 | e.with(Blocking::new()) |
| 72 | } else { | |
| 73 | e | |
| 74 |
|
|
| 72 | } else { | |
| 73 | e | |
| 74 | }; | |
| 75 | 75 | e.build(); |
| 76 | 76 | } |
| 77 | 77 | } |
| 88 | 88 | .with(Velocity { dx: 0.0, dy: 0.0 }) |
| 89 | 89 | .with(Foreground) |
| 90 | 90 | .with(Controlled) |
| 91 |
.with(Collision { |
|
| 91 | .with(Collision { | |
| 92 | has_collision: false, | |
| 93 | }) | |
| 92 | 94 | .with(Blocking { |
| 93 | 95 | volume: Box::new(ncollide2d::shape::Ball::new(10.0)), |
| 94 | 96 | }) |
| 1 | use crate::com::{self, Position, Sprite}; | |
| 1 | 2 | use crate::consts; |
| 2 | use crate::com::{self,Position,Sprite}; | |
| 3 | 3 | use crate::game::MyGame; |
| 4 | 4 | |
| 5 | use ggez::{ | |
| 6 | Context, | |
| 7 | graphics, | |
| 8 | graphics::spritebatch::SpriteBatch, | |
| 9 |
|
|
| 5 | use ggez::{graphics, graphics::spritebatch::SpriteBatch, Context}; | |
| 10 | 6 | use specs::RunNow; |
| 11 | 7 | |
| 12 | 8 | /// The `Draw` system is parameterized by which phase we want to draw |
| 17 | 13 | pub _phase: Phase, |
| 18 | 14 | } |
| 19 | 15 | |
| 20 |
impl<'a, 't, Phase |
|
| 16 | impl<'a, 't, Phase: specs::Component> specs::System<'a> for Draw<'t, Phase> { | |
| 21 | 17 | type SystemData = ( |
| 22 | 18 | specs::ReadStorage<'a, Position>, |
| 23 | 19 | specs::ReadStorage<'a, Sprite>, |
| 32 | 28 | let param = graphics::DrawParam { |
| 33 | 29 | src: spr.to_rect(), |
| 34 | 30 | dest: pos.to_point(), |
| 35 |
scale: mint::Vector2 { |
|
| 31 | scale: mint::Vector2 { | |
| 32 | x: consts::SCALE, | |
| 33 | y: consts::SCALE, | |
| 34 | }, | |
| 36 | 35 | ..Default::default() |
| 37 | 36 | }; |
| 38 | 37 | batch.add(param); |
| 39 | 38 | } |
| 40 | 39 | let basic: graphics::DrawParam = Default::default(); |
| 41 | graphics::draw( | |
| 42 | self.ctx, | |
| 43 | &*batch, | |
| 44 | basic).unwrap(); | |
| 40 | graphics::draw(self.ctx, &*batch, basic).unwrap(); | |
| 45 | 41 | batch.clear(); |
| 46 | 42 | } |
| 47 | 43 | } |
| 48 | ||
| 49 | 44 | |
| 50 | 45 | pub fn systems(game: &mut MyGame, ctx: &mut Context) -> ggez::GameResult<()> { |
| 51 | 46 | graphics::clear(ctx, graphics::BLACK); |
| 53 | 48 | Draw { |
| 54 | 49 | ctx, |
| 55 | 50 | _phase: com::Background, |
| 56 |
} |
|
| 51 | } | |
| 52 | .run_now(&game.world); | |
| 57 | 53 | Draw { |
| 58 | 54 | ctx, |
| 59 | 55 | _phase: com::Foreground, |
| 60 |
} |
|
| 56 | } | |
| 57 | .run_now(&game.world); | |
| 61 | 58 | Draw { |
| 62 | 59 | ctx, |
| 63 | 60 | _phase: com::Decoration, |
| 64 |
} |
|
| 61 | } | |
| 62 | .run_now(&game.world); | |
| 65 | 63 | |
| 66 | 64 | graphics::present(ctx) |
| 67 | 65 | } |
| 1 | pub mod drawing; | |
| 2 | pub mod input; | |
| 1 | 3 | pub mod physics; |
| 2 | pub mod input; | |
| 3 | pub mod drawing; | |
| 4 | pub use drawing::*; | |
| 4 | 5 | pub use input::*; |
| 5 | pub use drawing::*; |
| 1 | use crate::com::{Blocking, Collision, Velocity, Position}; | |
| 2 | use crate::types::{NPhase,BPhase}; | |
| 1 | use crate::com::{Blocking, Collision, Position, Velocity}; | |
| 3 | 2 | use crate::game::MyGame; |
| 3 | use crate::types::{BPhase, NPhase}; | |
| 4 | 4 | |
| 5 | 5 | use nalgebra::{Isometry2, Vector2}; |
| 6 | 6 | use ncollide2d::broad_phase::{ |
| 7 | broad_phase::BroadPhase, | |
| 8 | broad_phase::BroadPhaseProxyHandle, | |
| 9 |
|
|
| 7 | broad_phase::BroadPhase, broad_phase::BroadPhaseProxyHandle, BroadPhaseInterferenceHandler, | |
| 10 | 8 | }; |
| 11 |
use specs::{Join, |
|
| 9 | use specs::{Join, RunNow}; | |
| 12 | 10 | |
| 13 | 11 | struct InterferenceHandler<'a> { |
| 14 | 12 | collisions: specs::WriteStorage<'a, Collision>, |
| 21 | 19 | } |
| 22 | 20 | |
| 23 | 21 | fn interference_started(&mut self, a: &specs::Entity, b: &specs::Entity) { |
| 24 | self.collisions.get_mut(*a).map (|r| r.has_collision = true ); | |
| 25 | self.collisions.get_mut(*b).map (|r| r.has_collision = true ); | |
| 22 | self.collisions.get_mut(*a).map(|r| r.has_collision = true); | |
| 23 | self.collisions.get_mut(*b).map(|r| r.has_collision = true); | |
| 26 | 24 | } |
| 27 | 25 | |
| 28 | fn interference_stopped(&mut self, _: &specs::Entity, _: &specs::Entity) { | |
| 29 | } | |
| 26 | fn interference_stopped(&mut self, _: &specs::Entity, _: &specs::Entity) {} | |
| 30 | 27 | } |
| 31 | ||
| 32 | 28 | |
| 33 | 29 | struct Collide; |
| 34 | 30 | |
| 43 | 39 | specs::WriteExpect<'a, NPhase>, |
| 44 | 40 | ); |
| 45 | 41 | |
| 46 | fn run(&mut self, (entities, position, velocity, blocking, mut collisions, mut bf, _narrow): Self::SystemData) { | |
| 47 | let _: Vec<()> = (&mut collisions).join().map( |c| c.has_collision = false ).collect(); | |
| 48 | let handles: Vec<BroadPhaseProxyHandle> = | |
| 49 | (&entities, &position, &blocking).join().map( |(e, pos, bl)| { | |
| 42 | fn run( | |
| 43 | &mut self, | |
| 44 | (entities, position, velocity, blocking, mut collisions, mut bf, _narrow): Self::SystemData, | |
| 45 | ) { | |
| 46 | let _: Vec<()> = (&mut collisions) | |
| 47 | .join() | |
| 48 | .map(|c| c.has_collision = false) | |
| 49 | .collect(); | |
| 50 | let handles: Vec<BroadPhaseProxyHandle> = (&entities, &position, &blocking) | |
| 51 | .join() | |
| 52 | .map(|(e, pos, bl)| { | |
| 50 | 53 | let np = if let Some(vel) = velocity.get(e) { |
| 51 | 54 | pos.moved(vel) |
| 52 | 55 | } else { |
| 53 | 56 | pos.clone() |
| 54 | 57 | }; |
| 55 | 58 | bf.create_proxy( |
| 56 |
bl.volume |
|
| 59 | bl.volume | |
| 60 | .aabb(&Isometry2::new(Vector2::new(np.x, np.y), nalgebra::zero())), | |
| 57 | 61 | e, |
| 58 | 62 | ) |
| 59 |
|
|
| 63 | }) | |
| 64 | .collect(); | |
| 60 | 65 | |
| 61 | 66 | bf.update(&mut InterferenceHandler { |
| 62 | 67 | collisions: collisions, |
| 93 | 98 | |
| 94 | 99 | struct Physics; |
| 95 | 100 | |
| 96 |
impl |
|
| 101 | impl<'a> specs::System<'a> for Physics { | |
| 97 | 102 | type SystemData = ( |
| 98 | 103 | specs::ReadStorage<'a, Velocity>, |
| 99 | 104 | specs::ReadStorage<'a, Collision>, |
| 113 | 118 | struct ResetCollision; |
| 114 | 119 | |
| 115 | 120 | impl<'a> specs::System<'a> for ResetCollision { |
| 116 | type SystemData = | |
| 117 | specs::WriteStorage<'a, Collision>; | |
| 121 | type SystemData = specs::WriteStorage<'a, Collision>; | |
| 118 | 122 | |
| 119 | 123 | fn run(&mut self, mut collision: Self::SystemData) { |
| 120 | 124 | for mut e in (&mut collision).join() { |
| 1 | pub type BPhase = | |
| 2 | ncollide2d::broad_phase::DBVTBroadPhase<f32, ncollide2d::bounding_volume::AABB<f32>, specs::Entity>; | |
| 3 | pub type NPhase = | |
| 4 | ncollide2d::narrow_phase::NarrowPhase<f32, ()>; | |
| 1 | pub type BPhase = ncollide2d::broad_phase::DBVTBroadPhase< | |
| 2 | f32, | |
| 3 | ncollide2d::bounding_volume::AABB<f32>, | |
| 4 | specs::Entity, | |
| 5 | >; | |
| 6 | pub type NPhase = ncollide2d::narrow_phase::NarrowPhase<f32, ()>; |