...

/

Finding the Amulet of Yala

Finding the Amulet of Yala

In this lesson, we’ll place the Amulet. If the player finds the amulet, then the player will win.

The player wins the game when they collect the Amulet of Yala. “Yala” is an acronym for yet another lost amulet, a gentle poke at the plot of Nethack and its many derivatives.

Spawning the amulet

We’ll use two new tag components to denote the amulet:

Press + to interact
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Item;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AmuletOfYala;

The Item component denotes that an entity is an item. It doesn’t move or have health, but it should still appear on the map. We’ll add further support for items in the Inventory and Power Ups section. The AmuletOfYala tag indicates that the tagged item is the item that wins the game.

The amulet also shares components with other entities. It needs a name, a rendered graphic, and a map position. Add a function in spawner.rs to create the amulet:

Press + to interact
pub fn spawn_amulet_of_yala(ecs : &mut World, pos : Point) {
ecs.push(
(Item, AmuletOfYala,
pos,
Render{
color: ColorPair::new(WHITE, BLACK),
glyph : to_cp437('|')
},
Name("Amulet of Yala".to_string())
)
);
}

This is very similar to your other spawning functions. It creates an entity, tagged as an Item and the AmuletOfYala. It creates components containing a position, renders information, and a name. The amulet is defined in dungeon-font.png as the | glyph.

Now that we can spawn the amulet, we have to decide where to put it.

Placing the amulet

We want the amulet to be difficult to reach, otherwise, the game may be too easy. This lends itself to another use for Dijkstra maps: determining the most distant tile on the map.

Add a second Point to the MapBuilder type in map_builder.rs, storing the amulet’s destination:

Press + to interact
pub struct MapBuilder {
pub map : Map,
pub rooms : Vec<Rect>,
pub player_start : Point,
pub amulet_start : Point
}

We also need to initialize the ...