/
component.rs
80 lines (66 loc) · 1.98 KB
/
component.rs
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
//! Various Specs components.
use feather_core::world::Position;
use feather_core::Gamemode;
use glm::Vec3;
use specs::storage::BTreeStorage;
use specs::{Component, FlaggedStorage, Join, System, VecStorage, WriteStorage};
use uuid::Uuid;
pub struct PlayerComponent {
pub profile_properties: Vec<mojang_api::ServerAuthProperty>,
pub gamemode: Gamemode,
}
impl Component for PlayerComponent {
type Storage = BTreeStorage<Self>;
}
#[derive(Debug, PartialEq)]
pub struct PositionComponent {
/// The current position of this entity.
pub current: Position,
/// The position of this entity on the previous
/// tick. At the end of each tick, `reset` should
/// be called.
pub previous: Position,
}
impl PositionComponent {
/// Resets the current and previous position.
/// Should be called at the end of every tick.
pub fn reset(&mut self) {
self.previous = self.current;
}
}
impl Component for PositionComponent {
type Storage = FlaggedStorage<Self, VecStorage<Self>>;
}
/// An entity's velocity, in blocks per tick.
///
/// Entities without this component are assumed
/// to have a velocity of 0.
#[derive(Deref, DerefMut, Debug, PartialEq, Clone)]
pub struct VelocityComponent(pub Vec3);
impl Component for VelocityComponent {
type Storage = FlaggedStorage<Self, VecStorage<Self>>;
}
impl Default for VelocityComponent {
fn default() -> Self {
Self(glm::vec3(0.0, 0.0, 0.0))
}
}
#[derive(Clone, Debug)]
pub struct NamedComponent {
pub display_name: String,
pub uuid: Uuid,
}
impl Component for NamedComponent {
type Storage = BTreeStorage<Self>;
}
/// System for resetting an entity's components
/// at the end of the tick.
pub struct ComponentResetSystem;
impl<'a> System<'a> for ComponentResetSystem {
type SystemData = WriteStorage<'a, PositionComponent>;
fn run(&mut self, mut positions: Self::SystemData) {
for position in (&mut positions).join() {
position.reset();
}
}
}