Merge all spawn commands into one

This commit is contained in:
liids
2019-06-15 09:54:47 +02:00
parent d449972bc4
commit c859371ddb
2 changed files with 72 additions and 93 deletions

View File

@ -3,7 +3,9 @@ use lazy_static::lazy_static;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use serde_json; use serde_json;
use std::sync::Arc; use std::sync::Arc;
use std::str::FromStr;
#[derive(Clone, Copy)]
pub enum NpcKind { pub enum NpcKind {
Humanoid, Humanoid,
Wolf, Wolf,
@ -20,6 +22,19 @@ impl NpcKind {
} }
} }
impl FromStr for NpcKind {
type Err = ();
fn from_str(s: &str) -> Result<Self, ()> {
match s {
"humanoid" => Ok(NpcKind::Humanoid),
"wolf" => Ok(NpcKind::Wolf),
"pig" => Ok(NpcKind::Pig),
_ => Err(())
}
}
}
lazy_static! { lazy_static! {
static ref NPC_NAMES_JSON: Arc<serde_json::Value> = static ref NPC_NAMES_JSON: Arc<serde_json::Value> =
assets::load_expect("common/npc_names.json"); assets::load_expect("common/npc_names.json");

View File

@ -86,22 +86,10 @@ lazy_static! {
handle_kill handle_kill
), ),
ChatCommand::new( ChatCommand::new(
"pig", "spawn",
"{}", "{} {} {d}",
"/pig : Spawn a test pig NPC", "/spawn <alignment> <entity> <amount> : Spawn a test entity",
handle_pet_pig handle_spawn
),
ChatCommand::new(
"wolf",
"{}",
"/wolf : Spawn a test wolf NPC",
handle_pet_wolf
),
ChatCommand::new(
"enemy",
"{}",
"/enemy : Spawn a test enemy NPC",
handle_enemy
), ),
ChatCommand::new( ChatCommand::new(
"help", "", "/help: Display this message", handle_help) "help", "", "/help: Display this message", handle_help)
@ -222,84 +210,41 @@ fn handle_tp(server: &mut Server, entity: EcsEntity, args: String, action: &Chat
} }
} }
fn handle_pet_pig(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) { fn handle_spawn(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
match server let (opt_align, opt_id, opt_amount) = scan_fmt!(&args, action.arg_fmt, String, NpcKind, u32);
.state // This should probably be just an enum and be handled with scan_fmt!
.read_component_cloned::<comp::phys::Pos>(entity) let opt_agent = alignment_to_agent(&opt_align.unwrap_or(String::new()), entity);
{ match (opt_agent, opt_id, opt_amount) {
Some(mut pos) => { (Some(agent), Some(id), Some(amount)) => {
pos.0.x += 1.0; // Temp fix TODO: Solve NaN issue with positions of pets match server
server .state
.create_npc( .read_component_cloned::<comp::phys::Pos>(entity)
pos, {
get_npc_name(NpcKind::Pig), Some(mut pos) => {
comp::Body::Quadruped(comp::QuadrupedBody::random()), pos.0.x += 1.0; // Temp fix TODO: Solve NaN issue with positions of pets
) let body = kind_to_body(id);
.with(comp::Agent::Pet { for _ in 0..amount {
target: entity, server
offset: Vec2::zero(), .create_npc(
}) pos,
.build(); get_npc_name(id),
server body,
.clients )
.notify(entity, ServerMsg::Chat("Spawned pet!".to_owned())); .with(agent)
} .build();
None => server }
server
.clients
.notify(entity, ServerMsg::Chat(format!("Spawned {} entities", amount).to_owned()));
}
None => server
.clients
.notify(entity, ServerMsg::Chat("You have no position!".to_owned())),
}
},
_ => server
.clients .clients
.notify(entity, ServerMsg::Chat("You have no position!".to_owned())), .notify(entity, ServerMsg::Chat(String::from(action.help_string))),
}
}
fn handle_pet_wolf(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
match server
.state
.read_component_cloned::<comp::phys::Pos>(entity)
{
Some(mut pos) => {
pos.0.x += 1.0; // Temp fix TODO: Solve NaN issue with positions of pets
server
.create_npc(
pos,
get_npc_name(NpcKind::Wolf),
comp::Body::QuadrupedMedium(comp::QuadrupedMediumBody::random()),
)
.with(comp::Agent::Pet {
target: entity,
offset: Vec2::zero(),
})
.build();
server
.clients
.notify(entity, ServerMsg::Chat("Spawned pet!".to_owned()));
}
None => server
.clients
.notify(entity, ServerMsg::Chat("You have no position!".to_owned())),
}
}
fn handle_enemy(server: &mut Server, entity: EcsEntity, _args: String, _action: &ChatCommand) {
match server
.state
.read_component_cloned::<comp::phys::Pos>(entity)
{
Some(mut pos) => {
pos.0.x += 1.0; // Temp fix TODO: Solve NaN issue with positions of pets
server
.create_npc(
pos,
get_npc_name(NpcKind::Humanoid),
comp::Body::Humanoid(comp::HumanoidBody::random()),
)
.with(comp::Agent::Enemy { target: None })
.build();
server
.clients
.notify(entity, ServerMsg::Chat("Spawned enemy!".to_owned()));
}
None => server
.clients
.notify(entity, ServerMsg::Chat("You have no position!".to_owned())),
} }
} }
@ -310,3 +255,22 @@ fn handle_help(server: &mut Server, entity: EcsEntity, _args: String, _action: &
.notify(entity, ServerMsg::Chat(String::from(cmd.help_string))); .notify(entity, ServerMsg::Chat(String::from(cmd.help_string)));
} }
} }
fn alignment_to_agent(alignment: &str, target: EcsEntity) -> Option<comp::Agent> {
match alignment {
"hostile" => Some(comp::Agent::Enemy { target: None }),
"friendly" => Some ( comp::Agent::Pet {
target,
offset: Vec2::zero() }
),
_ => None
}
}
fn kind_to_body(kind: NpcKind) -> comp::Body {
match kind {
NpcKind::Humanoid => comp::Body::Humanoid(comp::HumanoidBody::random()),
NpcKind::Pig => comp::Body::Quadruped(comp::QuadrupedBody::random()),
NpcKind::Wolf => comp::Body::QuadrupedMedium(comp::QuadrupedMediumBody::random()),
}
}