portal event -> change body and teleport to

This commit is contained in:
Maxicarlos08 2023-07-27 00:11:41 +02:00
parent 8fe5f2e708
commit 26ada89bb9
No known key found for this signature in database
5 changed files with 42 additions and 54 deletions

View File

@ -4,11 +4,6 @@ use vek::Vec3;
use crate::resources::{Secs, Time}; use crate::resources::{Secs, Time};
pub enum TeleporterEvent {
PortalTeleport { entity: Entity, target: Vec3<f32> },
SetPortalActive { portal: Entity, active: bool },
}
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Teleporter { pub struct Teleporter {
pub target: Vec3<f32>, pub target: Vec3<f32>,

View File

@ -332,7 +332,10 @@ pub enum ServerEvent {
RemoveLightEmitter { RemoveLightEmitter {
entity: EcsEntity, entity: EcsEntity,
}, },
PortalEvent(comp::teleport::TeleporterEvent), TeleportToPosition {
entity: EcsEntity,
position: Vec3<f32>,
},
} }
pub struct EventBus<E> { pub struct EventBus<E> {

View File

@ -21,8 +21,6 @@ use common::{
inventory::item::{AbilityMap, MaterialStatManifest}, inventory::item::{AbilityMap, MaterialStatManifest},
item::flatten_counted_items, item::flatten_counted_items,
loot_owner::LootOwnerKind, loot_owner::LootOwnerKind,
object,
teleport::TeleporterEvent,
Alignment, Auras, Body, CharacterState, Energy, Group, Health, HealthChange, Inventory, Alignment, Auras, Body, CharacterState, Energy, Group, Health, HealthChange, Inventory,
Player, Poise, Pos, SkillSet, Stats, Player, Poise, Pos, SkillSet, Stats,
}, },
@ -1659,30 +1657,19 @@ pub fn handle_remove_light_emitter(server: &mut Server, entity: EcsEntity) {
.remove(entity); .remove(entity);
} }
pub fn handle_portal_event(server: &mut Server, event: TeleporterEvent) { pub fn handle_teleport_to_position_event(
server: &mut Server,
entity: EcsEntity,
position: Vec3<f32>,
) {
let ecs = server.state.ecs(); let ecs = server.state.ecs();
match event {
TeleporterEvent::PortalTeleport { entity, target } => {
ecs.write_storage::<comp::Pos>() ecs.write_storage::<comp::Pos>()
.get_mut(entity) .get_mut(entity)
.map(|old_position| { .map(|old_position| {
old_position.0 = target; old_position.0 = position;
}); });
ecs.write_storage::<comp::ForceUpdate>() ecs.write_storage::<comp::ForceUpdate>()
.get_mut(entity) .get_mut(entity)
.map(|forced_update| forced_update.update()); .map(|forced_update| forced_update.update());
},
TeleporterEvent::SetPortalActive { portal, active } => {
ecs.write_storage::<comp::Body>()
.get_mut(portal)
.map(|mut body| {
*body = comp::body::Body::Object(if active {
object::Body::PortalActive
} else {
object::Body::Portal
});
});
},
}
} }

View File

@ -1,7 +1,7 @@
use crate::{ use crate::{
events::{ events::{
entity_creation::handle_create_teleporter, entity_creation::handle_create_teleporter,
entity_manipulation::handle_portal_event, entity_manipulation::handle_teleport_to_position_event,
interaction::{handle_mount_volume, handle_tame_pet}, interaction::{handle_mount_volume, handle_tame_pet},
}, },
persistence::PersistedComponents, persistence::PersistedComponents,
@ -300,7 +300,9 @@ impl Server {
ServerEvent::RemoveLightEmitter { entity } => { ServerEvent::RemoveLightEmitter { entity } => {
handle_remove_light_emitter(self, entity) handle_remove_light_emitter(self, entity)
}, },
ServerEvent::PortalEvent(event) => handle_portal_event(self, event), ServerEvent::TeleportToPosition { entity, position } => {
handle_teleport_to_position_event(self, entity, position)
},
} }
} }

View File

@ -1,8 +1,5 @@
use common::{ use common::{
comp::{ comp::{object, Agent, Body, CharacterState, Player, Pos, Teleporter, Teleporting},
object, teleport::TeleporterEvent, Agent, Body, CharacterState, Player, Pos, Teleporter,
Teleporting,
},
event::{EventBus, ServerEvent}, event::{EventBus, ServerEvent},
resources::Time, resources::Time,
CachedSpatialGrid, CachedSpatialGrid,
@ -56,15 +53,6 @@ impl<'a> System<'a> for Sys {
event_bus, event_bus,
): Self::SystemData, ): Self::SystemData,
) { ) {
let mut player_data = (
&entities,
&positions,
&players,
&character_states,
teleporting.entries(),
)
.join();
let check_aggro = |entity, pos: Vec3<f32>| { let check_aggro = |entity, pos: Vec3<f32>| {
spatial_grid spatial_grid
.0 .0
@ -87,6 +75,15 @@ impl<'a> System<'a> for Sys {
let mut is_active = false; let mut is_active = false;
let mut player_data = (
&entities,
&positions,
&players,
&character_states,
teleporting.entries(),
)
.join();
for (entity, pos, _, character_state, teleporting) in for (entity, pos, _, character_state, teleporting) in
nearby_entities.filter_map(|entity| { nearby_entities.filter_map(|entity| {
player_data player_data
@ -120,10 +117,14 @@ impl<'a> System<'a> for Sys {
} }
if (*body == Body::Object(object::Body::PortalActive)) != is_active { if (*body == Body::Object(object::Body::PortalActive)) != is_active {
event_bus.emit_now(ServerEvent::PortalEvent(TeleporterEvent::SetPortalActive { event_bus.emit_now(ServerEvent::ChangeBody {
portal: portal_entity, entity: portal_entity,
active: is_active, new_body: Body::Object(if is_active {
})); object::Body::PortalActive
} else {
object::Body::Portal
}),
});
} }
} }
@ -143,10 +144,10 @@ impl<'a> System<'a> for Sys {
teleporting.remove(); teleporting.remove();
} else if teleporting.get().end_time.0 <= time.0 { } else if teleporting.get().end_time.0 <= time.0 {
teleporting.remove(); teleporting.remove();
event_bus.emit_now(ServerEvent::PortalEvent(TeleporterEvent::PortalTeleport { event_bus.emit_now(ServerEvent::TeleportToPosition {
entity, entity,
target: teleporter.target, position: teleporter.target,
})); });
} }
} }
} }