Skip to content

Commit

Permalink
feat: initial implementation of legion ecs (#4)
Browse files Browse the repository at this point in the history
* chore: refactor method locations

* feat: implement velocity based collision on player

* feat: support non-square maps

* feat: update door icon

* feat: initial implementation of legion ecs
  • Loading branch information
pbellchambers authored Feb 6, 2021
1 parent 56e1954 commit 2df6899
Show file tree
Hide file tree
Showing 16 changed files with 627 additions and 245 deletions.
321 changes: 316 additions & 5 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ edition = "2018"
log = "0.4.14"
simplelog = "0.9.0"
console_engine = "1.4.2"
uuid = { version = "0.8.2", features = ["v4"] }
uuid = { version = "0.8.2", features = ["v4"] }
legion = "0.3.1"
38 changes: 23 additions & 15 deletions assets/maps/default.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
########################################################################################################################
# #
# * #
# #
# #
# #
# #
# #
# #
# |--------| #
# | , | * #
# | | #
# |---+----| #
#####################################################################################################################
# | #
#* | #
# | #
# | #
# |----------/#
# * #
# #
# #
# |--------| #
# | , | * #
# | | #
# |---/----| #
# #
# #
# #
# #
# #
# |-----------| #
# | | #
# | | #
# | | #
# |-----x-----| #
# |-----+-----| #
# #
# #
# #
# * #
# #
# #
########################################################################################################################%
############################################ ####################################################
########################






%
50 changes: 20 additions & 30 deletions src/world_map.rs → src/background_map.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,52 @@
mod character_map;
use crate::entity::Entity;
pub(crate) mod tiles;

use crate::background_map::tiles::Tile;
use std::{env, fs, process};

pub struct WorldMap {
data: Vec<Vec<Entity>>,
boundary_x: u32,
boundary_y: u32,
pub struct BackgroundMap {
data: Vec<Vec<Tile>>,
}

impl WorldMap {
pub fn new(filename: String) -> WorldMap {
impl BackgroundMap {
pub fn new(filename: String) -> BackgroundMap {
initialise_map(filename)
}

pub fn data(&self) -> &Vec<Vec<Entity>> {
pub fn data(&self) -> &Vec<Vec<Tile>> {
&self.data
}

pub fn boundary_x(&self) -> &u32 {
&self.boundary_x
}

pub fn boundary_y(&self) -> &u32 {
&self.boundary_y
pub fn get_tile_at(&self, x: usize, y: usize) -> Tile {
self.data[y][x]
}
}

fn initialise_map(filename: String) -> WorldMap {
fn initialise_map(filename: String) -> BackgroundMap {
let unprocessed_map_data = load_map_data_from_file(filename);
let data = process_map_data(&unprocessed_map_data);
let boundary_x = data[0].len() as u32;
let boundary_y = data.len() as u32;
WorldMap {
data,
boundary_x,
boundary_y,
}
BackgroundMap { data }
}

fn process_map_data(data: &str) -> Vec<Vec<Entity>> {
let mut processed_data: Vec<Vec<Entity>> = Vec::new();
let mut row_data: Vec<Entity> = Vec::new();
let mut entity: Entity;
fn process_map_data(data: &str) -> Vec<Vec<Tile>> {
let mut processed_data: Vec<Vec<Tile>> = Vec::new();
let mut row_data: Vec<Tile> = Vec::new();
let mut entity: Tile;
let mut current_x = 0;
let mut current_y = 0;
for character in data.chars() {
entity = character_map::map_character_to_entity(current_x, current_y, character);
entity = character_map::map_character_to_tile(current_x, current_y, character);
match entity {
Entity::NewLine => {
Tile::NewLine => {
processed_data.push(row_data.clone());
row_data.clear();
current_x = 0;
current_y += 1;
}
Entity::CarriageReturn => {
Tile::CarriageReturn => {
//do nothing - handles builds on windows
}
Entity::EndOfFile => processed_data.push(row_data.clone()),
Tile::EndOfFile => processed_data.push(row_data.clone()),
_ => {
row_data.push(entity);
current_x += 1;
Expand Down
20 changes: 20 additions & 0 deletions src/background_map/character_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::background_map::tiles::door::Door;
use crate::background_map::tiles::wall::Wall;
use crate::background_map::tiles::{OpenState, Tile};

pub fn map_character_to_tile(x: i32, y: i32, character: char) -> Tile {
match character {
'\n' => Tile::NewLine,
'\r' => Tile::CarriageReturn,
'%' => Tile::EndOfFile,
'#' => Tile::Boundary,
' ' => Tile::EmptySpace,
'|' => Tile::Wall(Wall::new(x, y, character)),
'-' => Tile::Wall(Wall::new(x, y, character)),
'+' => Tile::Door(Door::new(x, y, OpenState::Closed)),
'/' => Tile::Door(Door::new(x, y, OpenState::Open)),
',' => Tile::Wall(Wall::new(x, y, character)),
'*' => Tile::Wall(Wall::new(x, y, character)),
_ => Tile::EmptySpace,
}
}
48 changes: 48 additions & 0 deletions src/background_map/tiles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
pub(crate) mod door;
pub(crate) mod wall;

use crate::background_map::tiles::door::Door;
use crate::background_map::tiles::wall::Wall;

#[derive(Clone, Copy, PartialEq)]
pub enum Collidable {
True,
False,
}

#[derive(Clone, Copy)]
pub enum OpenState {
Open,
Closed,
}

#[derive(Clone, Copy)]
pub struct Location {
pub x: i32,
pub y: i32,
}

#[derive(Clone, Copy)]
pub enum Tile {
Wall(Wall),
Door(Door),
EmptySpace,
Boundary,
NewLine,
CarriageReturn,
EndOfFile,
}

impl Tile {
pub fn character(&self) -> char {
match self {
Tile::Wall(wall) => wall.character_icon,
Tile::Door(door) => door.character_icon,
Tile::EmptySpace => ' ',
Tile::Boundary => '#',
Tile::NewLine => ' ',
Tile::CarriageReturn => ' ',
Tile::EndOfFile => ' ',
}
}
}
6 changes: 3 additions & 3 deletions src/entity/door.rs → src/background_map/tiles/door.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::entity::{Collidable, Location, OpenState};
use crate::background_map::tiles::{Collidable, Location, OpenState};

#[derive(Clone, Copy)]
pub struct Door {
Expand All @@ -13,8 +13,8 @@ impl Door {
Door {
location: Location { x, y },
character_icon: match open_state {
OpenState::Open => '+',
OpenState::Closed => 'x',
OpenState::Open => '/',
OpenState::Closed => '+',
},
collidable: match open_state {
OpenState::Open => Collidable::False,
Expand Down
2 changes: 1 addition & 1 deletion src/entity/wall.rs → src/background_map/tiles/wall.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::entity::{Collidable, Location};
use crate::background_map::tiles::{Collidable, Location};

#[derive(Clone, Copy)]
pub struct Wall {
Expand Down
1 change: 1 addition & 0 deletions src/ecs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod components;
49 changes: 49 additions & 0 deletions src/ecs/components.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use console_engine::Color;
use uuid::Uuid;

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Position {
pub x: i32,
pub y: i32,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Velocity {
pub x: i32,
pub y: i32,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct OpenState {
pub open: bool,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CollisionState {
pub collidable: bool,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Character {
pub icon: char,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct EntityColour {
pub colour: Color,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct IsPlayer {
pub is_player: bool,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct VisibleState {
pub visible: bool,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PlayerId {
pub uuid: Uuid,
}
Loading

0 comments on commit 2df6899

Please sign in to comment.