Browse Source

Move a bunch of the code around to more sensible places

Getty Ritter 4 years ago
parent
commit
e9b6b2f485
3 changed files with 74 additions and 62 deletions
  1. 9 2
      src/components.rs
  2. 62 2
      src/game.rs
  3. 3 58
      src/main.rs

+ 9 - 2
src/components.rs

@@ -110,6 +110,8 @@ pub struct Collision {
     pub has_collision: bool,
 }
 
+/// A component which represents things which have a collision shape
+/// in the world
 #[derive(Component)]
 #[storage(VecStorage)]
 pub struct Blocking {
@@ -117,7 +119,10 @@ pub struct Blocking {
 }
 
 impl Blocking {
-    pub fn new_shape<S: ncollide2d::shape::Shape<f32>>(e: specs::Entity, w: &mut World, volume: S) -> Blocking {
+    /// create a `Blocking` component for an entity given a specified shape
+    pub fn new_shape<S>(e: specs::Entity, w: &mut World, volume: S) -> Blocking
+    where S: ncollide2d::shape::Shape<f32>
+    {
         let (handle, _) = w.add(
             nalgebra::geometry::Isometry::identity(),
             ncollide2d::shape::ShapeHandle::new(volume),
@@ -130,13 +135,15 @@ impl Blocking {
         }
     }
 
+    /// create an 11pxx11px box for an entity
     pub fn new_box(e: specs::Entity, w: &mut World) -> Blocking {
         Blocking::new_shape(e, w, ncollide2d::shape::Cuboid::new(nalgebra::Vector2::new(
             11.0, 11.0,
         )))
     }
 
+    /// create a 11px ball for an entity
     pub fn new_ball(e: specs::Entity, w: &mut World) -> Blocking {
-        Blocking::new_shape(e, w, ncollide2d::shape::Ball::new(8.0))
+        Blocking::new_shape(e, w, ncollide2d::shape::Ball::new(11.0))
     }
 }

+ 62 - 2
src/game.rs

@@ -1,5 +1,65 @@
-/// The shared values that the game state needs, mostly contained in
-/// the specs ECS world
+use ggez::{Context, GameResult};
+use ggez::event::EventHandler;
+
+use specs::world::WorldExt;
+
+use crate::{components,resources,sys};
+
+/// The shared values that the game state needs, specifically as the specs world
 pub struct MyGame {
     pub world: specs::World,
 }
+
+impl MyGame {
+    // setup the necessary initial state for `MyGame`, inserting the
+    // relevant resources into it
+    pub fn setup(ctx: &mut Context) -> GameResult<MyGame> {
+        let mut world = specs::World::new();
+        components::register(&mut world);
+
+        world.insert(ncollide2d::world::CollisionWorld::<f32, specs::Entity>::new(0.1));
+        resources::world_from_file(&mut world, "assets/main.tmx");
+
+        let image = ggez::graphics::Image::new(ctx, "/spritesheet.png")?;
+        let mut sprites = ggez::graphics::spritebatch::SpriteBatch::new(image);
+        sprites.set_filter(ggez::graphics::FilterMode::Nearest);
+        world.insert(sprites);
+        world.insert(resources::KeySet::new());
+
+        Ok(MyGame { world })
+    }
+}
+
+impl EventHandler for MyGame {
+    fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
+        sys::input::systems(self);
+        sys::physics::systems(self);
+        Ok(())
+    }
+
+    fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
+        sys::drawing::systems(self, ctx)
+    }
+
+    fn key_down_event(
+        &mut self,
+        ctx: &mut Context,
+        keycode: winit::VirtualKeyCode,
+        _keymod: ggez::event::KeyMods,
+        _repeat: bool,
+    ) {
+        if keycode == winit::VirtualKeyCode::Escape {
+            ggez::event::quit(ctx);
+        }
+        self.world.write_resource::<resources::KeySet>().insert(keycode);
+    }
+
+    fn key_up_event(
+        &mut self,
+        _ctx: &mut Context,
+        keycode: winit::VirtualKeyCode,
+        _keymod: ggez::event::KeyMods,
+    ) {
+        self.world.write_resource::<resources::KeySet>().remove(&keycode);
+    }
+}

+ 3 - 58
src/main.rs

@@ -1,13 +1,6 @@
 #[macro_use]
 extern crate specs_derive;
 
-use ggez::{
-    event::{self, EventHandler},
-    Context, ContextBuilder, GameResult,
-};
-
-use specs::world::WorldExt;
-
 pub mod components;
 pub mod consts;
 pub mod game;
@@ -15,51 +8,10 @@ pub mod resources;
 pub mod sys;
 pub mod types;
 
-use game::MyGame;
-
-impl EventHandler for MyGame {
-    fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
-        sys::input::systems(self);
-        sys::physics::systems(self);
-        Ok(())
-    }
-
-    fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
-        sys::drawing::systems(self, ctx)
-    }
-
-    fn key_down_event(
-        &mut self,
-        ctx: &mut Context,
-        keycode: winit::VirtualKeyCode,
-        _keymod: ggez::event::KeyMods,
-        _repeat: bool,
-    ) {
-        if keycode == winit::VirtualKeyCode::Escape {
-            ggez::event::quit(ctx);
-        }
-        self.world.write_resource::<resources::KeySet>().insert(keycode);
-    }
-
-    fn key_up_event(
-        &mut self,
-        _ctx: &mut Context,
-        keycode: winit::VirtualKeyCode,
-        _keymod: ggez::event::KeyMods,
-    ) {
-        self.world.write_resource::<resources::KeySet>().remove(&keycode);
-    }
-}
-
 fn main() -> Result<(), ggez::error::GameError> {
-    let mut world = specs::World::new();
-    components::register(&mut world);
-
-    world.insert(ncollide2d::world::CollisionWorld::<f32, specs::Entity>::new(0.1));
-    resources::world_from_file(&mut world, "assets/main.tmx");
 
     // Make a Context and an EventLoop.
-    let (mut ctx, mut evloop) = ContextBuilder::new("game", "me")
+    let (mut ctx, mut evloop) = ggez::ContextBuilder::new("game", "me")
         .add_resource_path({
             let base = std::env::var("CARGO_MANIFEST_DIR").unwrap();
             let mut path = std::path::PathBuf::from(base);
@@ -73,13 +25,6 @@ fn main() -> Result<(), ggez::error::GameError> {
         })
         .build()?;
 
-    let image = ggez::graphics::Image::new(&mut ctx, "/spritesheet.png")?;
-    let mut sprites = ggez::graphics::spritebatch::SpriteBatch::new(image);
-    sprites.set_filter(ggez::graphics::FilterMode::Nearest);
-    world.insert(sprites);
-    world.insert(resources::KeySet::new());
-
-    let mut my_game = MyGame { world };
-
-    event::run(&mut ctx, &mut evloop, &mut my_game)
+    let mut my_game = game::MyGame::setup(&mut ctx)?;
+    ggez::event::run(&mut ctx, &mut evloop, &mut my_game)
 }