-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.rs
223 lines (192 loc) · 5.83 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use ggez;
use ggez::graphics;
use ggez::graphics::DrawParam;
use ggez::graphics::Image;
use ggez::nalgebra as na;
use ggez::{conf, event, Context, GameResult};
use specs::{
join::Join, Builder, Component, ReadStorage, RunNow, System, VecStorage, World, WorldExt,
};
use std::path;
const TILE_WIDTH: f32 = 32.0;
// Components
#[derive(Debug, Component, Clone, Copy)]
#[storage(VecStorage)]
pub struct Position {
x: u8,
y: u8,
z: u8,
}
#[derive(Component)]
#[storage(VecStorage)]
pub struct Renderable {
path: String,
}
#[derive(Component)]
#[storage(VecStorage)]
pub struct Wall {}
#[derive(Component)]
#[storage(VecStorage)]
pub struct Player {}
#[derive(Component)]
#[storage(VecStorage)]
pub struct Box {}
#[derive(Component)]
#[storage(VecStorage)]
pub struct BoxSpot {}
// Systems
pub struct RenderingSystem<'a> {
context: &'a mut Context,
}
// System implementation
impl<'a> System<'a> for RenderingSystem<'a> {
// Data
type SystemData = (ReadStorage<'a, Position>, ReadStorage<'a, Renderable>);
fn run(&mut self, data: Self::SystemData) {
let (positions, renderables) = data;
// Clearing the screen (this gives us the backround colour)
graphics::clear(self.context, graphics::Color::new(0.95, 0.95, 0.95, 1.0));
// Get all the renderables with their positions and sort by the position z
// This will allow us to have entities layered visually.
let mut rendering_data = (&positions, &renderables).join().collect::<Vec<_>>();
rendering_data.sort_by(|&a, &b| a.0.z.partial_cmp(&b.0.z).expect("expected comparison"));
// Iterate through all pairs of positions & renderables, load the image
// and draw it at the specified position.
for (position, renderable) in rendering_data.iter() {
// Load the image
let image = Image::new(self.context, renderable.path.clone()).expect("expected image");
let x = position.x as f32 * TILE_WIDTH;
let y = position.y as f32 * TILE_WIDTH;
// draw
let draw_params = DrawParam::new().dest(na::Point2::new(x, y));
graphics::draw(self.context, &image, draw_params).expect("expected render");
}
// Finally, present the context, this will actually display everything
// on the screen.
graphics::present(self.context).expect("expected to present");
}
}
// This struct will hold all our game state
// For now there is nothing to be held, but we'll add
// things shortly.
struct Game {
world: World,
}
// This is the main event loop. ggez tells us to implement
// two things:
// - updating
// - rendering
impl event::EventHandler for Game {
fn update(&mut self, _context: &mut Context) -> GameResult {
Ok(())
}
fn draw(&mut self, context: &mut Context) -> GameResult {
// Render game entities
{
let mut rs = RenderingSystem { context };
rs.run_now(&self.world);
}
Ok(())
}
}
// Register components with the world
pub fn register_components(world: &mut World) {
world.register::<Position>();
world.register::<Renderable>();
world.register::<Player>();
world.register::<Wall>();
world.register::<Box>();
world.register::<BoxSpot>();
}
// Create a wall entity
pub fn create_wall(world: &mut World, position: Position) {
world
.create_entity()
.with(Position { z: 10, ..position })
.with(Renderable {
path: "/images/wall.png".to_string(),
})
.with(Wall {})
.build();
}
pub fn create_floor(world: &mut World, position: Position) {
world
.create_entity()
.with(Position { z: 5, ..position })
.with(Renderable {
path: "/images/floor.png".to_string(),
})
.build();
}
pub fn create_box(world: &mut World, position: Position) {
world
.create_entity()
.with(Position { z: 10, ..position })
.with(Renderable {
path: "/images/box.png".to_string(),
})
.with(Box {})
.build();
}
pub fn create_box_spot(world: &mut World, position: Position) {
world
.create_entity()
.with(Position { z: 9, ..position })
.with(Renderable {
path: "/images/box_spot.png".to_string(),
})
.with(BoxSpot {})
.build();
}
pub fn create_player(world: &mut World, position: Position) {
world
.create_entity()
.with(Position { z: 10, ..position })
.with(Renderable {
path: "/images/player.png".to_string(),
})
.with(Player {})
.build();
}
// Initialize the level
pub fn initialize_level(world: &mut World) {
create_player(
world,
Position {
x: 0,
y: 0,
z: 0, // we will get the z from the factory functions
},
);
create_wall(
world,
Position {
x: 1,
y: 0,
z: 0, // we will get the z from the factory functions
},
);
create_box(
world,
Position {
x: 2,
y: 0,
z: 0, // we will get the z from the factory functions
},
);
}
pub fn main() -> GameResult {
let mut world = World::new();
register_components(&mut world);
initialize_level(&mut world);
// Create a game context and event loop
let context_builder = ggez::ContextBuilder::new("rust_sokoban", "sokoban")
.window_setup(conf::WindowSetup::default().title("Rust Sokoban!"))
.window_mode(conf::WindowMode::default().dimensions(800.0, 600.0))
.add_resource_path(path::PathBuf::from("./resources"));
let (context, event_loop) = &mut context_builder.build()?;
// Create the game state
let game = &mut Game { world };
// Run the main event loop
event::run(context, event_loop, game)
}