pub struct Entity {Show 15 fields
pub id: u32,
pub creator_id: Uuid,
pub orientation: Vec2<f32>,
pub position: Vec3<f32>,
pub tilt: f32,
pub action: EntityAction,
pub attributes: ValueContainer,
pub dirty_flags: u8,
pub dirty_attributes: FxHashSet<String>,
pub inventory: Vec<Option<Item>>,
pub inventory_additions: FxHashMap<usize, Item>,
pub inventory_removals: FxHashSet<usize>,
pub inventory_updates: FxHashMap<usize, ItemUpdate>,
pub equipped: IndexMap<String, Item>,
pub wallet: Wallet,
}Expand description
The Rust representation of an Entity. The real entity class lives in Python, this class is the Rust side instantiation (to avoid unnecessary Python look ups for common attributes). The class gets synced with the Python side.
Fields§
§id: u32The id of the entity in the entity manager
creator_id: UuidMaps the entity to a creator id
orientation: Vec2<f32>The XZ orientation
position: Vec3<f32>The position in the map
tilt: f32The vertical camera tilt, 0.0 means flat, no tilt.
action: EntityActionThe current action, server side.
attributes: ValueContainerAttributes
dirty_flags: u8Dirty static attributes
The dirty_flags field is a bitmask representing changes to various components of the entity.
Each bit corresponds to a specific type of change:
0b00000001(1): Position changed0b00000010(2): Orientation changed0b00000100(4): Tilt changed0b00001000(8): Inventory changed0b00010000(16): Equipped items changed0b00100000(32): Wallet changed
dirty_attributes: FxHashSet<String>Dirty Attributes
inventory: Vec<Option<Item>>Inventory: A container for the entity’s items
inventory_additions: FxHashMap<usize, Item>Track added items
inventory_removals: FxHashSet<usize>Track removed items
inventory_updates: FxHashMap<usize, ItemUpdate>Track updated items
equipped: IndexMap<String, Item>Equipped items
wallet: WalletWallet
Implementations§
Source§impl Entity
impl Entity
pub fn new() -> Self
Sourcepub fn get_pos_xz(&self) -> Vec2<f32>
pub fn get_pos_xz(&self) -> Vec2<f32>
Get the XZ position.
Sourcepub fn camera_look_at(&self) -> Vec3<f32>
pub fn camera_look_at(&self) -> Vec3<f32>
Computes the look-at target based on position, orientation, and vertical tilt (tilt).
Sourcepub fn turn_left(&mut self, degrees: f32)
pub fn turn_left(&mut self, degrees: f32)
Rotates the entity to the left by a certain degree.
Examples found in repository?
90 fn draw(&mut self, pixels: &mut [u8], ctx: &mut TheContext) {
91 let _start = get_time();
92
93 match &self.movement {
94 MoveForward => {
95 self.entity.move_forward(0.05);
96 }
97 MoveBackward => {
98 self.entity.move_backward(0.05);
99 }
100 TurnLeft => {
101 self.entity.turn_left(1.0);
102 }
103 TurnRight => {
104 self.entity.turn_right(1.0);
105 }
106 Off => {}
107 }
108 // self.entity.apply_to_camera(&mut self.camera);
109
110 // Set it up
111 Rasterizer::setup(
112 None,
113 self.camera.view_matrix(),
114 self.camera
115 .projection_matrix(ctx.width as f32, ctx.height as f32),
116 )
117 .ambient(Vec4::one())
118 .rasterize(
119 &mut self.scene,
120 pixels, // Destination buffer
121 ctx.width, // Destination buffer width
122 ctx.height, // Destination buffer height
123 40, // Tile size
124 &self.assets,
125 );
126
127 let _stop = get_time();
128 // println!("Execution time: {:?} ms.", _stop - _start);
129 }Sourcepub fn turn_right(&mut self, degrees: f32)
pub fn turn_right(&mut self, degrees: f32)
Rotates the entity to the right by a certain degree.
Examples found in repository?
90 fn draw(&mut self, pixels: &mut [u8], ctx: &mut TheContext) {
91 let _start = get_time();
92
93 match &self.movement {
94 MoveForward => {
95 self.entity.move_forward(0.05);
96 }
97 MoveBackward => {
98 self.entity.move_backward(0.05);
99 }
100 TurnLeft => {
101 self.entity.turn_left(1.0);
102 }
103 TurnRight => {
104 self.entity.turn_right(1.0);
105 }
106 Off => {}
107 }
108 // self.entity.apply_to_camera(&mut self.camera);
109
110 // Set it up
111 Rasterizer::setup(
112 None,
113 self.camera.view_matrix(),
114 self.camera
115 .projection_matrix(ctx.width as f32, ctx.height as f32),
116 )
117 .ambient(Vec4::one())
118 .rasterize(
119 &mut self.scene,
120 pixels, // Destination buffer
121 ctx.width, // Destination buffer width
122 ctx.height, // Destination buffer height
123 40, // Tile size
124 &self.assets,
125 );
126
127 let _stop = get_time();
128 // println!("Execution time: {:?} ms.", _stop - _start);
129 }Sourcepub fn move_forward(&mut self, distance: f32)
pub fn move_forward(&mut self, distance: f32)
Moves the entity forward along its current orientation.
Examples found in repository?
90 fn draw(&mut self, pixels: &mut [u8], ctx: &mut TheContext) {
91 let _start = get_time();
92
93 match &self.movement {
94 MoveForward => {
95 self.entity.move_forward(0.05);
96 }
97 MoveBackward => {
98 self.entity.move_backward(0.05);
99 }
100 TurnLeft => {
101 self.entity.turn_left(1.0);
102 }
103 TurnRight => {
104 self.entity.turn_right(1.0);
105 }
106 Off => {}
107 }
108 // self.entity.apply_to_camera(&mut self.camera);
109
110 // Set it up
111 Rasterizer::setup(
112 None,
113 self.camera.view_matrix(),
114 self.camera
115 .projection_matrix(ctx.width as f32, ctx.height as f32),
116 )
117 .ambient(Vec4::one())
118 .rasterize(
119 &mut self.scene,
120 pixels, // Destination buffer
121 ctx.width, // Destination buffer width
122 ctx.height, // Destination buffer height
123 40, // Tile size
124 &self.assets,
125 );
126
127 let _stop = get_time();
128 // println!("Execution time: {:?} ms.", _stop - _start);
129 }Sourcepub fn move_backward(&mut self, distance: f32)
pub fn move_backward(&mut self, distance: f32)
Moves the entity backward along its current orientation.
Examples found in repository?
90 fn draw(&mut self, pixels: &mut [u8], ctx: &mut TheContext) {
91 let _start = get_time();
92
93 match &self.movement {
94 MoveForward => {
95 self.entity.move_forward(0.05);
96 }
97 MoveBackward => {
98 self.entity.move_backward(0.05);
99 }
100 TurnLeft => {
101 self.entity.turn_left(1.0);
102 }
103 TurnRight => {
104 self.entity.turn_right(1.0);
105 }
106 Off => {}
107 }
108 // self.entity.apply_to_camera(&mut self.camera);
109
110 // Set it up
111 Rasterizer::setup(
112 None,
113 self.camera.view_matrix(),
114 self.camera
115 .projection_matrix(ctx.width as f32, ctx.height as f32),
116 )
117 .ambient(Vec4::one())
118 .rasterize(
119 &mut self.scene,
120 pixels, // Destination buffer
121 ctx.width, // Destination buffer width
122 ctx.height, // Destination buffer height
123 40, // Tile size
124 &self.assets,
125 );
126
127 let _stop = get_time();
128 // println!("Execution time: {:?} ms.", _stop - _start);
129 }Sourcepub fn get_forward_pos(&mut self, distance: f32) -> Vec2<f32>
pub fn get_forward_pos(&mut self, distance: f32) -> Vec2<f32>
Gets the position in the direction of the entity’s orientation by a given distance.
Sourcepub fn apply_to_camera(
&self,
camera: &mut Box<dyn D3Camera>,
firstp_eye_level: f32,
)
pub fn apply_to_camera( &self, camera: &mut Box<dyn D3Camera>, firstp_eye_level: f32, )
Applies the camera’s position and look-at parameters based on the entity’s state.
Sourcepub fn set_position(&mut self, new_position: Vec3<f32>)
pub fn set_position(&mut self, new_position: Vec3<f32>)
Set the position and mark it as dirty
Sourcepub fn set_pos_xz(&mut self, new_position: Vec2<f32>)
pub fn set_pos_xz(&mut self, new_position: Vec2<f32>)
Set the position as a Vec2 and mark it as dirty
Sourcepub fn set_orientation(&mut self, new_orientation: Vec2<f32>)
pub fn set_orientation(&mut self, new_orientation: Vec2<f32>)
Set the orientation and mark it as dirty
Sourcepub fn set_tilt_from_screen_coordinate(&mut self, screen_y: f32)
pub fn set_tilt_from_screen_coordinate(&mut self, screen_y: f32)
Maps a normalized screen coordinate (0.0 to 1.0) to a tilt angle.
0.0 -> maximum downward tilt, 1.0 -> maximum upward tilt.
Examples found in repository?
140 fn hover(&mut self, x: f32, y: f32, ctx: &mut TheContext) -> bool {
141 if self.camera.id() == "orbit" {
142 self.camera.set_parameter_vec2(
143 "from_normalized",
144 Vec2::new(x / ctx.width as f32, y / ctx.height as f32),
145 );
146 } else if self.camera.id() == "firstp" {
147 self.entity
148 .set_tilt_from_screen_coordinate(1.0 - y / ctx.height as f32);
149 }
150 true
151 }Sourcepub fn add_item(&mut self, item: Item) -> Result<usize, String>
pub fn add_item(&mut self, item: Item) -> Result<usize, String>
Add an item to the entity’s inventory and track additions
Sourcepub fn remove_item_from_slot(&mut self, slot: usize) -> Option<Item>
pub fn remove_item_from_slot(&mut self, slot: usize) -> Option<Item>
Remove an item from the given slot.
Sourcepub fn remove_item(&mut self, id: u32) -> Option<Item>
pub fn remove_item(&mut self, id: u32) -> Option<Item>
Remove an item from the entity’s inventory and track removals
Sourcepub fn get_item_in_slot(&self, slot: usize) -> Option<&Item>
pub fn get_item_in_slot(&self, slot: usize) -> Option<&Item>
Get a reference to an item in a given slot.
Sourcepub fn get_item_in_slot_mut(&mut self, slot: usize) -> Option<&mut Item>
pub fn get_item_in_slot_mut(&mut self, slot: usize) -> Option<&mut Item>
Get a mutable reference to an item in a given slot.
Sourcepub fn get_item_mut(&mut self, item_id: u32) -> Option<&mut Item>
pub fn get_item_mut(&mut self, item_id: u32) -> Option<&mut Item>
Get a mutable reference to an item by its ID
Sourcepub fn get_item_slot(&self, item_id: u32) -> Option<usize>
pub fn get_item_slot(&self, item_id: u32) -> Option<usize>
Get the slot index of an item by its ID
Sourcepub fn equip_item(&mut self, item_id: u32, slot: &str) -> Result<(), String>
pub fn equip_item(&mut self, item_id: u32, slot: &str) -> Result<(), String>
Equip an item into a specific slot
Sourcepub fn unequip_item(&mut self, slot: &str) -> Result<Item, String>
pub fn unequip_item(&mut self, slot: &str) -> Result<Item, String>
Unequip an item from a specific slot
Sourcepub fn get_equipped_item(&self, slot: &str) -> Option<&Item>
pub fn get_equipped_item(&self, slot: &str) -> Option<&Item>
Get a reference of an item equipped in a specific slot
Sourcepub fn get_equipped_item_mut(&mut self, slot: &str) -> Option<&mut Item>
pub fn get_equipped_item_mut(&mut self, slot: &str) -> Option<&mut Item>
Get the mutable reference of an item equipped in a specific slot
Sourcepub fn add_currency(
&mut self,
symbol: &str,
amount: i64,
currencies: &Currencies,
) -> Result<(), String>
pub fn add_currency( &mut self, symbol: &str, amount: i64, currencies: &Currencies, ) -> Result<(), String>
Add the given currency to the wallet.
Sourcepub fn add_base_currency(
&mut self,
amount: i64,
currencies: &Currencies,
) -> Result<(), String>
pub fn add_base_currency( &mut self, amount: i64, currencies: &Currencies, ) -> Result<(), String>
Add the given base currency to the wallet.
Sourcepub fn spend_currency(
&mut self,
base_amount: i64,
currencies: &Currencies,
) -> Result<(), String>
pub fn spend_currency( &mut self, base_amount: i64, currencies: &Currencies, ) -> Result<(), String>
Spend the given currency.
Sourcepub fn set_attribute(&mut self, key: &str, value: Value)
pub fn set_attribute(&mut self, key: &str, value: Value)
Set a dynamic attribute and mark it as dirty
Sourcepub fn get_attr_string(&self, key: &str) -> Option<String>
pub fn get_attr_string(&self, key: &str) -> Option<String>
Get the given String
Sourcepub fn get_attr_uuid(&self, key: &str) -> Option<Uuid>
pub fn get_attr_uuid(&self, key: &str) -> Option<Uuid>
Get the given Uuid
Sourcepub fn mark_dirty_attribute(&mut self, key: &str)
pub fn mark_dirty_attribute(&mut self, key: &str)
Mark a dynamic attribute as dirty
Sourcepub fn mark_all_dirty(&mut self)
pub fn mark_all_dirty(&mut self)
Mark all fields and attributes as dirty.
Sourcepub fn is_dirty(&mut self) -> bool
pub fn is_dirty(&mut self) -> bool
Check if the entity is dirty and generate item updates as needed.
Sourcepub fn set_static_dirty(&mut self)
pub fn set_static_dirty(&mut self)
Mark all static fields as dirty
Sourcepub fn clear_dirty(&mut self)
pub fn clear_dirty(&mut self)
Clear all dirty flags and attributes
Sourcepub fn get_update(&self) -> EntityUpdate
pub fn get_update(&self) -> EntityUpdate
Get a partial update containing only dirty fields and attributes
Sourcepub fn apply_update(&mut self, update: EntityUpdate) -> bool
pub fn apply_update(&mut self, update: EntityUpdate) -> bool
Apply an update to the entity. Returns true if the entities appearance has changed and needs to be updated.
Sourcepub fn face_north(&mut self)
pub fn face_north(&mut self)
Sets the orientation to face north.
Sourcepub fn face_south(&mut self)
pub fn face_south(&mut self)
Sets the orientation to face south.
Sourcepub fn face_random(&mut self)
pub fn face_random(&mut self)
Sets the orientation to face a random direction.
Sourcepub fn iter_inventory(&self) -> impl Iterator<Item = (usize, &Item)>
pub fn iter_inventory(&self) -> impl Iterator<Item = (usize, &Item)>
Create an iterator over the inventory.
Sourcepub fn iter_inventory_mut(&mut self) -> impl Iterator<Item = (usize, &mut Item)>
pub fn iter_inventory_mut(&mut self) -> impl Iterator<Item = (usize, &mut Item)>
Create a mutable iterator over the inventory.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Entity
impl<'de> Deserialize<'de> for Entity
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for Entity
impl RefUnwindSafe for Entity
impl Send for Entity
impl Sync for Entity
impl Unpin for Entity
impl UnsafeUnpin for Entity
impl UnwindSafe for Entity
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.