2019-06-03 22:11:27 +00:00
|
|
|
use crate::assets;
|
|
|
|
use lazy_static::lazy_static;
|
2019-06-01 19:24:17 +00:00
|
|
|
use rand::seq::SliceRandom;
|
2019-06-01 19:49:34 +00:00
|
|
|
use serde_json;
|
2019-06-15 07:54:47 +00:00
|
|
|
use std::str::FromStr;
|
2019-06-15 11:48:14 +00:00
|
|
|
use std::sync::Arc;
|
2019-06-01 19:24:17 +00:00
|
|
|
|
2019-11-23 08:26:39 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
2019-06-01 19:24:17 +00:00
|
|
|
pub enum NpcKind {
|
2019-06-03 22:38:44 +00:00
|
|
|
Humanoid,
|
2019-06-01 19:24:17 +00:00
|
|
|
Wolf,
|
2019-06-01 19:49:34 +00:00
|
|
|
Pig,
|
2019-06-01 19:24:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NpcKind {
|
2019-07-01 20:42:43 +00:00
|
|
|
fn as_str(self) -> &'static str {
|
|
|
|
match self {
|
2019-06-03 22:38:44 +00:00
|
|
|
NpcKind::Humanoid => "humanoid",
|
2019-06-01 19:24:17 +00:00
|
|
|
NpcKind::Wolf => "wolf",
|
2019-06-01 19:49:34 +00:00
|
|
|
NpcKind::Pig => "pig",
|
2019-06-01 19:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 07:54:47 +00:00
|
|
|
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),
|
2019-10-23 02:56:45 +00:00
|
|
|
|
2019-06-15 11:48:14 +00:00
|
|
|
_ => Err(()),
|
2019-06-15 07:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 22:11:27 +00:00
|
|
|
lazy_static! {
|
2019-08-12 16:11:06 +00:00
|
|
|
static ref NPC_NAMES_JSON: Arc<serde_json::Value> = assets::load_expect("common.npc_names");
|
2019-06-03 22:11:27 +00:00
|
|
|
}
|
2019-06-01 19:49:34 +00:00
|
|
|
|
2019-06-01 19:24:17 +00:00
|
|
|
pub fn get_npc_name(npc_type: NpcKind) -> String {
|
2019-06-03 22:11:27 +00:00
|
|
|
let npc_names = NPC_NAMES_JSON
|
2019-06-01 19:24:17 +00:00
|
|
|
.get(npc_type.as_str())
|
|
|
|
.expect("accessing json using NPC type provided as key")
|
|
|
|
.as_array()
|
|
|
|
.expect("parsing accessed json value into an array");
|
|
|
|
let npc_name = npc_names
|
|
|
|
.choose(&mut rand::thread_rng())
|
|
|
|
.expect("getting a random NPC name")
|
|
|
|
.as_str()
|
|
|
|
.expect("parsing NPC name json value into a &str");
|
|
|
|
String::from(npc_name)
|
|
|
|
}
|