forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeath.rs
More file actions
60 lines (51 loc) · 1.79 KB
/
Copy pathdeath.rs
File metadata and controls
60 lines (51 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! Handles when a player dies.
//!
//! Player deaths have some annoying properties which makes a correct implementation difficult.
//! Most notably, a dead player (one who is currently on the respawn screen) has no physical
//! presence within the world, despite them still being connected to the server and existing
//! in that sense.
use entity::drops::drop_item;
use feather_core::util::Position;
use feather_server_types::{Dead, EntityDeathEvent, Game, Inventory, InventoryUpdateEvent, Player};
use fecs::World;
/// Scatters a player's items when they die.
#[fecs::event_handler]
pub fn on_player_death_scatter_inventory(
event: &EntityDeathEvent,
game: &mut Game,
world: &mut World,
) {
if !world.has::<Player>(event.entity) {
return;
}
let inventory = world.get::<Inventory>(event.entity);
let pos = *world.get::<Position>(event.entity);
// Remove items and drop on ground
let slots_to_update = inventory
.enumerate()
.filter_map(|(index, slot)| slot.map(|_| index));
let event = InventoryUpdateEvent {
entity: event.entity,
slots: slots_to_update.collect(),
};
let items_to_spawn = inventory
.iter_mut()
.filter_map(|mut item| item.take())
.collect::<Vec<_>>();
drop(inventory);
for item in items_to_spawn {
drop_item(game, world, item, pos);
}
game.handle(world, event);
}
/// Adds the `Dead` component to a player when they die to
/// avoid causing them to be physically interacted with.
///
/// The component will be removed once the user clicks the respawn
/// button.
#[fecs::event_handler]
pub fn on_player_death_mark_dead(event: &EntityDeathEvent, world: &mut World) {
if world.has::<Player>(event.entity) {
world.add(event.entity, Dead).unwrap();
}
}