Move a bunch of the code around to more sensible places
Getty Ritter
5 years ago
110 | 110 | pub has_collision: bool, |
111 | 111 | } |
112 | 112 | |
113 | /// A component which represents things which have a collision shape | |
114 | /// in the world | |
113 | 115 | #[derive(Component)] |
114 | 116 | #[storage(VecStorage)] |
115 | 117 | pub struct Blocking { |
117 | 119 | } |
118 | 120 | |
119 | 121 | impl Blocking { |
120 |
|
|
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 | { | |
121 | 126 | let (handle, _) = w.add( |
122 | 127 | nalgebra::geometry::Isometry::identity(), |
123 | 128 | ncollide2d::shape::ShapeHandle::new(volume), |
130 | 135 | } |
131 | 136 | } |
132 | 137 | |
138 | /// create an 11pxx11px box for an entity | |
133 | 139 | pub fn new_box(e: specs::Entity, w: &mut World) -> Blocking { |
134 | 140 | Blocking::new_shape(e, w, ncollide2d::shape::Cuboid::new(nalgebra::Vector2::new( |
135 | 141 | 11.0, 11.0, |
136 | 142 | ))) |
137 | 143 | } |
138 | 144 | |
145 | /// create a 11px ball for an entity | |
139 | 146 | pub fn new_ball(e: specs::Entity, w: &mut World) -> Blocking { |
140 |
Blocking::new_shape(e, w, ncollide2d::shape::Ball::new( |
|
147 | Blocking::new_shape(e, w, ncollide2d::shape::Ball::new(11.0)) | |
141 | 148 | } |
142 | 149 | } |
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 | |
3 | 9 | pub struct MyGame { |
4 | 10 | pub world: specs::World, |
5 | 11 | } |
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 | } |
1 | 1 | #[macro_use] |
2 | 2 | extern crate specs_derive; |
3 | ||
4 | use ggez::{ | |
5 | event::{self, EventHandler}, | |
6 | Context, ContextBuilder, GameResult, | |
7 | }; | |
8 | ||
9 | use specs::world::WorldExt; | |
10 | 3 | |
11 | 4 | pub mod components; |
12 | 5 | pub mod consts; |
15 | 8 | pub mod sys; |
16 | 9 | pub mod types; |
17 | 10 | |
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 | ||
54 | 11 | 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"); | |
60 | 12 | |
61 | 13 | // Make a Context and an EventLoop. |
62 |
let (mut ctx, mut evloop) = |
|
14 | let (mut ctx, mut evloop) = ggez::ContextBuilder::new("game", "me") | |
63 | 15 | .add_resource_path({ |
64 | 16 | let base = std::env::var("CARGO_MANIFEST_DIR").unwrap(); |
65 | 17 | let mut path = std::path::PathBuf::from(base); |
73 | 25 | }) |
74 | 26 | .build()?; |
75 | 27 | |
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 |
|
|
28 | let mut my_game = game::MyGame::setup(&mut ctx)?; | |
29 | ggez::event::run(&mut ctx, &mut evloop, &mut my_game) | |
85 | 30 | } |