gdritter repos wenaglia / 862a3d2
rustfmt up in this bizzzzzzzznas Getty Ritter 4 years ago
8 changed file(s) with 82 addition(s) and 79 deletion(s). Collapse all Expand all
1 use specs::{Component, VecStorage, NullStorage};
2 use specs::world::{WorldExt};
1 use specs::world::WorldExt;
2 use specs::{Component, NullStorage, VecStorage};
33
44 /// Register all the components with the world.
55 pub fn register(world: &mut specs::World) {
3434 }
3535
3636 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)
4038 }
4139
4240 pub fn moved(&self, vel: &Velocity) -> Position {
5654 pub dx: f32,
5755 pub dy: f32,
5856 }
59
6057
6158 /// The `Sprite` components represents the current display location of
6259 /// a sprite in the spritesheet.
109106 #[derive(Component, Debug)]
110107 #[storage(VecStorage)]
111108 pub struct Collision {
112 pub has_collision: bool
109 pub has_collision: bool,
113110 }
114111
115112 #[derive(Component)]
119116 }
120117
121118 impl Blocking {
122 pub fn new() -> Blocking { Default::default() }
119 pub fn new() -> Blocking {
120 Default::default()
121 }
123122 }
124123
125124 impl Default for Blocking {
126125 fn default() -> Blocking {
127126 Blocking {
128 volume: Box::new(ncollide2d::shape::Cuboid::new(nalgebra::Vector2::new(11.0, 11.0))),
127 volume: Box::new(ncollide2d::shape::Cuboid::new(nalgebra::Vector2::new(
128 11.0, 11.0,
129 ))),
129130 }
130131 }
131132 }
1010 pub const WINDOW_WIDTH: f32 = BOARD_WIDTH as f32 * (TILE_SIZE * SCALE);
1111 pub const WINDOW_HEIGHT: f32 = BOARD_HEIGHT as f32 * (TILE_SIZE * SCALE);
1212 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] extern crate specs_derive;
1 #[macro_use]
2 extern crate specs_derive;
23
34 use ggez::{
4 Context,
5 ContextBuilder,
6 GameResult,
75 event::{self, EventHandler},
6 Context, ContextBuilder, GameResult,
87 };
98
109 mod nc {
1413 }
1514 use specs::world::WorldExt;
1615
16 pub mod com;
1717 pub mod consts;
18 pub mod com;
1918 pub mod game;
2019 pub mod res;
2120 pub mod sys;
8483 world.insert(sprites);
8584 world.insert(res::KeySet::new());
8685
87 let broad_phase: types::BPhase =
88 nc::DBVTBroadPhase::new(0.0f32);
86 let broad_phase: types::BPhase = nc::DBVTBroadPhase::new(0.0f32);
8987 world.insert(broad_phase);
9088
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 );
9693 world.insert(narrow_phase);
9794
9895 let mut my_game = MyGame { world };
1 use crate::com::*;
12 use crate::consts;
2 use crate::com::*;
33
4 use specs::world::{Builder,WorldExt};
4 use specs::world::{Builder, WorldExt};
55 use std::path::Path;
66
77 #[derive(Debug, Default)]
3636 Decoration,
3737 }
3838
39 static DRAW_LAYERS: [DrawLayer;3] = [
39 static DRAW_LAYERS: [DrawLayer; 3] = [
4040 DrawLayer::Background,
4141 DrawLayer::Foreground,
4242 DrawLayer::Decoration,
4444
4545 pub fn world_from_file<P: AsRef<Path>>(w: &mut specs::World, path: P) {
4646 let tiled::Map {
47 layers,
48 tilesets,
49 ..
47 layers, tilesets, ..
5048 } = tiled::parse_file(path.as_ref()).unwrap();
5149
5250 for (phase, layer) in DRAW_LAYERS.iter().zip(layers) {
5755 let y = y as f32 * consts::TILE_SIZE;
5856 let u = ((n - 1) % 32) as u8;
5957 let v = ((n - u as u32 - 1) / 32) as u8;
60 let mut e = w.create_entity()
58 let mut e = w
59 .create_entity()
6160 .with(Position { x, y })
6261 .with(Sprite { u, v });
6362 e = match phase {
6665 DrawLayer::Decoration => e.with(Decoration),
6766 };
6867
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 {
7171 e.with(Blocking::new())
72 } else {
73 e
74 };
72 } else {
73 e
74 };
7575 e.build();
7676 }
7777 }
8888 .with(Velocity { dx: 0.0, dy: 0.0 })
8989 .with(Foreground)
9090 .with(Controlled)
91 .with(Collision { has_collision: false })
91 .with(Collision {
92 has_collision: false,
93 })
9294 .with(Blocking {
9395 volume: Box::new(ncollide2d::shape::Ball::new(10.0)),
9496 })
1 use crate::com::{self, Position, Sprite};
12 use crate::consts;
2 use crate::com::{self,Position,Sprite};
33 use crate::game::MyGame;
44
5 use ggez::{
6 Context,
7 graphics,
8 graphics::spritebatch::SpriteBatch,
9 };
5 use ggez::{graphics, graphics::spritebatch::SpriteBatch, Context};
106 use specs::RunNow;
117
128 /// The `Draw` system is parameterized by which phase we want to draw
1713 pub _phase: Phase,
1814 }
1915
20 impl<'a, 't, Phase : specs::Component> specs::System<'a> for Draw<'t, Phase> {
16 impl<'a, 't, Phase: specs::Component> specs::System<'a> for Draw<'t, Phase> {
2117 type SystemData = (
2218 specs::ReadStorage<'a, Position>,
2319 specs::ReadStorage<'a, Sprite>,
3228 let param = graphics::DrawParam {
3329 src: spr.to_rect(),
3430 dest: pos.to_point(),
35 scale: mint::Vector2 { x: consts::SCALE, y: consts::SCALE },
31 scale: mint::Vector2 {
32 x: consts::SCALE,
33 y: consts::SCALE,
34 },
3635 ..Default::default()
3736 };
3837 batch.add(param);
3938 }
4039 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();
4541 batch.clear();
4642 }
4743 }
48
4944
5045 pub fn systems(game: &mut MyGame, ctx: &mut Context) -> ggez::GameResult<()> {
5146 graphics::clear(ctx, graphics::BLACK);
5348 Draw {
5449 ctx,
5550 _phase: com::Background,
56 }.run_now(&game.world);
51 }
52 .run_now(&game.world);
5753 Draw {
5854 ctx,
5955 _phase: com::Foreground,
60 }.run_now(&game.world);
56 }
57 .run_now(&game.world);
6158 Draw {
6259 ctx,
6360 _phase: com::Decoration,
64 }.run_now(&game.world);
61 }
62 .run_now(&game.world);
6563
6664 graphics::present(ctx)
6765 }
1 pub mod drawing;
2 pub mod input;
13 pub mod physics;
2 pub mod input;
3 pub mod drawing;
4 pub use drawing::*;
45 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};
32 use crate::game::MyGame;
3 use crate::types::{BPhase, NPhase};
44
55 use nalgebra::{Isometry2, Vector2};
66 use ncollide2d::broad_phase::{
7 broad_phase::BroadPhase,
8 broad_phase::BroadPhaseProxyHandle,
9 BroadPhaseInterferenceHandler,
7 broad_phase::BroadPhase, broad_phase::BroadPhaseProxyHandle, BroadPhaseInterferenceHandler,
108 };
11 use specs::{Join,RunNow};
9 use specs::{Join, RunNow};
1210
1311 struct InterferenceHandler<'a> {
1412 collisions: specs::WriteStorage<'a, Collision>,
2119 }
2220
2321 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);
2624 }
2725
28 fn interference_stopped(&mut self, _: &specs::Entity, _: &specs::Entity) {
29 }
26 fn interference_stopped(&mut self, _: &specs::Entity, _: &specs::Entity) {}
3027 }
31
3228
3329 struct Collide;
3430
4339 specs::WriteExpect<'a, NPhase>,
4440 );
4541
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)| {
5053 let np = if let Some(vel) = velocity.get(e) {
5154 pos.moved(vel)
5255 } else {
5356 pos.clone()
5457 };
5558 bf.create_proxy(
56 bl.volume.aabb(&Isometry2::new(Vector2::new(np.x, np.y), nalgebra::zero())),
59 bl.volume
60 .aabb(&Isometry2::new(Vector2::new(np.x, np.y), nalgebra::zero())),
5761 e,
5862 )
59 }).collect();
63 })
64 .collect();
6065
6166 bf.update(&mut InterferenceHandler {
6267 collisions: collisions,
9398
9499 struct Physics;
95100
96 impl <'a> specs::System<'a> for Physics {
101 impl<'a> specs::System<'a> for Physics {
97102 type SystemData = (
98103 specs::ReadStorage<'a, Velocity>,
99104 specs::ReadStorage<'a, Collision>,
113118 struct ResetCollision;
114119
115120 impl<'a> specs::System<'a> for ResetCollision {
116 type SystemData =
117 specs::WriteStorage<'a, Collision>;
121 type SystemData = specs::WriteStorage<'a, Collision>;
118122
119123 fn run(&mut self, mut collision: Self::SystemData) {
120124 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, ()>;