Add ServerEvent::CreateShip, and use it instead of CreateNpc for RtSim airships.

This commit is contained in:
Avi Weinstock 2021-03-22 18:11:13 -04:00 committed by Marcel Märtens
parent 49f39fb752
commit deb9358c3a
7 changed files with 105 additions and 65 deletions

View File

@ -112,7 +112,7 @@ pub enum ServerEvent {
CreateNpc {
pos: comp::Pos,
stats: comp::Stats,
health: Option<comp::Health>,
health: comp::Health,
poise: comp::Poise,
loadout: comp::inventory::loadout::Loadout,
body: comp::Body,
@ -123,6 +123,14 @@ pub enum ServerEvent {
drop_item: Option<Item>,
rtsim_entity: Option<RtSimEntity>,
},
CreateShip {
pos: comp::Pos,
ship: comp::ship::Body,
level: u16,
mountable: bool,
agent: Option<comp::Agent>,
rtsim_entity: Option<RtSimEntity>,
},
CreateWaypoint(Vec3<f32>),
ClientDisconnect(EcsEntity),
ChunkRequest(EcsEntity, Vec2<i32>),

View File

@ -855,7 +855,7 @@ fn handle_spawn(
id,
npc::BodyType::from_body(body),
)),
Some(comp::Health::new(body, 1)),
comp::Health::new(body, 1),
comp::Poise::new(body),
inventory,
body,
@ -968,14 +968,7 @@ fn handle_spawn_training_dummy(
server
.state
.create_npc(
pos,
stats,
Some(health),
poise,
Inventory::new_empty(),
body,
)
.create_npc(pos, stats, health, poise, Inventory::new_empty(), body)
.with(comp::Vel(vel))
.with(comp::MountState::Unmounted)
.build();
@ -1013,17 +1006,19 @@ fn handle_spawn_airship(
200.0,
)
});
server
let mut builder = server
.state
.create_ship(pos, comp::ship::Body::DefaultAirship, 1, destination)
.with(comp::Scale(comp::ship::AIRSHIP_SCALE))
.create_ship(pos, comp::ship::Body::DefaultAirship, 1, true)
.with(LightEmitter {
col: Rgb::new(1.0, 0.65, 0.2),
strength: 2.0,
flicker: 1.0,
animated: true,
})
.build();
});
if let Some(pos) = destination {
builder = builder.with(comp::Agent::with_destination(pos))
}
builder.build();
server.notify_client(
client,

View File

@ -48,7 +48,7 @@ pub fn handle_create_npc(
server: &mut Server,
pos: Pos,
stats: Stats,
health: Option<Health>,
health: Health,
poise: Poise,
loadout: Loadout,
body: Body,
@ -109,6 +109,26 @@ pub fn handle_create_npc(
entity.build();
}
#[allow(clippy::too_many_arguments)]
pub fn handle_create_ship(
server: &mut Server,
pos: comp::Pos,
ship: comp::ship::Body,
level: u16,
mountable: bool,
agent: Option<Agent>,
rtsim_entity: Option<RtSimEntity>,
) {
let mut entity = server.state.create_ship(pos, ship, level, mountable);
if let Some(agent) = agent {
entity = entity.with(agent);
}
if let Some(rtsim_entity) = rtsim_entity {
entity = entity.with(rtsim_entity);
}
entity.build();
}
#[allow(clippy::too_many_arguments)]
pub fn handle_shoot(
server: &mut Server,

View File

@ -2,8 +2,8 @@ use crate::{state_ext::StateExt, Server};
use common::event::{EventBus, ServerEvent};
use common_base::span;
use entity_creation::{
handle_beam, handle_create_npc, handle_create_waypoint, handle_initialize_character,
handle_loaded_character_data, handle_shockwave, handle_shoot,
handle_beam, handle_create_npc, handle_create_ship, handle_create_waypoint,
handle_initialize_character, handle_loaded_character_data, handle_shockwave, handle_shoot,
};
use entity_manipulation::{
handle_aura, handle_buff, handle_combo_change, handle_damage, handle_delete, handle_destroy,
@ -162,6 +162,14 @@ impl Server {
home_chunk,
rtsim_entity,
),
ServerEvent::CreateShip {
pos,
ship,
level,
mountable,
agent,
rtsim_entity,
} => handle_create_ship(self, pos, ship, level, mountable, agent, rtsim_entity),
ServerEvent::CreateWaypoint(pos) => handle_create_waypoint(self, pos),
ServerEvent::ClientDisconnect(entity) => {
frontend_events.push(handle_client_disconnect(self, entity))

View File

@ -2,7 +2,7 @@
use super::*;
use common::{
comp::{self, inventory::loadout_builder::LoadoutBuilder, ship},
comp::{self, inventory::loadout_builder::LoadoutBuilder},
event::{EventBus, ServerEvent},
resources::{DeltaTime, Time},
terrain::TerrainGrid,
@ -102,38 +102,49 @@ impl<'a> System<'a> for Sys {
.map(|e| e as f32)
+ Vec3::new(0.5, 0.5, 0.0);
let body = entity.get_body();
server_emitter.emit(ServerEvent::CreateNpc {
pos: comp::Pos(spawn_pos),
stats: comp::Stats::new(entity.get_name()),
health: match body {
comp::Body::Ship(ship::Body::DefaultAirship) => None,
_ => Some(comp::Health::new(body, 10)),
let pos = comp::Pos(spawn_pos);
let agent = Some(comp::Agent::new(
None,
matches!(body, comp::Body::Humanoid(_)),
None,
&body,
false,
));
let rtsim_entity = Some(RtSimEntity(id));
let event = match body {
comp::Body::Ship(ship) => ServerEvent::CreateShip {
pos,
ship,
level: 1,
mountable: false,
agent,
rtsim_entity,
},
loadout: match body {
comp::Body::Humanoid(_) => entity.get_loadout(),
_ => LoadoutBuilder::new().build(),
_ => ServerEvent::CreateNpc {
pos: comp::Pos(spawn_pos),
stats: comp::Stats::new(entity.get_name()),
health: comp::Health::new(body, 10),
loadout: match body {
comp::Body::Humanoid(_) => entity.get_loadout(),
_ => LoadoutBuilder::new().build(),
},
poise: comp::Poise::new(body),
body,
agent,
alignment: match body {
comp::Body::Humanoid(_) => comp::Alignment::Npc,
_ => comp::Alignment::Wild,
},
scale: match body {
comp::Body::Ship(_) => comp::Scale(comp::ship::AIRSHIP_SCALE),
_ => comp::Scale(1.0),
},
drop_item: None,
home_chunk: None,
rtsim_entity,
},
poise: comp::Poise::new(body),
body,
agent: Some(comp::Agent::new(
None,
matches!(body, comp::Body::Humanoid(_)),
None,
&body,
false,
)),
alignment: match body {
comp::Body::Humanoid(_) => comp::Alignment::Npc,
_ => comp::Alignment::Wild,
},
scale: match body {
comp::Body::Ship(_) => comp::Scale(comp::ship::AIRSHIP_SCALE),
_ => comp::Scale(1.0),
},
drop_item: None,
home_chunk: None,
rtsim_entity: Some(RtSimEntity(id)),
});
};
server_emitter.emit(event);
}
// Update rtsim with real entity data

View File

@ -35,7 +35,7 @@ pub trait StateExt {
&mut self,
pos: comp::Pos,
stats: comp::Stats,
health: Option<comp::Health>,
health: comp::Health,
poise: comp::Poise,
inventory: comp::Inventory,
body: comp::Body,
@ -47,7 +47,7 @@ pub trait StateExt {
pos: comp::Pos,
ship: comp::ship::Body,
level: u16,
destination: Option<Vec3<f32>>,
mountable: bool,
) -> EcsEntityBuilder;
/// Build a projectile
fn create_projectile(
@ -162,13 +162,12 @@ impl StateExt for State {
&mut self,
pos: comp::Pos,
stats: comp::Stats,
health: Option<comp::Health>,
health: comp::Health,
poise: comp::Poise,
inventory: comp::Inventory,
body: comp::Body,
) -> EcsEntityBuilder {
let mut res = self
.ecs_mut()
self.ecs_mut()
.create_entity_synced()
.with(pos)
.with(comp::Vel(Vec3::zero()))
@ -200,11 +199,9 @@ impl StateExt for State {
.unwrap_or(None)
.unwrap_or(0),
))
.with(stats);
if let Some(health) = health {
res = res.with(health);
}
res.with(poise)
.with(stats)
.with(health)
.with(poise)
.with(comp::Alignment::Npc)
.with(comp::Gravity(1.0))
.with(comp::CharacterState::default())
@ -235,7 +232,7 @@ impl StateExt for State {
pos: comp::Pos,
ship: comp::ship::Body,
level: u16,
destination: Option<Vec3<f32>>,
mountable: bool,
) -> EcsEntityBuilder {
let mut builder = self
.ecs_mut()
@ -249,19 +246,20 @@ impl StateExt for State {
})
.with(comp::Body::Ship(ship))
.with(comp::Gravity(1.0))
.with(comp::Scale(comp::ship::AIRSHIP_SCALE))
.with(comp::Controller::default())
.with(comp::inventory::Inventory::new_empty())
.with(comp::CharacterState::default())
// TODO: some of these are required in order for the character_behavior system to
// recognize a possesed airship; that system should be refactored to use `.maybe()`
.with(comp::Energy::new(ship.into(), level))
.with(comp::Health::new(ship.into(), level))
//.with(comp::Health::new(ship.into(), level))
.with(comp::Stats::new("Airship".to_string()))
.with(comp::Buffs::default())
.with(comp::MountState::Unmounted)
.with(comp::Combo::default());
if let Some(pos) = destination {
builder = builder.with(comp::Agent::with_destination(pos))
if mountable {
builder = builder.with(comp::MountState::Unmounted);
}
builder
}

View File

@ -181,7 +181,7 @@ impl<'a> System<'a> for Sys {
server_emitter.emit(ServerEvent::CreateNpc {
pos: Pos(entity.pos),
stats,
health: Some(health),
health,
poise,
loadout,
agent: if entity.has_agency {