2020-06-04 07:11:35 +00:00
|
|
|
use crate::{state_ext::StateExt, Server};
|
2020-06-05 01:48:26 +00:00
|
|
|
use common::event::{EventBus, ServerEvent};
|
2020-02-16 20:04:06 +00:00
|
|
|
use entity_creation::{
|
2020-06-16 01:00:32 +00:00
|
|
|
handle_create_npc, handle_create_waypoint, handle_initialize_character,
|
|
|
|
handle_loaded_character_data, handle_shoot,
|
2020-02-16 20:04:06 +00:00
|
|
|
};
|
|
|
|
use entity_manipulation::{
|
2020-06-01 09:21:33 +00:00
|
|
|
handle_damage, handle_destroy, handle_explosion, handle_land_on_ground, handle_level_up,
|
|
|
|
handle_respawn,
|
2020-02-16 20:04:06 +00:00
|
|
|
};
|
2020-04-26 17:03:19 +00:00
|
|
|
use group_manip::handle_group;
|
2020-05-04 15:15:31 +00:00
|
|
|
use interaction::{handle_lantern, handle_mount, handle_possess, handle_unmount};
|
2020-02-16 20:04:06 +00:00
|
|
|
use inventory_manip::handle_inventory;
|
|
|
|
use player::{handle_client_disconnect, handle_exit_ingame};
|
|
|
|
use specs::{Entity as EcsEntity, WorldExt};
|
|
|
|
|
|
|
|
mod entity_creation;
|
|
|
|
mod entity_manipulation;
|
2020-04-26 17:03:19 +00:00
|
|
|
mod group_manip;
|
2020-02-16 20:04:06 +00:00
|
|
|
mod interaction;
|
|
|
|
mod inventory_manip;
|
|
|
|
mod player;
|
|
|
|
|
|
|
|
pub enum Event {
|
2020-06-05 18:12:18 +00:00
|
|
|
ClientConnected {
|
|
|
|
entity: EcsEntity,
|
|
|
|
},
|
|
|
|
ClientDisconnected {
|
|
|
|
entity: EcsEntity,
|
|
|
|
},
|
|
|
|
Chat {
|
|
|
|
entity: Option<EcsEntity>,
|
|
|
|
msg: String,
|
|
|
|
},
|
2020-02-16 20:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Server {
|
|
|
|
pub fn handle_events(&mut self) -> Vec<Event> {
|
|
|
|
let mut frontend_events = Vec::new();
|
|
|
|
|
|
|
|
let mut requested_chunks = Vec::new();
|
|
|
|
let mut chat_commands = Vec::new();
|
2020-06-05 01:48:26 +00:00
|
|
|
let mut chat_messages = Vec::new();
|
2020-02-16 20:04:06 +00:00
|
|
|
|
|
|
|
let events = self
|
|
|
|
.state
|
|
|
|
.ecs()
|
|
|
|
.read_resource::<EventBus<ServerEvent>>()
|
|
|
|
.recv_all();
|
|
|
|
|
|
|
|
for event in events {
|
|
|
|
match event {
|
2020-08-07 04:33:24 +00:00
|
|
|
ServerEvent::Explosion {
|
|
|
|
pos,
|
|
|
|
power,
|
|
|
|
owner,
|
|
|
|
friendly_damage,
|
|
|
|
} => handle_explosion(&self, pos, power, owner, friendly_damage),
|
2020-02-16 20:04:06 +00:00
|
|
|
ServerEvent::Shoot {
|
|
|
|
entity,
|
|
|
|
dir,
|
|
|
|
body,
|
|
|
|
light,
|
|
|
|
projectile,
|
|
|
|
gravity,
|
|
|
|
} => handle_shoot(self, entity, dir, body, light, projectile, gravity),
|
|
|
|
ServerEvent::Damage { uid, change } => handle_damage(&self, uid, change),
|
|
|
|
ServerEvent::Destroy { entity, cause } => handle_destroy(self, entity, cause),
|
|
|
|
ServerEvent::InventoryManip(entity, manip) => handle_inventory(self, entity, manip),
|
2020-04-26 17:03:19 +00:00
|
|
|
ServerEvent::GroupManip(entity, manip) => handle_group(self, entity, manip),
|
2020-02-16 20:04:06 +00:00
|
|
|
ServerEvent::Respawn(entity) => handle_respawn(&self, entity),
|
|
|
|
ServerEvent::LandOnGround { entity, vel } => {
|
|
|
|
handle_land_on_ground(&self, entity, vel)
|
|
|
|
},
|
2020-05-04 15:15:31 +00:00
|
|
|
ServerEvent::ToggleLantern(entity) => handle_lantern(self, entity),
|
2020-02-16 20:04:06 +00:00
|
|
|
ServerEvent::Mount(mounter, mountee) => handle_mount(self, mounter, mountee),
|
|
|
|
ServerEvent::Unmount(mounter) => handle_unmount(self, mounter),
|
|
|
|
ServerEvent::Possess(possessor_uid, possesse_uid) => {
|
|
|
|
handle_possess(&self, possessor_uid, possesse_uid)
|
|
|
|
},
|
2020-06-16 01:00:32 +00:00
|
|
|
ServerEvent::InitCharacterData {
|
2020-02-16 20:04:06 +00:00
|
|
|
entity,
|
2020-04-25 13:41:27 +00:00
|
|
|
character_id,
|
2020-06-16 01:00:32 +00:00
|
|
|
} => handle_initialize_character(self, entity, character_id),
|
|
|
|
ServerEvent::UpdateCharacterData { entity, components } => {
|
|
|
|
handle_loaded_character_data(self, entity, components);
|
|
|
|
},
|
2020-06-01 09:21:33 +00:00
|
|
|
ServerEvent::LevelUp(entity, new_level) => handle_level_up(self, entity, new_level),
|
2020-02-16 20:04:06 +00:00
|
|
|
ServerEvent::ExitIngame { entity } => handle_exit_ingame(self, entity),
|
|
|
|
ServerEvent::CreateNpc {
|
|
|
|
pos,
|
|
|
|
stats,
|
2020-02-26 17:04:43 +00:00
|
|
|
loadout,
|
2020-02-16 20:04:06 +00:00
|
|
|
body,
|
|
|
|
agent,
|
|
|
|
alignment,
|
|
|
|
scale,
|
2020-05-15 15:05:50 +00:00
|
|
|
drop_item,
|
|
|
|
} => handle_create_npc(
|
|
|
|
self, pos, stats, loadout, body, agent, alignment, scale, drop_item,
|
|
|
|
),
|
2020-02-16 20:04:06 +00:00
|
|
|
ServerEvent::CreateWaypoint(pos) => handle_create_waypoint(self, pos),
|
|
|
|
ServerEvent::ClientDisconnect(entity) => {
|
|
|
|
frontend_events.push(handle_client_disconnect(self, entity))
|
|
|
|
},
|
|
|
|
|
|
|
|
ServerEvent::ChunkRequest(entity, key) => {
|
|
|
|
requested_chunks.push((entity, key));
|
|
|
|
},
|
|
|
|
ServerEvent::ChatCmd(entity, cmd) => {
|
|
|
|
chat_commands.push((entity, cmd));
|
|
|
|
},
|
2020-06-04 07:11:35 +00:00
|
|
|
ServerEvent::Chat(msg) => {
|
2020-06-05 01:48:26 +00:00
|
|
|
chat_messages.push(msg);
|
2020-06-04 07:11:35 +00:00
|
|
|
},
|
2020-02-16 20:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate requested chunks.
|
|
|
|
for (entity, key) in requested_chunks {
|
|
|
|
self.generate_chunk(entity, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (entity, cmd) in chat_commands {
|
|
|
|
self.process_chat_cmd(entity, cmd);
|
|
|
|
}
|
|
|
|
|
2020-06-05 01:48:26 +00:00
|
|
|
for msg in chat_messages {
|
|
|
|
self.state.send_chat(msg);
|
|
|
|
}
|
|
|
|
|
2020-02-16 20:04:06 +00:00
|
|
|
frontend_events
|
|
|
|
}
|
|
|
|
}
|