use std::hash::Hash;
use naia_shared::{EntityAuthStatus, ReplicaMutWrapper, ReplicatedComponent, WorldMutType};
use crate::{room::RoomKey, server::Server, ReplicationConfig};
pub struct EntityMut<'s, E: Copy + Eq + Hash + Send + Sync, W: WorldMutType<E>> {
server: &'s mut Server<E>,
world: W,
entity: E,
}
impl<'s, E: Copy + Eq + Hash + Send + Sync, W: WorldMutType<E>> EntityMut<'s, E, W> {
pub(crate) fn new(server: &'s mut Server<E>, world: W, entity: &E) -> Self {
EntityMut {
server,
world,
entity: *entity,
}
}
pub fn id(&self) -> E {
self.entity
}
pub fn despawn(&mut self) {
self.server.despawn_entity(&mut self.world, &self.entity);
}
pub fn has_component<R: ReplicatedComponent>(&self) -> bool {
self.world.has_component::<R>(&self.entity)
}
pub fn component<R: ReplicatedComponent>(&mut self) -> Option<ReplicaMutWrapper<R>> {
self.world.component_mut::<R>(&self.entity)
}
pub fn insert_component<R: ReplicatedComponent>(&mut self, component_ref: R) -> &mut Self {
self.server
.insert_component(&mut self.world, &self.entity, component_ref);
self
}
pub fn insert_components<R: ReplicatedComponent>(&mut self, mut component_refs: Vec<R>) -> &mut Self {
while let Some(component_ref) = component_refs.pop() {
self.insert_component(component_ref);
}
self
}
pub fn remove_component<R: ReplicatedComponent>(&mut self) -> Option<R> {
self.server
.remove_component::<R, W>(&mut self.world, &self.entity)
}
pub fn configure_replication(&mut self, config: ReplicationConfig) -> &mut Self {
self.server
.configure_entity_replication(&mut self.world, &self.entity, config);
self
}
pub fn replication_config(&self) -> Option<ReplicationConfig> {
self.server.entity_replication_config(&self.entity)
}
pub fn authority(&self) -> Option<EntityAuthStatus> {
self.server.entity_authority_status(&self.entity)
}
pub fn enter_room(&mut self, room_key: &RoomKey) -> &mut Self {
self.server.room_add_entity(room_key, &self.entity);
self
}
pub fn leave_room(&mut self, room_key: &RoomKey) -> &mut Self {
self.server.room_remove_entity(room_key, &self.entity);
self
}
}