Managing Monsters
Learn how to manage monsters.
Naming monsters
The HUD provides a golden opportunity to tell the player about the entities on the map via tooltips. Tooltips are text that pops up when you hover the mouse over an icon.
Names are another component type. Add the following code to components.rs
:
#[derive(Clone, PartialEq)]pub struct Name(pub String);
Notice that the syntax is different here. Structs can also be tuples. If we only have one piece of information to store inside a struct, we can replace the named fields with (pub type)
, a string in this case. We can then access the contents of the structure just like a tuple—mystruct.0
contains the string. This can be a convenient shorthand when our structure only contains one or two items but it’s less readable. An in-development (at the time of writing) extension to Rust will allow us to mark single entry structures as “transparent” and directly access the contents.
Having created a name component, let’s add it to monsters along with some health.
Giving monsters names and hit points
Currently, monsters only vary by the glyph they’re assigned. As the game grows in complexity, we want more monster variety. For now, we’ll vary their names and hit points.
Now, we create a new function,
goblin()
, in src/spawner.rs
:
fn goblin() -> (i32, String, FontCharType) {(1, "Goblin".to_string(), to_cp437('g'))}
This is a straightforward function. ...