Skip to content

Commit

Permalink
add & use LetterEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
jayrave committed Jun 8, 2020
1 parent c208e33 commit 75868e3
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 3 deletions.
2 changes: 2 additions & 0 deletions core/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod enemy;
mod ground;
mod icon;
pub mod input;
mod letter;
pub mod player;
pub mod score;

Expand All @@ -14,3 +15,4 @@ pub use drawable::Drawable;
pub use enemy::Enemy;
pub use ground::Ground;
pub use icon::Icon;
pub use letter::Letter;
6 changes: 6 additions & 0 deletions core/src/components/letter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use specs::Component;
use specs::NullStorage;

#[derive(Component, Default)]
#[storage(NullStorage)]
pub struct Letter;
10 changes: 7 additions & 3 deletions core/src/ecs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::components;
use crate::data::enemy_data::EnemyData;
use crate::data::{CloudData, GroundData, PlayerData, WorldData};
use crate::entities::{GroundEntity, IconEntity, PlayerEntity, ScoreEntity};
use crate::entities::{GroundEntity, IconEntity, LetterEntity, PlayerEntity, ScoreEntity};
use crate::resources::{EventQueue, GamePlay};
use crate::systems::{
CloudSystem, CollisionSystem, EnemySystem, EventSystem, GamePlayTickUpdater, GameSpeedUpdater,
Expand Down Expand Up @@ -43,6 +43,7 @@ impl<'a, 'b> Ecs<'a, 'b> {
world.register::<components::Ground>();
world.register::<components::Icon>();
world.register::<components::input::InputControlled>();
world.register::<components::Letter>();
world.register::<components::player::Player>();
world.register::<components::score::Score>();

Expand All @@ -59,16 +60,19 @@ impl<'a, 'b> Ecs<'a, 'b> {
}

pub fn show_instructions(&mut self) {
IconEntity::create_direction_tiles_at_world_center(&mut self.world);
// IconEntity::create_direction_tiles_at_world_center(&mut self.world);
LetterEntity::create_game_instructions_tiles_at_world_center(&mut self.world);
}

pub fn show_game_end(&mut self) {
IconEntity::create_retry_tile_at_world_center(&mut self.world);
// IconEntity::create_retry_tile_at_world_center(&mut self.world);
LetterEntity::create_retry_tiles_at_world_center(&mut self.world);
}

pub fn start_game_play(&mut self) {
// Remove everything that was added for instructional purposes
IconEntity::remove_all_tiles(&mut self.world);
LetterEntity::remove_all_tiles(&mut self.world);

// Orchestrate systems for game play
let game_play_tick_updater = "game_play_tick_updater";
Expand Down
3 changes: 3 additions & 0 deletions core/src/entities.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
mod cloud_entity;
mod enemy_entity;
mod entity_remover;
mod ground_entity;
mod icon_entity;
mod letter_entity;
mod player_entity;
mod score_entity;

pub use cloud_entity::CloudEntity;
pub use enemy_entity::EnemyEntity;
pub use ground_entity::GroundEntity;
pub use icon_entity::IconEntity;
pub use letter_entity::LetterEntity;
pub use player_entity::PlayerEntity;
pub use score_entity::ScoreEntity;
123 changes: 123 additions & 0 deletions core/src/entities/letter_entity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use crate::components::{Drawable, Letter};
use crate::graphics::data;
use crate::graphics::data::{LetterTile, Tile};
use crate::rect::Rect;
use specs::join::Join;
use specs::{Builder, World, WorldExt};

const TILE_TO_WORLD_DIVIDER: u32 = 8;
const LETTER_SPACING: u32 = 2;

pub struct LetterEntity;

impl LetterEntity {
pub fn create_game_instructions_tiles_at_world_center(world: &mut World) -> u32 {
LetterEntity::create_tiles_at_world_center(
world,
&[
LetterTile::P,
LetterTile::R,
LetterTile::E,
LetterTile::S,
LetterTile::S,
LetterTile::SPACE,
LetterTile::S,
LetterTile::P,
LetterTile::A,
LetterTile::C,
LetterTile::E,
LetterTile::SPACE,
LetterTile::T,
LetterTile::O,
LetterTile::SPACE,
LetterTile::S,
LetterTile::T,
LetterTile::A,
LetterTile::R,
LetterTile::T,
],
)
}

pub fn create_retry_tiles_at_world_center(world: &mut World) -> u32 {
LetterEntity::create_tiles_at_world_center(
world,
&[
LetterTile::P,
LetterTile::R,
LetterTile::E,
LetterTile::S,
LetterTile::S,
LetterTile::SPACE,
LetterTile::S,
LetterTile::P,
LetterTile::A,
LetterTile::C,
LetterTile::E,
LetterTile::SPACE,
LetterTile::T,
LetterTile::O,
LetterTile::SPACE,
LetterTile::P,
LetterTile::L,
LetterTile::A,
LetterTile::Y,
LetterTile::SPACE,
LetterTile::A,
LetterTile::G,
LetterTile::A,
LetterTile::I,
LetterTile::N,
]
)
}

pub fn remove_all_tiles(world: &mut World) {
super::entity_remover::remove_all_entities_matching_type::<Letter>(world);
}

fn create_tiles_at_world_center(world: &mut World, tiles: &[LetterTile]) -> u32 {
// Initially create drawables at 0, 0
let mut last_icon_x_end_at = 0;
let mut max_drawable_height = 0;
let drawables: Vec<Drawable> = tiles
.iter()
.map(|tile| {
let tile_data = data::build_tile_data(Tile::Letter { tile: *tile });
let width_in_world = tile_data.bounds_in_tile_sheet.width() / TILE_TO_WORLD_DIVIDER;
let height_in_world =
tile_data.bounds_in_tile_sheet.height() / TILE_TO_WORLD_DIVIDER;

let bounds_in_world = Rect::new(
last_icon_x_end_at + LETTER_SPACING as i32,
0,
width_in_world,
height_in_world,
);

// Update tracked measures
last_icon_x_end_at = bounds_in_world.right();
if max_drawable_height < height_in_world {
max_drawable_height = height_in_world;
}

Drawable {
tile_data,
world_bounds: bounds_in_world,
}
})
.collect();

// Compute offsets to move drawables to world center
let height_offset: i32 = -((max_drawable_height / 2) as i32);
let width_offset = -last_icon_x_end_at / 2;

// Offset drawables & create entities
for mut drawable in drawables.into_iter() {
drawable.world_bounds.offset(width_offset, height_offset);
world.create_entity().with(Letter).with(drawable).build();
}

height_offset.abs() as u32
}
}
2 changes: 2 additions & 0 deletions core/src/graphics/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub enum LetterTile {
X,
Y,
Z,
SPACE,
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
Expand Down Expand Up @@ -209,6 +210,7 @@ pub fn build_tile_data(tile: Tile) -> TileData {
LetterTile::X => Rect::new(740, 2, 80, 112),
LetterTile::Y => Rect::new(412, 2, 80, 112),
LetterTile::Z => Rect::new(330, 2, 80, 112),
LetterTile::SPACE => Rect::new(166, 246, 80, 112),
}
}

Expand Down

0 comments on commit 75868e3

Please sign in to comment.