This is the second day of my participation in the August Text Challenge.More challenges in August

create

Those who are not at all familiar with Rust are not suitable to begin reading this article. Please leave now and begin your rust learning. Then come back to this article.

As usual, start creating project:

> cargo new dungeon_ecs > exa -- tree. ├ ─ ─ cargo. Toml └ ─ ─ the SRC └ ─ ─ main. RsCopy the code

Import Legion Crate:

[dependencies] bracket-lib = "= "legion = "="Copy the code
  1. =versionFlicker locks version during development. Because Rust Crate updates fairly quickly, API incompatibility may occur during development.
  2. bracket-libTail Rust terminal GUI Lib Use it as a game GUI

Prelude

mod prelude { pub use bracket_lib::prelude::*; pub use legion::*; pub use legion::world::SubWorld; pub use legion::systems::CommandBuffer; . }Copy the code

Prelude is a preimport that can actually be seen in many libraries. The essence is also to reduce module import code in main.rs: make common modules globally shareable.

Create World

Legion stores all entities and components in a structure it calls World. The game needs to create a world in which to place the game’s entities.

So State in SRC /main.rs contains a World and a Schedule to hold the system:

struct State {
    ecs: World,
    resources: Resources,
    systems: Schedule,
}
Copy the code

Initialize this State:

impl State { fn new() -> Self { let mut ecs = World::default(); let mut resources = Resources::default(); let mut rng = RandomNumberGenerator::new(); // TODO resources < -map // Map is injected into resources State {ecs, resources,}}Copy the code

World and Resources are created with a simple constructor called Default. It’s essentially an empty constructor, so you can think of it this way.

We currently lack a Map. Inject this into resources and make it public


Now that the ECS is in place, we need to start building the first component → Player and then inject it into the ECS.

Player Component

Rather than coding everything relevant to the player into a single module, ECS describes the player in terms of the components the player uses.

  • How does a player appear on a screen
  • Position Denotes the Position of an object on a map.bracket-libIn thePoint structVery suitable for this; It contains an X vector and a y vector, and it also provides several mathematical functions related to points.

OK, create a new file: SRC /components.rs

use crate::prelude::*;

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Render {
    pub color: ColorPair,
    pub glyph: FontCharType,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Player;
Copy the code
  1. use crate::prelude::*;You don’t have to refer to some function variable in the native library
  2. PlayerIs an empty structure that contains no data and acts as a marker indicating that the entity with the component is a player.

Then you need to use SRC /main.rs:

mod prelude {
    pub use bracket_lib::prelude::*;
    pub use legion::*;
    pub use legion::world::SubWorld;
    pub use legion::systems::CommandBuffer;
    pub use crate::components::*;
}
mod components;

use prelude::*;
Copy the code