forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.rs
More file actions
191 lines (162 loc) · 5.54 KB
/
Copy pathentity.rs
File metadata and controls
191 lines (162 loc) · 5.54 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! Provides several useful components, including `EntityComponent`
//! and `PlayerComponent`. In the future, will also
//! provide entity-specific components and systems.
use specs::storage::BTreeStorage;
use specs::{
Component, Entities, Entity, Join, Read, ReadStorage, ReaderId, System, SystemData, VecStorage,
World, Write,
};
use uuid::Uuid;
use feather_core::network::packet::implementation::{
DestroyEntities, EntityHeadLook, EntityLook, EntityLookAndRelativeMove, EntityRelativeMove,
};
use feather_core::world::Position;
use feather_core::Gamemode;
use crate::network::{send_packet_to_all_players, send_packet_to_player, NetworkComponent};
use shrev::EventChannel;
pub struct PlayerComponent {
pub profile_properties: Vec<mojang_api::ServerAuthProperty>,
pub gamemode: Gamemode,
}
impl Component for PlayerComponent {
type Storage = BTreeStorage<Self>;
}
pub struct EntityComponent {
pub uuid: Uuid,
pub display_name: String,
pub position: Position,
pub on_ground: bool,
}
impl Component for EntityComponent {
type Storage = VecStorage<Self>;
}
/// Event triggered when an entity
/// of any type is destroyed.
pub struct EntityDestroyEvent {
/// Note that when this event is triggered,
/// the entity isn't actually removed from the world
/// yet. This allows systems to access the entity's
/// data before it is destroyed.
///
/// `EntityDestroySystem` is responsible for removing
/// entities once the `EntityDestroyEvent` has been
/// handled by all readers.
pub entity: Entity,
}
/// System for removing entities from the world when they
/// are destroyed.
#[derive(Default)]
pub struct EntityDestroySystem {
reader: Option<ReaderId<EntityDestroyEvent>>,
}
impl EntityDestroySystem {
pub fn new() -> Self {
Self { reader: None }
}
}
impl<'a> System<'a> for EntityDestroySystem {
type SystemData = (Write<'a, EventChannel<EntityDestroyEvent>>, Entities<'a>);
fn run(&mut self, data: Self::SystemData) {
let (events, entities) = data;
for event in events.read(&mut self.reader.as_mut().unwrap()) {
entities.delete(event.entity).unwrap();
}
}
fn setup(&mut self, world: &mut World) {
Self::SystemData::setup(world);
self.reader = Some(
world
.fetch_mut::<EventChannel<EntityDestroyEvent>>()
.register_reader(),
);
}
}
/// System for broadcasting when an entity is destroyed.
#[derive(Default)]
pub struct EntityDestroyBroadcastSystem {
reader: Option<ReaderId<EntityDestroyEvent>>,
}
impl EntityDestroyBroadcastSystem {
pub fn new() -> Self {
Self { reader: None }
}
}
impl<'a> System<'a> for EntityDestroyBroadcastSystem {
type SystemData = (
ReadStorage<'a, NetworkComponent>,
Read<'a, EventChannel<EntityDestroyEvent>>,
);
fn run(&mut self, data: Self::SystemData) {
let (net_comps, events) = data;
for event in events.read(&mut self.reader.as_mut().unwrap()) {
let destroy_entities = DestroyEntities::new(vec![event.entity.id() as i32]);
// TODO only broadcast to nearby
for net in net_comps.join() {
send_packet_to_player(net, destroy_entities.clone());
}
}
}
fn setup(&mut self, world: &mut World) {
Self::SystemData::setup(world);
self.reader = Some(
world
.fetch_mut::<EventChannel<EntityDestroyEvent>>()
.register_reader(),
);
}
}
/// Broadcasts to all joined players that an entity has moved.
#[allow(clippy::too_many_arguments)]
pub fn broadcast_entity_movement(
entity: Entity,
old_pos: Position,
new_pos: Position,
has_moved: bool,
has_looked: bool,
netcomps: &ReadStorage<NetworkComponent>,
entities: &Entities,
) {
assert!(has_moved || has_looked);
if has_moved {
let (rx, ry, rz) = calculate_relative_move(old_pos, new_pos);
if has_looked {
let packet = EntityLookAndRelativeMove::new(
entity.id() as i32,
rx,
ry,
rz,
degrees_to_stops(new_pos.yaw),
degrees_to_stops(new_pos.pitch),
true,
);
send_packet_to_all_players(netcomps, entities, packet, Some(entity));
} else {
let packet = EntityRelativeMove::new(entity.id() as i32, rx, ry, rz, true);
send_packet_to_all_players(netcomps, entities, packet, Some(entity));
}
} else {
let packet = EntityLook::new(
entity.id() as i32,
degrees_to_stops(new_pos.yaw),
degrees_to_stops(new_pos.pitch),
true,
);
send_packet_to_all_players(netcomps, entities, packet, Some(entity));
}
// Entity Head Look also needs to be sent if the entity turned its head
if has_looked {
let packet = EntityHeadLook::new(entity.id() as i32, degrees_to_stops(new_pos.yaw));
send_packet_to_all_players(netcomps, entities, packet, Some(entity));
}
}
/// Calculates the relative move fields
/// as used in the Entity Relative Move packets.
pub fn calculate_relative_move(old: Position, current: Position) -> (i16, i16, i16) {
let x = ((current.x * 32.0 - old.x * 32.0) * 128.0) as i16;
let y = ((current.y * 32.0 - old.y * 32.0) * 128.0) as i16;
let z = ((current.z * 32.0 - old.z * 32.0) * 128.0) as i16;
(x, y, z)
}
pub fn degrees_to_stops(degs: f32) -> u8 {
((degs / 360.0) * 256.0) as u8
}