Storing the Dungeon Map

Hands-on Rust — by Herbert Wolverson (45 / 120)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Organizing Your Imports With a Prelude | TOC | Adding the Adventurer 👉

Most games include a map, typically an array of tiles. For a dungeon crawler game, the map structure represents the dungeon layout. Platform games use maps to represent the location of ledges, platforms, and ladders. A Minesweeper game represents the discovered areas and the positions of mines on the map. Most two-dimensional games represent their map as a series of tiles in a grid pattern. Each tile has a type describing how that tile is rendered — and what happens if you try to enter it.

You’ll represent your map as a vector. Each entry in the vector represents one tile, so for an 80x50 map you will have 4,000 tiles. Tiles represent portions of the map, and the same tile graphic is re-used to represent tiles of a particular type. Entities — such as the player or any monsters — are overlaid on top:

images/BasicDungeonCrawler/MapGrid.png

Represent Your Tiles

Tiles are limited to a pre-defined set of tile types, making them perfect for an enum. In map.rs, define a public enumeration named TileType with entries for walls and floors:

BasicDungeonCrawler/dungeon_crawl_map/src/map.rs

​ ​#​[​derive​(Copy, Clone, PartialEq)]
​…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.