Skip to content

Commit

Permalink
finish map v2
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyTurtleDev committed May 12, 2023
1 parent 7f9e349 commit 42da102
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
2 changes: 2 additions & 0 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub fn include_map(input: TokenStream) -> TokenStream {
{
use m3_map::Map;
use m3_map::tiles::MapBaseTile;
use m3_map::Player;
use m3_map::tiles::ObjectTile;
// include the bytes so that the compiler knows to recompile when the
// map or tilesets changes
const _: &[u8] = ::core::include_bytes!(#path);
Expand Down
14 changes: 7 additions & 7 deletions map/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use thiserror::Error;
use tiled::{LayerType, Loader};

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

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

#[derive(Clone, Debug, SelfRustTokenize)]
Expand Down Expand Up @@ -180,16 +180,16 @@ impl Map {

/// return an iterator over all static Tiles and its x and y postion.
/// starting from the lowest layer
pub fn iter_all(&self) -> impl Iterator<Item = (u8, u8, MapTiles)> + '_ {
pub fn iter_all(&self) -> impl Iterator<Item = (u8, u8, Tile)> + '_ {
let base = self
.iter_base_layer()
.map(|(x, y, tile)| (x, y, MapTiles::MapBaseTile(tile.to_owned())));
.map(|(x, y, tile)| (x, y, Tile::MapBaseTile(tile.to_owned())));
let objects = self
.iter_object_layer()
.map(|(x, y, tile)| (x, y, MapTiles::MapObjectTile(tile.to_owned())));
.map(|(x, y, tile)| (x, y, Tile::MapObjectTile(tile.to_owned())));
let goals = self
.iter_player_goals()
.map(|(x, y, tile)| (x, y, MapTiles::PlayerTile(tile)));
.map(|(x, y, tile)| (x, y, Tile::PlayerTile(tile)));
base.chain(objects).chain(goals)
}
}
2 changes: 1 addition & 1 deletion map/src/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use num_enum::TryFromPrimitive;
use self_rust_tokenize::SelfRustTokenize;
use thiserror::Error;

pub enum MapTiles {
pub enum Tile {
MapBaseTile(MapBaseTile),
MapObjectTile(ObjectTile),
PlayerTile(PlayerTile)
Expand Down
2 changes: 1 addition & 1 deletion pc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl State for GameState {
(window_size.0 / map.width as i32) as f32,
(window_size.1 / map.height as i32) as f32
);
for (x, y, tile) in map.iter_base_layer() {
for (x, y, tile) in map.iter_all() {
let texture = tile.texture(&self.textures);
texture.draw(
ctx,
Expand Down
24 changes: 16 additions & 8 deletions pc/src/tiles.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::GetTexture;
pub use m3_map::tiles::MapBaseTile;
use m3_map::tiles::{ObjectTile, PlayerTile, Tiles};
use m3_map::tiles::{ObjectTile, PlayerTile, Tile};
use tetra::{graphics::Texture, Context};

///Store all Textures
Expand All @@ -11,7 +11,7 @@ pub struct Textures {
player2_car: Texture,
player3_car: Texture,
player4_car: Texture,
global_goal: Texture,
global_goal: Texture
}

impl Textures {
Expand All @@ -31,15 +31,18 @@ impl Textures {
player1_car: Texture::from_encoded(
ctx,
include_bytes!("../assets/img/Player/player1_car.png")
).unwrap(),
)
.unwrap(),
player2_car: Texture::from_encoded(
ctx,
include_bytes!("../assets/img/Player/player2_car.png")
).unwrap(),
)
.unwrap(),
player3_car: Texture::from_encoded(
ctx,
include_bytes!("../assets/img/Player/player3_car.png")
).unwrap(),
)
.unwrap(),
player4_car: Texture::from_encoded(
ctx,
include_bytes!("../assets/img/Player/player4_car.png")
Expand All @@ -50,7 +53,6 @@ impl Textures {
include_bytes!("../assets/img/Player/goal.png")
)
.unwrap()

}
}
}
Expand Down Expand Up @@ -81,13 +83,19 @@ impl<'a> GetTexture<'a> for PlayerTile {
Self::Car2 => &textures.player1_car,
Self::Car3 => &textures.player1_car,
Self::Car4 => &textures.player1_car,
Self::GlobalGoal => &textures.global_goal,
Self::GlobalGoal => &textures.global_goal
}
}
}

impl<'a> GetTexture<'a> for Tile {
match Tile
fn texture(&self, textures: &'a Textures) -> &'a Texture {
match self {
Tile::MapBaseTile(tile) => tile.texture(textures),
Tile::MapObjectTile(tile) => tile.texture(textures),
Tile::PlayerTile(tile) => tile.texture(textures)
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit 42da102

Please sign in to comment.