gdritter repos wenaglia / e9b6b2f
Move a bunch of the code around to more sensible places Getty Ritter 5 years ago
3 changed file(s) with 74 addition(s) and 62 deletion(s). Collapse all Expand all
110110 pub has_collision: bool,
111111 }
112112
113 /// A component which represents things which have a collision shape
114 /// in the world
113115 #[derive(Component)]
114116 #[storage(VecStorage)]
115117 pub struct Blocking {
117119 }
118120
119121 impl Blocking {
120 pub fn new_shape<S: ncollide2d::shape::Shape<f32>>(e: specs::Entity, w: &mut World, volume: S) -> Blocking {
122 /// create a `Blocking` component for an entity given a specified shape
123 pub fn new_shape<S>(e: specs::Entity, w: &mut World, volume: S) -> Blocking
124 where S: ncollide2d::shape::Shape<f32>
125 {
121126 let (handle, _) = w.add(
122127 nalgebra::geometry::Isometry::identity(),
123128 ncollide2d::shape::ShapeHandle::new(volume),
130135 }
131136 }
132137
138 /// create an 11pxx11px box for an entity
133139 pub fn new_box(e: specs::Entity, w: &mut World) -> Blocking {
134140 Blocking::new_shape(e, w, ncollide2d::shape::Cuboid::new(nalgebra::Vector2::new(
135141 11.0, 11.0,
136142 )))
137143 }
138144
145 /// create a 11px ball for an entity
139146 pub fn new_ball(e: specs::Entity, w: &mut World) -> Blocking {
140 Blocking::new_shape(e, w, ncollide2d::shape::Ball::new(8.0))
147 Blocking::new_shape(e, w, ncollide2d::shape::Ball::new(11.0))
141148 }
142149 }
1 /// The shared values that the game state needs, mostly contained in
2 /// the specs ECS world
1 use ggez::{Context, GameResult};
2 use ggez::event::EventHandler;
3
4 use specs::world::WorldExt;
5
6 use crate::{components,resources,sys};
7
8 /// The shared values that the game state needs, specifically as the specs world
39 pub struct MyGame {
410 pub world: specs::World,
511 }
12
13 impl MyGame {
14 // setup the necessary initial state for `MyGame`, inserting the
15 // relevant resources into it
16 pub fn setup(ctx: &mut Context) -> GameResult<MyGame> {
17 let mut world = specs::World::new();
18 components::register(&mut world);
19
20 world.insert(ncollide2d::world::CollisionWorld::<f32, specs::Entity>::new(0.1));
21 resources::world_from_file(&mut world, "assets/main.tmx");
22
23 let image = ggez::graphics::Image::new(ctx, "/spritesheet.png")?;
24 let mut sprites = ggez::graphics::spritebatch::SpriteBatch::new(image);
25 sprites.set_filter(ggez::graphics::FilterMode::Nearest);
26 world.insert(sprites);
27 world.insert(resources::KeySet::new());
28
29 Ok(MyGame { world })
30 }
31 }
32
33 impl EventHandler for MyGame {
34 fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
35 sys::input::systems(self);
36 sys::physics::systems(self);
37 Ok(())
38 }
39
40 fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
41 sys::drawing::systems(self, ctx)
42 }
43
44 fn key_down_event(
45 &mut self,
46 ctx: &mut Context,
47 keycode: winit::VirtualKeyCode,
48 _keymod: ggez::event::KeyMods,
49 _repeat: bool,
50 ) {
51 if keycode == winit::VirtualKeyCode::Escape {
52 ggez::event::quit(ctx);
53 }
54 self.world.write_resource::<resources::KeySet>().insert(keycode);
55 }
56
57 fn key_up_event(
58 &mut self,
59 _ctx: &mut Context,
60 keycode: winit::VirtualKeyCode,
61 _keymod: ggez::event::KeyMods,
62 ) {
63 self.world.write_resource::<resources::KeySet>().remove(&keycode);
64 }
65 }
11 #[macro_use]
22 extern crate specs_derive;
3
4 use ggez::{
5 event::{self, EventHandler},
6 Context, ContextBuilder, GameResult,
7 };
8
9 use specs::world::WorldExt;
103
114 pub mod components;
125 pub mod consts;
158 pub mod sys;
169 pub mod types;
1710
18 use game::MyGame;
19
20 impl EventHandler for MyGame {
21 fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
22 sys::input::systems(self);
23 sys::physics::systems(self);
24 Ok(())
25 }
26
27 fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
28 sys::drawing::systems(self, ctx)
29 }
30
31 fn key_down_event(
32 &mut self,
33 ctx: &mut Context,
34 keycode: winit::VirtualKeyCode,
35 _keymod: ggez::event::KeyMods,
36 _repeat: bool,
37 ) {
38 if keycode == winit::VirtualKeyCode::Escape {
39 ggez::event::quit(ctx);
40 }
41 self.world.write_resource::<resources::KeySet>().insert(keycode);
42 }
43
44 fn key_up_event(
45 &mut self,
46 _ctx: &mut Context,
47 keycode: winit::VirtualKeyCode,
48 _keymod: ggez::event::KeyMods,
49 ) {
50 self.world.write_resource::<resources::KeySet>().remove(&keycode);
51 }
52 }
53
5411 fn main() -> Result<(), ggez::error::GameError> {
55 let mut world = specs::World::new();
56 components::register(&mut world);
57
58 world.insert(ncollide2d::world::CollisionWorld::<f32, specs::Entity>::new(0.1));
59 resources::world_from_file(&mut world, "assets/main.tmx");
6012
6113 // Make a Context and an EventLoop.
62 let (mut ctx, mut evloop) = ContextBuilder::new("game", "me")
14 let (mut ctx, mut evloop) = ggez::ContextBuilder::new("game", "me")
6315 .add_resource_path({
6416 let base = std::env::var("CARGO_MANIFEST_DIR").unwrap();
6517 let mut path = std::path::PathBuf::from(base);
7325 })
7426 .build()?;
7527
76 let image = ggez::graphics::Image::new(&mut ctx, "/spritesheet.png")?;
77 let mut sprites = ggez::graphics::spritebatch::SpriteBatch::new(image);
78 sprites.set_filter(ggez::graphics::FilterMode::Nearest);
79 world.insert(sprites);
80 world.insert(resources::KeySet::new());
81
82 let mut my_game = MyGame { world };
83
84 event::run(&mut ctx, &mut evloop, &mut my_game)
28 let mut my_game = game::MyGame::setup(&mut ctx)?;
29 ggez::event::run(&mut ctx, &mut evloop, &mut my_game)
8530 }