veloren/server/src/events/entity_creation.rs

147 lines
3.6 KiB
Rust
Raw Normal View History

2020-06-25 18:50:04 +00:00
use crate::{sys, Server, StateExt};
use common::{
character::CharacterId,
comp::{
2020-06-13 08:21:47 +00:00
self, humanoid::DEFAULT_HUMANOID_EYE_HEIGHT, Agent, Alignment, Body, Gravity, Item,
2020-08-08 20:53:55 +00:00
ItemDrop, LightEmitter, Loadout, Pos, Projectile, Scale, Shockwave, Stats, Vel, WaypointArea,
},
2020-07-31 17:16:20 +00:00
outcome::Outcome,
2020-08-06 16:41:43 +00:00
util::Dir,
2020-02-16 20:04:06 +00:00
};
2020-08-08 12:53:07 +00:00
use comp::group;
2020-02-16 20:04:06 +00:00
use specs::{Builder, Entity as EcsEntity, WorldExt};
use vek::{Rgb, Vec3};
pub fn handle_initialize_character(
server: &mut Server,
entity: EcsEntity,
character_id: CharacterId,
) {
server.state.initialize_character_data(entity, character_id);
}
pub fn handle_loaded_character_data(
2020-02-16 20:04:06 +00:00
server: &mut Server,
entity: EcsEntity,
loaded_components: (comp::Body, comp::Stats, comp::Inventory, comp::Loadout),
2020-02-16 20:04:06 +00:00
) {
2020-06-25 18:50:04 +00:00
server
.state
.update_character_data(entity, loaded_components);
sys::subscription::initialize_region_subscription(server.state.ecs(), entity);
2020-02-16 20:04:06 +00:00
}
#[allow(clippy::too_many_arguments)] // TODO: Pending review in #587
2020-02-16 20:04:06 +00:00
pub fn handle_create_npc(
server: &mut Server,
pos: Pos,
stats: Stats,
loadout: Loadout,
2020-02-16 20:04:06 +00:00
body: Body,
2020-07-05 12:39:28 +00:00
agent: impl Into<Option<Agent>>,
2020-02-16 20:04:06 +00:00
alignment: Alignment,
scale: Scale,
drop_item: Option<Item>,
2020-02-16 20:04:06 +00:00
) {
let group = match alignment {
Alignment::Wild => None,
Alignment::Passive => None,
Alignment::Enemy => Some(group::ENEMY),
Alignment::Npc | Alignment::Tame => Some(group::NPC),
// TODO: handle
Alignment::Owned(_) => None,
};
let entity = server
2020-02-16 20:04:06 +00:00
.state
.create_npc(pos, stats, loadout, body)
2020-02-16 20:04:06 +00:00
.with(scale)
.with(alignment);
let entity = if let Some(group) = group {
entity.with(group)
} else {
entity
};
2020-07-05 12:39:28 +00:00
let entity = if let Some(agent) = agent.into() {
entity.with(agent)
} else {
entity
};
let entity = if let Some(drop_item) = drop_item {
entity.with(ItemDrop(drop_item))
} else {
entity
};
entity.build();
2020-02-16 20:04:06 +00:00
}
pub fn handle_shoot(
server: &mut Server,
entity: EcsEntity,
dir: Dir,
2020-02-16 20:04:06 +00:00
body: Body,
light: Option<LightEmitter>,
projectile: Projectile,
gravity: Option<Gravity>,
speed: f32,
2020-02-16 20:04:06 +00:00
) {
let state = server.state_mut();
let mut pos = state
.ecs()
.read_storage::<Pos>()
.get(entity)
.expect("Failed to fetch entity")
.0;
let vel = *dir * speed;
2020-07-31 17:16:20 +00:00
// Add an outcome
2020-08-06 16:41:43 +00:00
state
.ecs()
.write_resource::<Vec<Outcome>>()
.push(Outcome::ProjectileShot { pos, body, vel });
2020-07-31 17:16:20 +00:00
2020-06-13 08:21:47 +00:00
let eye_height = match state.ecs().read_storage::<comp::Body>().get(entity) {
Some(comp::Body::Humanoid(body)) => body.eye_height(),
_ => DEFAULT_HUMANOID_EYE_HEIGHT,
};
pos.z += eye_height;
2020-02-16 20:04:06 +00:00
2020-07-31 17:16:20 +00:00
let mut builder = state.create_projectile(Pos(pos), Vel(vel), body, projectile);
2020-02-16 20:04:06 +00:00
if let Some(light) = light {
builder = builder.with(light)
}
if let Some(gravity) = gravity {
builder = builder.with(gravity)
}
builder.build();
}
2020-08-08 20:53:55 +00:00
pub fn handle_shockwave(server: &mut Server, shockwave: Shockwave) {
let state = server.state_mut();
let builder = state.create_shockwave(shockwave);
builder.build();
}
2020-02-16 20:04:06 +00:00
pub fn handle_create_waypoint(server: &mut Server, pos: Vec3<f32>) {
server
.state
2020-02-16 20:04:06 +00:00
.create_object(Pos(pos), comp::object::Body::CampfireLit)
.with(LightEmitter {
2020-08-30 20:10:56 +00:00
col: Rgb::new(1.0, 0.3, 0.1),
strength: 5.0,
flicker: 1.0,
animated: true,
2020-02-16 20:04:06 +00:00
})
.with(WaypointArea::default())
.with(comp::Mass(100000.0))
2020-02-16 20:04:06 +00:00
.build();
}