Collision Detection
Hands-on Rust — by Herbert Wolverson (57 / 120)
👈 Adding Monsters | TOC | Wrap-Up 👉
If you run around the map, you’ll notice that nothing happens when you hit a monster. Let’s change that by adding collision detection. You’ll write a combat system in Chapter 8, Health and Melee Combat — for now, walking into a monster will remove it from the dungeon.
Collision detection will be its own system. Add a new file to your project, src/systems/collisions.rs. Don’t forget to add mod collisions; to your src/systems/mod.rs file.
Start collisions.rs with a similar pattern:
EntitiesComponentsAndSystems/dungeonecs/src/systems/collisions.rs
use crate::prelude::*;
#[system]
①#[read_component(Point)]
#[read_component(Player)]
#[read_component(Enemy)]
②pub fn collisions(ecs: &mut SubWorld, commands: &mut CommandBuffer) {
①
This system requires access to Point, Player, and Enemy.
②
Legion can give your system a CommandBuffer. This is a special container to insert instructions for Legion to perform after the system is finished. You’ll use the command buffer to remove entities from the game.
Next, create a variable named player_pos to store the player’s position. You then create the same players query you used in player_input to find just the player’s position. You…