-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathevents.rs
More file actions
60 lines (51 loc) · 1.63 KB
/
events.rs
File metadata and controls
60 lines (51 loc) · 1.63 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
use base::{ChunkHandle, ChunkPosition};
use crate::view::View;
mod block_change;
mod plugin_message;
pub use block_change::BlockChangeEvent;
pub use plugin_message::PluginMessageEvent;
/// Event triggered when a player changes their `View`,
/// meaning they crossed into a new chunk.
#[derive(Debug)]
pub struct ViewUpdateEvent {
pub old_view: View,
pub new_view: View,
/// Chunks that are in `new_view` but not `old_view`
pub new_chunks: Vec<ChunkPosition>,
/// Chunks that are in `old_view` but not in `new_view`
pub old_chunks: Vec<ChunkPosition>,
}
impl ViewUpdateEvent {
pub fn new(old_view: View, new_view: View) -> Self {
let mut this = Self {
old_view,
new_view,
new_chunks: new_view.difference(old_view).collect(),
old_chunks: old_view.difference(new_view).collect(),
};
this.new_chunks
.sort_unstable_by_key(|chunk| chunk.distance_squared_to(new_view.center()));
this.old_chunks
.sort_unstable_by_key(|chunk| chunk.distance_squared_to(old_view.center()));
this
}
}
/// Event triggered when an entity crosses into a new chunk.
///
/// Unlike [`ViewUpdateEvent`], this event triggers for all entities,
/// not just players.
pub struct ChunkCrossEvent {
pub old_chunk: ChunkPosition,
pub new_chunk: ChunkPosition,
}
/// Triggered when a chunk is loaded.
#[derive(Debug)]
pub struct ChunkLoadEvent {
pub position: ChunkPosition,
pub chunk: ChunkHandle,
}
/// Triggered when an error occurs while loading a chunk.
#[derive(Debug)]
pub struct ChunkLoadFailEvent {
pub position: ChunkPosition,
}