Merge branch 'zesterer/small-fixes' into 'master'

Added entity event system, fixed fall damage

See merge request veloren/veloren!418
This commit is contained in:
Joshua Barretto
2019-08-09 23:56:49 +00:00
8 changed files with 161 additions and 38 deletions

View File

@ -17,9 +17,10 @@ use crate::{
};
use common::{
comp,
event::{Event as GameEvent, EventBus},
msg::{ClientMsg, ClientState, RequestStateError, ServerError, ServerInfo, ServerMsg},
net::PostOffice,
state::{State, TimeOfDay, Uid},
state::{BlockChange, State, TimeOfDay, Uid},
terrain::{block::Block, TerrainChunk, TerrainChunkSize, TerrainMap},
vol::Vox,
vol::{ReadVol, VolSize},
@ -89,6 +90,7 @@ impl Server {
state
.ecs_mut()
.add_resource(SpawnPoint(Vec3::new(16_384.0, 16_384.0, 380.0)));
state.ecs_mut().add_resource(EventBus::default());
// Set starting time for the server.
state.ecs_mut().write_resource::<TimeOfDay>().0 = settings.start_time;
@ -205,6 +207,44 @@ impl Server {
client.allow_state(ClientState::Character);
}
/// Handle events coming through via the event bus
fn handle_events(&mut self) {
let terrain = self.state.ecs().read_resource::<TerrainMap>();
let mut block_change = self.state.ecs().write_resource::<BlockChange>();
let mut stats = self.state.ecs().write_storage::<comp::Stats>();
for event in self.state.ecs().read_resource::<EventBus>().recv_all() {
match event {
GameEvent::LandOnGround { entity, vel } => {
if let Some(stats) = stats.get_mut(entity) {
let falldmg = (vel.z / 1.5 + 10.0) as i32;
if falldmg < 0 {
stats.health.change_by(falldmg, comp::HealthSource::World);
}
}
}
GameEvent::Explosion { pos, radius } => {
const RAYS: usize = 500;
for _ in 0..RAYS {
let dir = Vec3::new(
rand::random::<f32>() - 0.5,
rand::random::<f32>() - 0.5,
rand::random::<f32>() - 0.5,
)
.normalized();
let _ = terrain
.ray(pos, pos + dir * radius)
.until(|_| rand::random::<f32>() < 0.05)
.for_each(|pos| block_change.set(pos, Block::empty()))
.cast();
}
}
}
}
}
/// Execute a single server tick, handle input and update the game state by the given duration.
pub fn tick(&mut self, _input: Input, dt: Duration) -> Result<Vec<Event>, Error> {
// This tick function is the centre of the Veloren universe. Most server-side things are
@ -235,6 +275,9 @@ impl Server {
frontend_events.append(&mut self.handle_new_connections()?);
frontend_events.append(&mut self.handle_new_messages()?);
// Handle game events
self.handle_events();
// 4) Tick the client's LocalState.
self.state.tick(dt);