Reuse combat code

This commit is contained in:
timokoesters 2019-09-25 23:31:25 +02:00
parent 2ba143a514
commit ab634f1d21
No known key found for this signature in database
GPG Key ID: CD80BE9AAEE78097
4 changed files with 23 additions and 16 deletions

View File

@ -28,7 +28,7 @@ pub enum ServerEvent {
},
Damage {
uid: Uid,
power: u32,
dmg: u32,
cause: comp::HealthSource,
},
Destroy {

View File

@ -2,6 +2,7 @@ use crate::{
comp::{
ActionState::*, CharacterState, Controller, ForceUpdate, HealthSource, Ori, Pos, Stats, Vel,
},
event::{EventBus, LocalEvent, ServerEvent},
state::{DeltaTime, Uid},
};
use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage};
@ -14,7 +15,7 @@ pub const ATTACK_DURATION: Duration = Duration::from_millis(500);
// Delay before hit
const PREPARE_DURATION: Duration = Duration::from_millis(100);
const BASE_DMG: i32 = 10;
const BASE_DMG: u32 = 10;
const BLOCK_EFFICIENCY: f32 = 0.9;
const ATTACK_RANGE: f32 = 4.0;
@ -28,8 +29,10 @@ pub struct Sys;
impl<'a> System<'a> for Sys {
type SystemData = (
Entities<'a>,
ReadStorage<'a, Uid>,
Read<'a, EventBus<ServerEvent>>,
Read<'a, EventBus<LocalEvent>>,
Read<'a, DeltaTime>,
ReadStorage<'a, Uid>,
ReadStorage<'a, Pos>,
ReadStorage<'a, Ori>,
ReadStorage<'a, Controller>,
@ -43,8 +46,10 @@ impl<'a> System<'a> for Sys {
&mut self,
(
entities,
uids,
server_bus,
local_bus,
dt,
uids,
positions,
orientations,
controllers,
@ -54,6 +59,9 @@ impl<'a> System<'a> for Sys {
mut force_updates,
): Self::SystemData,
) {
let mut server_emitter = server_bus.emitter();
let mut local_emitter = local_bus.emitter();
// Attacks
for (entity, uid, pos, ori, _) in
(&entities, &uids, &positions, &orientations, &controllers).join()
@ -79,8 +87,9 @@ impl<'a> System<'a> for Sys {
if deal_damage {
if let Some(Attack { .. }) = &character_states.get(entity).map(|c| c.action) {
// Go through all other entities
for (b, pos_b, ori_b, character_b, mut vel_b, stat_b) in (
for (b, uid_b, pos_b, ori_b, character_b, mut vel_b, stat_b) in (
&entities,
&uids,
&positions,
&orientations,
&character_states,
@ -105,18 +114,16 @@ impl<'a> System<'a> for Sys {
&& ori_b.0.angle_between(pos.0 - pos_b.0).to_degrees()
< BLOCK_ANGLE / 2.0
{
(BASE_DMG as f32 * (1.0 - BLOCK_EFFICIENCY)) as i32
(BASE_DMG as f32 * (1.0 - BLOCK_EFFICIENCY)) as u32
} else {
BASE_DMG
};
// Deal damage
stat_b
.health
.change_by(-dmg, HealthSource::Attack { by: *uid }); // TODO: variable damage and weapon
vel_b.0 += (pos_b.0 - pos.0).normalized() * KNOCKBACK_XY;
vel_b.0.z = KNOCKBACK_Z;
let _ = force_updates.insert(b, ForceUpdate);
server_emitter.emit(ServerEvent::Damage {
uid: *uid_b,
dmg,
cause: HealthSource::Attack { by: *uid },
}); // TODO: Variable damage
}
}
}

View File

@ -76,7 +76,7 @@ impl<'a> System<'a> for Sys {
projectile::Effect::Damage(power) => {
server_emitter.emit(ServerEvent::Damage {
uid: other,
power,
dmg: power,
cause: HealthSource::Projectile,
})
}

View File

@ -313,11 +313,11 @@ impl Server {
.build();
}
ServerEvent::Damage { uid, power, cause } => {
ServerEvent::Damage { uid, dmg, cause } => {
let ecs = state.ecs_mut();
if let Some(entity) = ecs.entity_from_uid(uid.into()) {
if let Some(stats) = ecs.write_storage::<comp::Stats>().get_mut(entity) {
stats.health.change_by(-(power as i32), cause);
stats.health.change_by(-(dmg as i32), cause);
}
}
}