Cut down on player entity generation boilerplate

This commit is contained in:
Timothy Warren 2022-01-25 15:04:55 -05:00
parent 408f9e6ab9
commit 2df20956bb
2 changed files with 77 additions and 74 deletions

View File

@ -177,6 +177,15 @@ pub struct HungerClock {
pub duration: i32, pub duration: i32,
} }
impl Default for HungerClock {
fn default() -> Self {
HungerClock {
state: HungerState::WellFed,
duration: 20,
}
}
}
#[derive(Component, Debug, Serialize, Deserialize, Clone)] #[derive(Component, Debug, Serialize, Deserialize, Clone)]
pub struct Door { pub struct Door {
pub open: bool, pub open: bool,
@ -212,6 +221,17 @@ pub struct Attributes {
pub intelligence: Attribute, pub intelligence: Attribute,
} }
impl Attributes {
pub fn new(base: i32) -> Self {
Attributes {
might: Attribute::new(base),
fitness: Attribute::new(base),
quickness: Attribute::new(base),
intelligence: Attribute::new(base),
}
}
}
#[derive(Component, Debug, Default, Serialize, Deserialize, Clone)] #[derive(Component, Debug, Default, Serialize, Deserialize, Clone)]
pub struct Skills { pub struct Skills {
pub skills: HashMap<Skill, i32>, pub skills: HashMap<Skill, i32>,
@ -255,6 +275,23 @@ pub struct Pools {
pub god_mode: bool, pub god_mode: bool,
} }
impl Pools {
pub fn player_pools() -> Self {
use crate::gamesystem::{mana_at_level, player_hp_at_level};
Pools {
hit_points: Pool::new(player_hp_at_level(11, 1)),
mana: Pool::new(mana_at_level(11, 1)),
xp: 0,
level: 1,
total_weight: 0.,
total_initiative_penalty: 0.,
gold: 0.,
god_mode: false,
}
}
}
#[derive(Serialize, Deserialize, Clone)] #[derive(Serialize, Deserialize, Clone)]
pub struct NaturalAttack { pub struct NaturalAttack {
pub name: String, pub name: String,
@ -288,7 +325,16 @@ pub struct LightSource {
pub range: i32, pub range: i32,
} }
#[derive(Component, Debug, Serialize, Deserialize, Clone)] impl LightSource {
pub fn torch_light() -> Self {
LightSource {
color: crate::colors::TORCH_LIGHT,
range: 8,
}
}
}
#[derive(Component, Debug, Default, Serialize, Deserialize, Clone)]
pub struct Initiative { pub struct Initiative {
pub current: i32, pub current: i32,
} }
@ -388,6 +434,17 @@ pub struct AttributeBonus {
pub intelligence: Option<i32>, pub intelligence: Option<i32>,
} }
impl AttributeBonus {
pub fn hangover() -> Self {
AttributeBonus {
might: Some(-1),
fitness: None,
quickness: Some(-1),
intelligence: Some(-1),
}
}
}
#[derive(Component, Debug, Serialize, Deserialize, Clone)] #[derive(Component, Debug, Serialize, Deserialize, Clone)]
pub struct Consumable { pub struct Consumable {
pub max_charges: i32, pub max_charges: i32,
@ -410,7 +467,7 @@ pub struct KnownSpell {
pub mana_cost: i32, pub mana_cost: i32,
} }
#[derive(Component, Debug, Serialize, Deserialize, Clone)] #[derive(Component, Debug, Default, Serialize, Deserialize, Clone)]
pub struct KnownSpells { pub struct KnownSpells {
pub spells: Vec<KnownSpell>, pub spells: Vec<KnownSpell>,
} }

View File

@ -5,7 +5,6 @@ use ::specs::prelude::*;
use ::specs::saveload::{MarkedBuilder, SimpleMarker}; use ::specs::saveload::{MarkedBuilder, SimpleMarker};
use crate::components::*; use crate::components::*;
use crate::gamesystem::{mana_at_level, player_hp_at_level};
use crate::random_table::RandomTable; use crate::random_table::RandomTable;
use crate::raws::{ use crate::raws::{
get_spawn_table_for_depth, spawn_all_spells, spawn_named_entity, SpawnType, RAWS, get_spawn_table_for_depth, spawn_all_spells, spawn_named_entity, SpawnType, RAWS,
@ -17,10 +16,7 @@ pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity {
spawn_all_spells(ecs); spawn_all_spells(ecs);
let player = ecs let player = ecs
.create_entity() .create_entity()
.with(Position { .with(Position::from((player_x, player_y)))
x: player_x,
y: player_y,
})
.with(Renderable { .with(Renderable {
glyph: rltk::to_cp437('@'), glyph: rltk::to_cp437('@'),
fg: colors::YELLOW, fg: colors::YELLOW,
@ -30,92 +26,42 @@ pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity {
.with(Player {}) .with(Player {})
.with(Viewshed::default()) .with(Viewshed::default())
.with(Name::from("Player")) .with(Name::from("Player"))
.with(HungerClock { .with(HungerClock::default())
state: HungerState::WellFed, .with(Attributes::new(11))
duration: 20,
})
.with(Attributes {
might: Attribute::new(11),
fitness: Attribute::new(11),
quickness: Attribute::new(11),
intelligence: Attribute::new(11),
})
.with(Skills::new(1)) .with(Skills::new(1))
.with(Pools { .with(Pools::player_pools())
hit_points: Pool::new(player_hp_at_level(11, 1)),
mana: Pool::new(mana_at_level(11, 1)),
xp: 0,
level: 1,
total_weight: 0.,
total_initiative_penalty: 0.,
gold: 0.,
god_mode: false,
})
.with(EquipmentChanged {}) .with(EquipmentChanged {})
.with(LightSource { .with(LightSource::torch_light())
color: colors::TORCH_LIGHT, .with(Initiative::default())
range: 8,
})
.with(Initiative { current: 0 })
.with(Faction::from("Player")) .with(Faction::from("Player"))
.with(KnownSpells { .with(KnownSpells::default())
spells: vec![KnownSpell {
display_name: "Zap".to_string(),
mana_cost: 1,
}],
})
.marked::<SimpleMarker<SerializeMe>>() .marked::<SimpleMarker<SerializeMe>>()
.build(); .build();
// Starting equipment // Starting equipment
spawn_named_entity( let starting_equipment = vec![
&RAWS.lock().unwrap(),
ecs,
"Rusty Longsword", "Rusty Longsword",
SpawnType::Equipped { by: player },
);
spawn_named_entity(
&RAWS.lock().unwrap(),
ecs,
"Dried Sausage", "Dried Sausage",
SpawnType::Carried { by: player },
);
spawn_named_entity(
&RAWS.lock().unwrap(),
ecs,
"Beer", "Beer",
SpawnType::Carried { by: player },
);
spawn_named_entity(
&RAWS.lock().unwrap(),
ecs,
"Stained Tunic", "Stained Tunic",
SpawnType::Equipped { by: player },
);
spawn_named_entity(
&RAWS.lock().unwrap(),
ecs,
"Torn Trousers", "Torn Trousers",
SpawnType::Equipped { by: player },
);
spawn_named_entity(
&RAWS.lock().unwrap(),
ecs,
"Old Boots", "Old Boots",
SpawnType::Equipped { by: player }, ];
); for equipment in starting_equipment.iter() {
spawn_named_entity(
&RAWS.lock().unwrap(),
ecs,
*equipment,
SpawnType::Carried { by: player },
);
}
// Starting hangover // Starting hangover
ecs.create_entity() ecs.create_entity()
.with(StatusEffect { target: player }) .with(StatusEffect { target: player })
.with(Duration { turns: 10 }) .with(Duration { turns: 10 })
.with(Name::from("Hangover")) .with(Name::from("Hangover"))
.with(AttributeBonus { .with(AttributeBonus::hangover())
might: Some(-1),
fitness: None,
quickness: Some(-1),
intelligence: Some(-1),
})
.marked::<SimpleMarker<SerializeMe>>() .marked::<SimpleMarker<SerializeMe>>()
.build(); .build();