#![allow(unused)]fnmain() {
// events.rsuse hecs::Entity;
#[derive(Debug)]pubstructEntityMoved {
pub entity: Entity,
}
#[allow(dead_code)]#[derive(Debug)]pubstructBoxPlacedOnSpot {
pub is_correct_spot: bool,
}
#[derive(Debug)]pubenumEvent {
// Fired when the player hits an obstacle like a wall
PlayerHitObstacle,
// Fired when an entity is moved
EntityMoved(EntityMoved),
// Fired when the box is placed on a spot
BoxPlacedOnSpot(BoxPlacedOnSpot),
}
}
#![allow(unused)]fnmain() {
// input.rspubfnrun_input(world: &World, context: &mut Context) {
letmut to_move: Vec<(Entity, KeyCode)> = Vec::new();
letmut events = Vec::new();
/// Code omitted/// ....../// ......// find a movable// if it exists, we try to move it and continue// if it doesn't exist, we continue and try to find an immovable insteadmatch mov.get(&pos) {
Some(entity) => to_move.push((*entity, key)),
None => {
// find an immovable// if it exists, we need to stop and not move anything// if it doesn't exist, we stop because we found a gapmatch immov.get(&pos) {
Some(_id) => {
to_move.clear();
events.push(Event::PlayerHitObstacle {});
break;
}
None => break,
}
}
}
/// Code omitted/// ....../// ....../// // Now actually move what needs to be movedfor (entity, key) in to_move {
letmut position = world.get::<&mut Position>(entity).unwrap();
match key {
KeyCode::Up => position.y -= 1,
KeyCode::Down => position.y += 1,
KeyCode::Left => position.x -= 1,
KeyCode::Right => position.x += 1,
_ => (),
}
// Fire an event for the entity that just moved
events.push(Event::EntityMoved(EntityMoved { entity }));
}
}
#![allow(unused)]fnmain() {
// systems/events.rsuse crate::components::*;
use crate::events::*;
use hecs::World;
use std::collections::HashMap;
pubfnrun_process_events(world: &mut World) {
let events = {
letmut query = world.query::<&mut crate::components::EventQueue>();
let events = query
.iter()
.next()
.unwrap()
.1
.events
.drain(..)
.collect::<Vec<_>>();
events
};
letmut new_events = Vec::new();
letmut query = world.query::<(&Position, &BoxSpot)>();
let box_spots_by_position: HashMap<(u8, u8), &BoxSpot> = query
.iter()
.map(|(_, t)| ((t.0.x, t.0.y), t.1))
.collect::<HashMap<_, _>>();
for event in events {
println!("New event: {:?}", event);
match event {
Event::PlayerHitObstacle => {
// play sound here
}
Event::EntityMoved(EntityMoved { entity }) => {
// An entity was just moved, check if it was a box and fire// more events if it's been moved on a spot.ifletOk(the_box) = world.get::<&Box>(entity) {
ifletOk(box_position) = world.get::<&Position>(entity) {
// Check if there is a spot on this position, and if there// is if it's the correct or incorrect typeifletSome(box_spot) =
box_spots_by_position.get(&(box_position.x, box_position.y))
{
new_events.push(Event::BoxPlacedOnSpot(BoxPlacedOnSpot {
is_correct_spot: (box_spot.colour == the_box.colour),
}));
}
}
}
}
Event::BoxPlacedOnSpot(BoxPlacedOnSpot { is_correct_spot: _ }) => {
// play sound here
}
}
}
// Finally add events back into the world
{
letmut query = world.query::<&mut EventQueue>();
let event_queue = query.iter().next().unwrap().1;
event_queue.events.append(&mut new_events);
}
}
}