| 4 | 4 |
use ggez::event::{self, EventHandler};
|
| 5 | 5 |
use ggez::graphics;
|
| 6 | 6 |
|
| 7 | |
use std::path::Path;
|
| 8 | |
use specs::{Component, RunNow, System, NullStorage, VecStorage};
|
| 9 | |
use specs::world::Builder;
|
| 7 |
use specs::RunNow;
|
| 10 | 8 |
|
| 11 | |
#[allow(dead_code)]
|
| 12 | |
mod consts {
|
| 13 | |
pub const SPEED: u32 = 2;
|
| 14 | |
pub const TILE_SIZE: u32 = 24;
|
| 15 | |
pub const HALF_TILE: u32 = TILE_SIZE / 2;
|
| 16 | |
pub const QUARTER_TILE: u32 = TILE_SIZE / 4;
|
| 17 | |
pub const BOARD_WIDTH: usize = 16;
|
| 18 | |
pub const BOARD_HEIGHT: usize = 12;
|
| 19 | |
pub const SCALE: u32 = 3;
|
| 20 | |
pub const TILED_TRUE: tiled::PropertyValue =
|
| 21 | |
tiled::PropertyValue::BoolValue(true);
|
| 22 | |
}
|
| 9 |
use sdl2::keyboard as sdl;
|
| 23 | 10 |
|
| 24 | |
#[derive(Component, Debug)]
|
| 25 | |
#[storage(VecStorage)]
|
| 26 | |
struct Position {
|
| 27 | |
x: f32,
|
| 28 | |
y: f32,
|
| 29 | |
}
|
| 30 | |
|
| 31 | |
impl Position {
|
| 32 | |
fn to_point(&self) -> ggez::nalgebra::Point2<f32> {
|
| 33 | |
ggez::nalgebra::Point2::new(self.x, self.y)
|
| 34 | |
}
|
| 35 | |
}
|
| 36 | |
|
| 37 | |
#[derive(Component, Debug)]
|
| 38 | |
#[storage(VecStorage)]
|
| 39 | |
struct Sprite {
|
| 40 | |
u: u8,
|
| 41 | |
v: u8,
|
| 42 | |
}
|
| 43 | |
|
| 44 | |
impl Sprite {
|
| 45 | |
fn to_rect(&self) -> graphics::Rect {
|
| 46 | |
graphics::Rect {
|
| 47 | |
x: (1.0 / 32.0) * self.u as f32,
|
| 48 | |
y: (1.0 / 32.0) * self.v as f32,
|
| 49 | |
w: 1.0 / 32.0,
|
| 50 | |
h: 1.0 / 32.0,
|
| 51 | |
}
|
| 52 | |
}
|
| 53 | |
}
|
| 54 | |
|
| 55 | |
#[derive(Debug, Copy, Clone)]
|
| 56 | |
enum DrawLayer {
|
| 57 | |
Background,
|
| 58 | |
Foreground,
|
| 59 | |
Decoration,
|
| 60 | |
}
|
| 61 | |
|
| 62 | |
static DRAW_LAYERS: [DrawLayer;3] = [
|
| 63 | |
DrawLayer::Background,
|
| 64 | |
DrawLayer::Foreground,
|
| 65 | |
DrawLayer::Decoration,
|
| 66 | |
];
|
| 67 | |
|
| 68 | |
#[derive(Component, Default, Debug)]
|
| 69 | |
#[storage(NullStorage)]
|
| 70 | |
struct Background;
|
| 71 | |
|
| 72 | |
#[derive(Component, Default, Debug)]
|
| 73 | |
#[storage(NullStorage)]
|
| 74 | |
struct Foreground;
|
| 75 | |
|
| 76 | |
#[derive(Component, Default, Debug)]
|
| 77 | |
#[storage(NullStorage)]
|
| 78 | |
struct Decoration;
|
| 79 | |
|
| 80 | |
fn world_from_file(w: &mut specs::World, path: &Path) {
|
| 81 | |
let tiled::Map {
|
| 82 | |
layers,
|
| 83 | |
..
|
| 84 | |
} = tiled::parse_file(path).unwrap();
|
| 85 | |
|
| 86 | |
for (phase, layer) in DRAW_LAYERS.iter().zip(layers) {
|
| 87 | |
for (row, y) in layer.tiles.iter().zip(0..) {
|
| 88 | |
for (&n, x) in row.iter().zip(0..) {
|
| 89 | |
if n != 0 {
|
| 90 | |
let x = x as f32 * 24.0;
|
| 91 | |
let y = y as f32 * 24.0;
|
| 92 | |
let u = ((n - 1) % 32) as u8;
|
| 93 | |
let v = ((n - u as u32 - 1) / 32) as u8;
|
| 94 | |
let mut e = w.create_entity()
|
| 95 | |
.with(Position { x, y })
|
| 96 | |
.with(Sprite { u, v });
|
| 97 | |
e = match phase {
|
| 98 | |
DrawLayer::Background => e.with(Background),
|
| 99 | |
DrawLayer::Foreground => e.with(Foreground),
|
| 100 | |
DrawLayer::Decoration => e.with(Decoration),
|
| 101 | |
};
|
| 102 | |
e.build();
|
| 103 | |
}
|
| 104 | |
}
|
| 105 | |
}
|
| 106 | |
}
|
| 107 | |
}
|
| 11 |
pub mod consts;
|
| 12 |
pub mod components;
|
| 13 |
pub mod res;
|
| 14 |
pub mod sys;
|
| 108 | 15 |
|
| 109 | 16 |
struct MyGame {
|
| 110 | 17 |
world: specs::World,
|
| 111 | 18 |
sprites: graphics::spritebatch::SpriteBatch,
|
| 112 | 19 |
}
|
| 113 | 20 |
|
| 114 | |
struct Draw<'t, Phase> {
|
| 115 | |
ctx: &'t mut Context,
|
| 116 | |
sprites: &'t mut graphics::spritebatch::SpriteBatch,
|
| 117 | |
_phase: Phase,
|
| 118 | |
}
|
| 21 |
impl EventHandler for MyGame {
|
| 119 | 22 |
|
| 120 | |
impl<'a, 't, Phase: Component> System<'a> for Draw<'t, Phase> {
|
| 121 | |
type SystemData = (
|
| 122 | |
specs::ReadStorage<'a, Position>,
|
| 123 | |
specs::ReadStorage<'a, Sprite>,
|
| 124 | |
specs::ReadStorage<'a, Phase>,
|
| 125 | |
);
|
| 126 | |
|
| 127 | |
fn run(&mut self, (positions, sprites, phase): Self::SystemData) {
|
| 128 | |
use specs::Join;
|
| 129 | |
|
| 130 | |
for (pos, spr, _) in (&positions, &sprites, &phase).join() {
|
| 131 | |
let param = graphics::DrawParam {
|
| 132 | |
src: spr.to_rect(),
|
| 133 | |
dest: pos.to_point(),
|
| 134 | |
..Default::default()
|
| 135 | |
};
|
| 136 | |
self.sprites.add(param);
|
| 137 | |
graphics::draw(
|
| 138 | |
self.ctx,
|
| 139 | |
self.sprites,
|
| 140 | |
ggez::nalgebra::Point2::new(0.0, 0.0),
|
| 141 | |
0.0).unwrap();
|
| 142 | |
self.sprites.clear();
|
| 143 | |
}
|
| 144 | |
}
|
| 145 | |
}
|
| 146 | |
|
| 147 | |
impl EventHandler for MyGame {
|
| 148 | 23 |
fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
|
| 149 | 24 |
Ok(())
|
| 150 | 25 |
}
|
| 151 | 26 |
|
| 152 | 27 |
fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
|
| 153 | |
use ggez::graphics as g;
|
| 154 | |
use ggez::nalgebra as n;
|
| 155 | |
g::set_background_color(ctx, g::BLACK);
|
| 156 | |
g::clear(ctx);
|
| 28 |
ggez::graphics::set_background_color(ctx, ggez::graphics::BLACK);
|
| 29 |
ggez::graphics::clear(ctx);
|
| 157 | 30 |
|
| 158 | |
Draw { ctx, sprites: &mut self.sprites, _phase: Background }.run_now(&self.world.res);
|
| 159 | |
Draw { ctx, sprites: &mut self.sprites, _phase: Foreground }.run_now(&self.world.res);
|
| 160 | |
Draw { ctx, sprites: &mut self.sprites, _phase: Decoration }.run_now(&self.world.res);
|
| 31 |
sys::Draw {
|
| 32 |
ctx,
|
| 33 |
sprites: &mut self.sprites,
|
| 34 |
_phase: components::Background,
|
| 35 |
}.run_now(&self.world.res);
|
| 36 |
sys::Draw {
|
| 37 |
ctx,
|
| 38 |
sprites: &mut self.sprites,
|
| 39 |
_phase: components::Foreground,
|
| 40 |
}.run_now(&self.world.res);
|
| 41 |
sys::Draw {
|
| 42 |
ctx,
|
| 43 |
sprites: &mut self.sprites,
|
| 44 |
_phase: components::Decoration,
|
| 45 |
}.run_now(&self.world.res);
|
| 161 | 46 |
|
| 162 | |
g::draw(ctx, &self.sprites, n::Point2::new(0.0, 0.0), 0.0)?;
|
| 47 |
ggez::graphics::draw(
|
| 48 |
ctx,
|
| 49 |
&self.sprites,
|
| 50 |
ggez::nalgebra::Point2::new(0.0, 0.0),
|
| 51 |
0.0
|
| 52 |
)?;
|
| 163 | 53 |
self.sprites.clear();
|
| 164 | |
g::present(ctx);
|
| 54 |
ggez::graphics::present(ctx);
|
| 165 | 55 |
Ok(())
|
| 56 |
}
|
| 57 |
|
| 58 |
fn key_down_event(
|
| 59 |
&mut self,
|
| 60 |
ctx: &mut Context,
|
| 61 |
keycode: sdl::Keycode,
|
| 62 |
_keymod: sdl::Mod,
|
| 63 |
_repeat: bool,
|
| 64 |
) {
|
| 65 |
if keycode == sdl::Keycode::Escape {
|
| 66 |
ctx.quit().expect("Should never fail");
|
| 67 |
}
|
| 68 |
sys::Move { keycode }.run_now(&self.world.res);
|
| 166 | 69 |
}
|
| 167 | 70 |
}
|
| 168 | 71 |
|
| 169 | 72 |
fn main() -> Result<(), ggez::error::GameError> {
|
| 170 | 73 |
let mut world = specs::World::new();
|
| 171 | |
world.register::<Position>();
|
| 172 | |
world.register::<Sprite>();
|
| 173 | |
world.register::<Background>();
|
| 174 | |
world.register::<Foreground>();
|
| 175 | |
world.register::<Decoration>();
|
| 74 |
components::register(&mut world);
|
| 176 | 75 |
|
| 177 | |
world_from_file(&mut world, &Path::new("assets/main.tmx"));
|
| 76 |
res::world_from_file(&mut world, "assets/main.tmx");
|
| 178 | 77 |
|
| 179 | 78 |
// Make a Context and an EventLoop.
|
| 180 | 79 |
let mut ctx = ContextBuilder::new("game", "me")
|
|
| 184 | 83 |
path.push("assets");
|
| 185 | 84 |
path
|
| 186 | 85 |
})
|
| 86 |
.window_mode(ggez::conf::WindowMode {
|
| 87 |
width: consts::WINDOW_WIDTH,
|
| 88 |
height: consts::WINDOW_HEIGHT,
|
| 89 |
..Default::default()
|
| 90 |
})
|
| 187 | 91 |
.build()?;
|
| 188 | 92 |
let image = graphics::Image::new(&mut ctx, "/spritesheet.png")?;
|
| 189 | |
let sprites = graphics::spritebatch::SpriteBatch::new(image);
|
| 93 |
let mut sprites = graphics::spritebatch::SpriteBatch::new(image);
|
| 94 |
sprites.set_filter(ggez::graphics::FilterMode::Nearest);
|
| 190 | 95 |
|
| 191 | 96 |
let mut my_game = MyGame {
|
| 192 | 97 |
world,
|