Skip to content

Commit

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

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

#[derive(Clone, Debug, SelfRustTokenize)]
pub struct Player {
start: (u8, u8),
goal: Option<(u8, u8)>
}

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

#[derive(Error, Debug)]
Expand Down Expand Up @@ -40,6 +51,11 @@ impl Map {
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);
let mut global_goal = None;
let mut player_1 = None;
let mut player_2 = None;
let mut player_3 = None;
let mut player_4 = None;
for (i, layer) in map.layers().enumerate() {
match i {
0 => match layer.layer_type() {
Expand Down Expand Up @@ -74,14 +90,62 @@ impl Map {
},
_ => return Err(MapError::WrongLayer(i, "TileLayer".to_owned()))
},
2 => match layer.layer_type() {
LayerType::Tiles(tile_layer) => {
for x in 0..width {
for y in 0..height {
if let Some(tile) =
tile_layer.get_tile(x.into(), y.into())
{
let tile = PlayerTile::try_from(tile.id())?;
match tile {
PlayerTile::Car1 => {
player_1 = Some(Player {
start: (x, y),
goal: None
})
},
PlayerTile::Car2 => {
player_2 = Some(Player {
start: (x, y),
goal: None
})
},
PlayerTile::Car3 => {
player_3 = Some(Player {
start: (x, y),
goal: None
})
},
PlayerTile::Car4 => {
player_4 = Some(Player {
start: (x, y),
goal: None
})
},
PlayerTile::GlobalGoal => {
global_goal = Some((x, y))
},
}
}
}
}
},
_ => return Err(MapError::WrongLayer(i, "TileLayer".to_owned()))
},
_ => return Err(MapError::ToManyLayers)
}
}
Ok(Map {
width,
height,
base_layer,
object_layer
object_layer,
global_goal,
player_1,
player_2,
player_3,
player_4
})
}

Expand Down
4 changes: 2 additions & 2 deletions map/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ pub struct Opt {

fn main() {
let opt = Opt::parse();
let result = Map::from_tmx(&opt.file);
let result = Map::from_tmx(opt.file);
match result {
Err(err) => {
eprintln!("ERROR: {err}");
std::process::exit(1);
},
Ok(map) => println!("{map:#?}")
Ok(map) => println!("{map:#?}\nmap is valid")
}
}
25 changes: 24 additions & 1 deletion map/src/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl TryFrom<u32> for MapBaseTile {
#[repr(u8)]
pub enum ObjectTile {
//numbers must match them from the Tiled tilesets
Stone = 0
Stone = 1
}

impl TryFrom<u32> for ObjectTile {
Expand All @@ -46,3 +46,26 @@ impl TryFrom<u32> for ObjectTile {
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 PlayerTile {
//numbers must match them from the Tiled tilesets
Car1 = 0,
Car2 = 1,
Car3 = 2,
Car4 = 3,
//goal, which can be used by all players
GlobalGoal = 4
}

impl TryFrom<u32> for PlayerTile {
type Error = InvalidTileID;
fn try_from(value: u32) -> Result<PlayerTile, 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))
}
}
7 changes: 6 additions & 1 deletion pc/assets/img/ObjectTiles/ObjectTiles.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
<?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"/>
<tileset version="1.10" tiledversion="1.10.1" name="ObjectTiles" tilewidth="256" tileheight="256" tilecount="1" columns="0">
<grid orientation="orthogonal" width="1" height="1"/>
<tile id="1">
<image width="256" height="256" source="stone.png"/>
</tile>
</tileset>
19 changes: 19 additions & 0 deletions pc/assets/img/Player/Player.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.10.1" name="Player" tilewidth="256" tileheight="256" tilecount="5" columns="0">
<grid orientation="orthogonal" width="1" height="1"/>
<tile id="0">
<image width="256" height="256" source="player1_car.png"/>
</tile>
<tile id="1">
<image width="256" height="256" source="player2_car.png"/>
</tile>
<tile id="2">
<image width="256" height="256" source="player3_car.png"/>
</tile>
<tile id="3">
<image width="256" height="256" source="player4_car.png"/>
</tile>
<tile id="4">
<image width="256" height="256" source="goal.png"/>
</tile>
</tileset>
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Binary file removed pc/assets/img/player1_car.png
Binary file not shown.
22 changes: 18 additions & 4 deletions pc/assets/level/001.tmx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?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="3" 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="4" nextobjectid="1">
<tileset firstgid="1" source="../img/BaseTiles/BaseTiles.tsx"/>
<tileset firstgid="2" source="../img/ObjectTiles/ObjectTiles.tsx"/>
<tileset firstgid="4" source="../img/Player/Player.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,
Expand All @@ -19,12 +20,25 @@
<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,3,0,0,0,
0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,3,0,0,3,3,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,3,3,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,0
</data>
</layer>
<layer id="3" name="Player" 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,5,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,6,0,0,
0,0,0,0,0,0,0,0,8,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,4,0,0,0,0,0,0,0,0,0,0,7,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
</layer>
Expand Down
2 changes: 1 addition & 1 deletion pc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use tetra::{
Context, ContextBuilder, State
};
type Vec2 = vek::vec::repr_c::vec2::Vec2<f32>;
use log::info;
use m3_macro::include_map;
use m3_map::Map;
use once_cell::sync::Lazy;
use tetra::{
graphics::{DrawParams, Texture},
time::get_delta_time
};
use log::{info};

mod tiles;
use tiles::Textures;
Expand Down

0 comments on commit 8eb4421

Please sign in to comment.