Skip to content

Commit

Permalink
start object layer
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyTurtleDev committed May 12, 2023
1 parent 3cb7aa3 commit 174df93
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 21 deletions.
36 changes: 33 additions & 3 deletions map/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use thiserror::Error;
use tiled::{LayerType, Loader};

pub mod tiles;
use tiles::{InvalidTileID, MapBaseTile};
use tiles::{InvalidTileID, MapBaseTile, ObjectTile};

#[derive(Clone, Debug, SelfRustTokenize)]
pub struct Map {
pub width: u8,
pub height: u8,
pub base_layer: Vec<Vec<MapBaseTile>>
pub base_layer: Vec<Vec<MapBaseTile>>,
pub object_layer: Vec<Vec<Option<ObjectTile>>>
}

#[derive(Error, Debug)]
Expand All @@ -32,11 +33,13 @@ pub enum MapError {
}

impl Map {
// this is ugly. Should i refactor this?
pub fn from_tmx(path: impl AsRef<Path>) -> Result<Self, MapError> {
let map = Loader::new().load_tmx_map(path)?;
let width: u8 = map.width.try_into().map_err(|_| MapError::ToWidth)?;
let height: u8 = map.height.try_into().map_err(|_| MapError::ToHight)?;
let mut base_layer = Vec::with_capacity(height as usize);
let mut object_layer = Vec::with_capacity(height as usize);
for (i, layer) in map.layers().enumerate() {
match i {
0 => match layer.layer_type() {
Expand All @@ -55,13 +58,30 @@ impl Map {
},
_ => return Err(MapError::WrongLayer(i, "TileLayer".to_owned()))
},
1 => match layer.layer_type() {
LayerType::Tiles(tile_layer) => {
for x in 0..width {
let mut column = Vec::with_capacity(width as usize);
for y in 0..height {
let tile = match tile_layer.get_tile(x.into(), y.into()) {
Some(tile) => Some(ObjectTile::try_from(tile.id())?),
None => None
};
column.push(tile);
}
object_layer.push(column);
}
},
_ => return Err(MapError::WrongLayer(i, "TileLayer".to_owned()))
},
_ => return Err(MapError::ToManyLayers)
}
}
Ok(Map {
width,
height,
base_layer
base_layer,
object_layer
})
}

Expand All @@ -74,4 +94,14 @@ impl Map {
.map(move |(y, item)| (x as u8, y as u8, item))
})
}

/// return an iterator over all ObjectTiles and its x and y postion
pub fn iter_object_layer(&self) -> impl Iterator<Item = (u8, u8, ObjectTile)> + '_ {
self.object_layer.iter().enumerate().flat_map(|(x, y_vec)| {
y_vec
.iter()
.enumerate()
.filter_map(move |(y, item)| item.map(|item| (x as u8, y as u8, item)))
})
}
}
33 changes: 25 additions & 8 deletions map/src/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ use num_enum::TryFromPrimitive;
use self_rust_tokenize::SelfRustTokenize;
use thiserror::Error;

#[derive(Debug, Copy, Clone, Error)]
pub enum InvalidTileID {
#[error("invalid tiel id {0}")]
InvalidId(u32)
}

///Store all Tiles, with can be used at the map background
#[derive(
Clone, Copy, Debug, Default, Eq, SelfRustTokenize, PartialEq, TryFromPrimitive,
Expand All @@ -10,14 +16,7 @@ use thiserror::Error;
pub enum MapBaseTile {
//numbers must match them from the Tiled tilesets
#[default]
Grass = 0,
Stone = 1
}

#[derive(Debug, Copy, Clone, Error)]
pub enum InvalidTileID {
#[error("invalid tiel id {0}")]
InvalidId(u32)
Grass = 0
}

impl TryFrom<u32> for MapBaseTile {
Expand All @@ -29,3 +28,21 @@ impl TryFrom<u32> for MapBaseTile {
Self::try_from_primitive(value_u8).map_err(|_| Self::Error::InvalidId(value))
}
}

///Store all Tiles, with can be place the layer above the background
#[derive(Clone, Copy, Debug, Eq, SelfRustTokenize, PartialEq, TryFromPrimitive)]
#[repr(u8)]
pub enum ObjectTile {
//numbers must match them from the Tiled tilesets
Stone = 0
}

impl TryFrom<u32> for ObjectTile {
type Error = InvalidTileID;
fn try_from(value: u32) -> Result<ObjectTile, Self::Error> {
let value_u8: u8 = value
.try_into()
.map_err(|_| Self::Error::InvalidId(value))?;
Self::try_from_primitive(value_u8).map_err(|_| Self::Error::InvalidId(value))
}
}
5 changes: 1 addition & 4 deletions pc/assets/img/BaseTiles/BaseTiles.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.10.1" name="BaseTiles" tilewidth="256" tileheight="256" tilecount="2" columns="0">
<tileset version="1.10" tiledversion="1.10.1" name="BaseTiles" tilewidth="256" tileheight="256" tilecount="1" columns="0">
<grid orientation="orthogonal" width="1" height="1"/>
<tile id="0">
<image width="256" height="256" source="grass.png"/>
</tile>
<tile id="1">
<image width="256" height="256" source="stone.png"/>
</tile>
</tileset>
Binary file removed pc/assets/img/BaseTiles/Grass mit Rand.png
Binary file not shown.
Binary file removed pc/assets/img/BaseTiles/Grass mit Rand2.png
Binary file not shown.
Binary file modified pc/assets/img/BaseTiles/grass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed pc/assets/img/BaseTiles/stone.png
Binary file not shown.
2 changes: 2 additions & 0 deletions pc/assets/img/ObjectTiles/ObjectTiles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.10.1" name="ObjectTiles" tilewidth="1" tileheight="1" tilecount="0" columns="0"/>
File renamed without changes
26 changes: 20 additions & 6 deletions pc/assets/level/001.tmx
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.1" orientation="orthogonal" renderorder="right-down" width="16" height="9" tilewidth="256" tileheight="256" infinite="0" nextlayerid="2" nextobjectid="1">
<map version="1.10" tiledversion="1.10.1" orientation="orthogonal" renderorder="right-down" width="16" height="9" tilewidth="256" tileheight="256" infinite="0" nextlayerid="3" nextobjectid="1">
<tileset firstgid="1" source="../img/BaseTiles/BaseTiles.tsx"/>
<layer id="1" name="Tile Layer 1" width="16" height="9">
<tileset firstgid="2" source="../img/ObjectTiles/ObjectTiles.tsx"/>
<layer id="1" name="Base" width="16" height="9">
<data encoding="csv">
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,2,2,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
</data>
</layer>
<layer id="2" name="Objects" width="16" height="9">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,2,2,0,2,0,0,0,0,
0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
</layer>
</map>

0 comments on commit 174df93

Please sign in to comment.