Complete Chapter 2
This commit is contained in:
parent
f7586c81e2
commit
f0a871381c
@ -21,6 +21,12 @@ pub struct Renderable {
|
|||||||
pub path: String,
|
pub path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Renderable {
|
||||||
|
pub fn new(path: &str) -> Self {
|
||||||
|
Renderable { path: path.to_string() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
#[storage(VecStorage)]
|
#[storage(VecStorage)]
|
||||||
pub struct Wall {}
|
pub struct Wall {}
|
||||||
|
@ -5,9 +5,7 @@ pub fn create_wall(world: &mut World, position: Position) {
|
|||||||
world
|
world
|
||||||
.create_entity()
|
.create_entity()
|
||||||
.with(Position { z: 10, ..position })
|
.with(Position { z: 10, ..position })
|
||||||
.with(Renderable {
|
.with(Renderable::new("/images/wall.png"))
|
||||||
path: "/images/wall.png".to_string(),
|
|
||||||
})
|
|
||||||
.with(Wall {})
|
.with(Wall {})
|
||||||
.with(Immovable)
|
.with(Immovable)
|
||||||
.build();
|
.build();
|
||||||
@ -17,9 +15,7 @@ pub fn create_floor(world: &mut World, position: Position) {
|
|||||||
world
|
world
|
||||||
.create_entity()
|
.create_entity()
|
||||||
.with(Position { z: 5, ..position })
|
.with(Position { z: 5, ..position })
|
||||||
.with(Renderable {
|
.with(Renderable::new("/images/floor.png"))
|
||||||
path: "/images/floor.png".to_string(),
|
|
||||||
})
|
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,9 +23,7 @@ pub fn create_box(world: &mut World, position: Position) {
|
|||||||
world
|
world
|
||||||
.create_entity()
|
.create_entity()
|
||||||
.with(Position { z: 10, ..position })
|
.with(Position { z: 10, ..position })
|
||||||
.with(Renderable {
|
.with(Renderable::new("/images/box.png"))
|
||||||
path: "/images/box.png".to_string(),
|
|
||||||
})
|
|
||||||
.with(Box {})
|
.with(Box {})
|
||||||
.with(Movable)
|
.with(Movable)
|
||||||
.build();
|
.build();
|
||||||
@ -39,9 +33,7 @@ pub fn create_box_spot(world: &mut World, position: Position) {
|
|||||||
world
|
world
|
||||||
.create_entity()
|
.create_entity()
|
||||||
.with(Position { z: 9, ..position })
|
.with(Position { z: 9, ..position })
|
||||||
.with(Renderable {
|
.with(Renderable::new("/images/box_spot.png"))
|
||||||
path: "/images/box_spot.png".to_string(),
|
|
||||||
})
|
|
||||||
.with(BoxSpot {})
|
.with(BoxSpot {})
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
@ -50,9 +42,7 @@ pub fn create_player(world: &mut World, position: Position) {
|
|||||||
world
|
world
|
||||||
.create_entity()
|
.create_entity()
|
||||||
.with(Position { z: 10, ..position })
|
.with(Position { z: 10, ..position })
|
||||||
.with(Renderable {
|
.with(Renderable::new("/images/player.png"))
|
||||||
path: "/images/player.png".to_string(),
|
|
||||||
})
|
|
||||||
.with(Player {})
|
.with(Player {})
|
||||||
.with(Movable)
|
.with(Movable)
|
||||||
.build();
|
.build();
|
||||||
|
@ -1,12 +1,42 @@
|
|||||||
use ggez::event::KeyCode;
|
use ggez::event::KeyCode;
|
||||||
use specs::World;
|
use specs::World;
|
||||||
|
use std::fmt;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
// Resources
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct InputQueue {
|
pub struct InputQueue {
|
||||||
pub keys_pressed: Vec<KeyCode>,
|
pub keys_pressed: Vec<KeyCode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn register_resources(world: &mut World) {
|
#[derive(Default)]
|
||||||
world.insert(InputQueue::default())
|
pub struct Gameplay {
|
||||||
|
pub state: GameplayState,
|
||||||
|
pub moves_count: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum GameplayState {
|
||||||
|
Playing,
|
||||||
|
Won,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for GameplayState {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::Playing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for GameplayState {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
fmt.write_str(match self {
|
||||||
|
GameplayState::Playing => "Playing",
|
||||||
|
GameplayState::Won => "Won",
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn register_resources(world: &mut World) {
|
||||||
|
world.insert(InputQueue::default());
|
||||||
|
world.insert(Gameplay::default());
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,9 @@ use specs::{Entities, ReadStorage, System, Write, WriteStorage};
|
|||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::components::{Immovable, Movable, Player, Position};
|
use crate::components::*;
|
||||||
use crate::constants::{MAP_HEIGHT, MAP_WIDTH};
|
use crate::constants::*;
|
||||||
use crate::resources::InputQueue;
|
use crate::resources::{Gameplay, InputQueue};
|
||||||
|
|
||||||
pub struct InputSystem {}
|
pub struct InputSystem {}
|
||||||
|
|
||||||
@ -15,6 +15,7 @@ impl<'a> System<'a> for InputSystem {
|
|||||||
// Data
|
// Data
|
||||||
type SystemData = (
|
type SystemData = (
|
||||||
Write<'a, InputQueue>,
|
Write<'a, InputQueue>,
|
||||||
|
Write<'a, Gameplay>,
|
||||||
Entities<'a>,
|
Entities<'a>,
|
||||||
WriteStorage<'a, Position>,
|
WriteStorage<'a, Position>,
|
||||||
ReadStorage<'a, Player>,
|
ReadStorage<'a, Player>,
|
||||||
@ -23,7 +24,8 @@ impl<'a> System<'a> for InputSystem {
|
|||||||
);
|
);
|
||||||
|
|
||||||
fn run(&mut self, data: Self::SystemData) {
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
let (mut input_queue, entities, mut positions, players, movables, immovables) = data;
|
let (mut input_queue, mut gameplay, entities, mut positions, players, movables, immovables) =
|
||||||
|
data;
|
||||||
|
|
||||||
let mut to_move = Vec::new();
|
let mut to_move = Vec::new();
|
||||||
|
|
||||||
@ -82,6 +84,11 @@ impl<'a> System<'a> for InputSystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We've just moved, so let's increase the number of moves
|
||||||
|
if to_move.len() > 0 {
|
||||||
|
gameplay.moves_count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
// Now actually move what needs to be moved
|
// Now actually move what needs to be moved
|
||||||
for (key, id) in to_move {
|
for (key, id) in to_move {
|
||||||
let position = positions.get_mut(entities.entity(id));
|
let position = positions.get_mut(entities.entity(id));
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
|
mod gameplay_state_sytem;
|
||||||
mod input_system;
|
mod input_system;
|
||||||
mod rendering_system;
|
mod rendering_system;
|
||||||
|
|
||||||
|
pub use self::gameplay_state_sytem::GameplayStateSystem;
|
||||||
pub use self::input_system::InputSystem;
|
pub use self::input_system::InputSystem;
|
||||||
pub use self::rendering_system::RenderingSystem;
|
pub use self::rendering_system::RenderingSystem;
|
||||||
|
@ -1,21 +1,44 @@
|
|||||||
use ggez::graphics::DrawParam;
|
|
||||||
use ggez::graphics::Image;
|
use ggez::graphics::Image;
|
||||||
|
use ggez::graphics::{Color, DrawParam};
|
||||||
use ggez::nalgebra as na;
|
use ggez::nalgebra as na;
|
||||||
use ggez::{graphics, Context};
|
use ggez::{graphics, Context};
|
||||||
use specs::{Join, ReadStorage, System};
|
use specs::{Join, Read, ReadStorage, System};
|
||||||
|
|
||||||
use crate::components::{Position, Renderable};
|
use crate::components::{Position, Renderable};
|
||||||
use crate::constants::TILE_WIDTH;
|
use crate::constants::TILE_WIDTH;
|
||||||
|
use crate::resources::Gameplay;
|
||||||
|
|
||||||
pub struct RenderingSystem<'a> {
|
pub struct RenderingSystem<'a> {
|
||||||
pub context: &'a mut Context,
|
pub context: &'a mut Context,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl RenderingSystem<'_> {
|
||||||
|
pub fn draw_text(&mut self, text_string: &str, x: f32, y: f32) {
|
||||||
|
let text = graphics::Text::new(text_string);
|
||||||
|
let destination = na::Point2::new(x, y);
|
||||||
|
let color = Some(Color::new(0.0, 0.0, 0.0, 1.0));
|
||||||
|
let dimensions = na::Point2::new(0.0, 20.0);
|
||||||
|
|
||||||
|
graphics::queue_text(self.context, &text, dimensions, color);
|
||||||
|
graphics::draw_queued_text(
|
||||||
|
self.context,
|
||||||
|
graphics::DrawParam::new().dest(destination),
|
||||||
|
None,
|
||||||
|
graphics::FilterMode::Linear,
|
||||||
|
)
|
||||||
|
.expect("expected drawing queued text");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> System<'a> for RenderingSystem<'a> {
|
impl<'a> System<'a> for RenderingSystem<'a> {
|
||||||
type SystemData = (ReadStorage<'a, Position>, ReadStorage<'a, Renderable>);
|
type SystemData = (
|
||||||
|
Read<'a, Gameplay>,
|
||||||
|
ReadStorage<'a, Position>,
|
||||||
|
ReadStorage<'a, Renderable>,
|
||||||
|
);
|
||||||
|
|
||||||
fn run(&mut self, data: Self::SystemData) {
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
let (positions, renderables) = data;
|
let (gameplay, positions, renderables) = data;
|
||||||
|
|
||||||
// Clear the screen/set the background
|
// Clear the screen/set the background
|
||||||
graphics::clear(self.context, graphics::Color::new(0.95, 0.95, 0.95, 1.0));
|
graphics::clear(self.context, graphics::Color::new(0.95, 0.95, 0.95, 1.0));
|
||||||
@ -38,6 +61,10 @@ impl<'a> System<'a> for RenderingSystem<'a> {
|
|||||||
graphics::draw(self.context, &image, draw_params).expect("expected render");
|
graphics::draw(self.context, &image, draw_params).expect("expected render");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Render any text
|
||||||
|
self.draw_text(&gameplay.state.to_string(), 525.0, 80.0);
|
||||||
|
self.draw_text(&gameplay.moves_count.to_string(), 525.0, 100.0);
|
||||||
|
|
||||||
// Finally, present the context, this will actually display everything
|
// Finally, present the context, this will actually display everything
|
||||||
// on the screen.
|
// on the screen.
|
||||||
graphics::present(self.context).expect("expected to present");
|
graphics::present(self.context).expect("expected to present");
|
||||||
|
Loading…
Reference in New Issue
Block a user