Back to Amethyst

Create a Tile Map

book/src/tiles/create_a_tile_map.md

0.15.31.7 KB
Original Source

Create a Tile Map

With the tiles feature installed and our RenderTiles2D render pass setup, we can create a TileMap component and add it an entity. We need to have a sprite sheet loaded before the creation so this example assume a handle to a sprite sheet exists.

rust
# extern crate amethyst;
use amethyst::{
    core::{
        math::{Point3, Vector3},
        transform::Transform,
    },
    tiles::{Tile, TileMap},
};
# use amethyst::{assets::Handle, prelude::*, renderer::SpriteSheet};
# 
# #[derive(Clone, Default)]
# struct SimpleTile;
# impl Tile for SimpleTile {
#   fn sprite(&self, _coords: Point3<u32>, _: &World) -> Option<usize> {
#       Some(1)
#   }
# }

# pub fn load_sprite_sheet() -> Handle<SpriteSheet> {
#   unimplemented!();
# }

#[derive(Debug)]
struct ExampleState;

impl SimpleState for ExampleState {
    fn on_start(&mut self, mut data: StateData<'_, GameData>) {
#       let world = data.world;
        let sprite_sheet_handle = load_sprite_sheet();
        // ...
        init_map(world, sprite_sheet_handle.clone())
    }
}

fn init_map(world: &mut World, sprite_sheet_handle: Handle<SpriteSheet>) {
    let map = TileMap::<SimpleTile>::new(
        Vector3::new(10, 10, 1), // The dimensions of the map
        Vector3::new(16, 16, 1), // The dimensions of each tile
        Some(sprite_sheet_handle),
    );
    let transform = Transform::default();

    world.push((map, transform));
}
# fn main() {}

The tile map component was created and added to the entity we created and thats it! Check out the tiles example in the examples directory.