roguelike-game/src/components.rs
2022-01-06 10:00:42 -05:00

303 lines
6.4 KiB
Rust

mod enums;
mod simple;
use std::collections::HashMap;
use ::rltk::{Point, RGB};
use ::serde::{Deserialize, Serialize};
use ::specs::error::NoError;
use ::specs::prelude::*;
use ::specs::saveload::{ConvertSaveload, Marker};
use ::specs_derive::*;
pub use enums::*;
pub use simple::*;
use crate::gamesystem::attr_bonus;
#[derive(Component, ConvertSaveload, Default, Copy, Clone)]
pub struct Position {
pub x: i32,
pub y: i32,
}
impl From<(i32, i32)> for Position {
fn from(f: (i32, i32)) -> Self {
Position { x: f.0, y: f.1 }
}
}
impl From<Point> for Position {
fn from(p: Point) -> Self {
Position { x: p.x, y: p.y }
}
}
impl From<Position> for Point {
fn from(p: Position) -> Self {
Point { x: p.x, y: p.y }
}
}
#[derive(Component, ConvertSaveload, Clone)]
pub struct Renderable {
pub glyph: rltk::FontCharType,
pub fg: RGB,
pub bg: RGB,
pub render_order: i32,
}
#[derive(Component, ConvertSaveload, Clone)]
pub struct Viewshed {
pub visible_tiles: Vec<Point>,
pub range: i32,
pub dirty: bool,
}
impl Default for Viewshed {
fn default() -> Self {
Viewshed {
visible_tiles: Vec::new(),
range: 8,
dirty: true,
}
}
}
#[derive(Component, Debug, ConvertSaveload, Clone)]
pub struct Name {
pub name: String,
}
impl Name {
pub fn from<S: ToString>(s: S) -> Self {
Name {
name: s.to_string(),
}
}
}
#[derive(Component, Debug, ConvertSaveload, Clone)]
pub struct WantsToMelee {
pub target: Entity,
}
#[derive(Component, Debug, ConvertSaveload, Clone)]
pub struct SufferDamage {
pub amount: Vec<(i32, bool)>,
}
impl SufferDamage {
pub fn new_damage(
store: &mut WriteStorage<SufferDamage>,
victim: Entity,
amount: i32,
from_player: bool,
) {
if let Some(suffering) = store.get_mut(victim) {
suffering.amount.push((amount, from_player));
} else {
let dmg = SufferDamage {
amount: vec![(amount, from_player)],
};
store.insert(victim, dmg).expect("Unable to insert damage");
}
}
}
#[derive(Component, Debug, ConvertSaveload, Clone)]
pub struct Ranged {
pub range: i32,
}
#[derive(Component, Debug, ConvertSaveload, Clone)]
pub struct InflictsDamage {
pub damage: i32,
}
#[derive(Component, Debug, ConvertSaveload, Clone)]
pub struct AreaOfEffect {
pub radius: i32,
}
#[derive(Component, Debug, ConvertSaveload, Clone)]
pub struct Confusion {
pub turns: i32,
}
#[derive(Component, Debug, ConvertSaveload, Clone)]
pub struct ProvidesHealing {
pub heal_amount: i32,
}
#[derive(Component, Debug, ConvertSaveload)]
pub struct InBackpack {
pub owner: Entity,
}
#[derive(Component, Debug, ConvertSaveload)]
pub struct WantsToPickupItem {
pub collected_by: Entity,
pub item: Entity,
}
#[derive(Component, Debug, ConvertSaveload)]
pub struct WantsToUseItem {
pub item: Entity,
pub target: Option<Point>,
}
#[derive(Component, Debug, ConvertSaveload)]
pub struct WantsToDropItem {
pub item: Entity,
}
#[derive(Component, Debug, ConvertSaveload, Clone)]
pub struct WantsToRemoveItem {
pub item: Entity,
}
#[derive(Component, Serialize, Deserialize, Clone)]
pub struct Equippable {
pub slot: EquipmentSlot,
}
#[derive(Component, ConvertSaveload, Clone)]
pub struct Equipped {
pub owner: Entity,
pub slot: EquipmentSlot,
}
#[derive(Component, ConvertSaveload, Clone)]
pub struct MeleeWeapon {
pub attribute: WeaponAttribute,
pub damage_n_dice: i32,
pub damage_die_type: i32,
pub damage_bonus: i32,
pub hit_bonus: i32,
}
#[derive(Component, ConvertSaveload, Clone)]
pub struct Wearable {
pub armor_class: f32,
pub slot: EquipmentSlot,
}
#[derive(Component, Serialize, Deserialize, Clone)]
pub struct ParticleLifetime {
pub lifetime_ms: f32,
}
#[derive(Component, Serialize, Deserialize, Clone)]
pub struct HungerClock {
pub state: HungerState,
pub duration: i32,
}
#[derive(Component, Debug, Serialize, Deserialize, Clone)]
pub struct Door {
pub open: bool,
}
#[derive(Component, Debug, Serialize, Deserialize, Clone)]
pub struct Quips {
pub available: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Attribute {
pub base: i32,
pub modifiers: i32,
pub bonus: i32,
}
impl Attribute {
pub fn new(base: i32) -> Self {
Attribute {
base,
modifiers: 0,
bonus: attr_bonus(base),
}
}
}
#[derive(Component, Debug, Serialize, Deserialize, Clone)]
pub struct Attributes {
pub might: Attribute,
pub fitness: Attribute,
pub quickness: Attribute,
pub intelligence: Attribute,
}
#[derive(Component, Debug, Default, Serialize, Deserialize, Clone)]
pub struct Skills {
pub skills: HashMap<Skill, i32>,
}
impl Skills {
pub fn new(level: i32) -> Self {
let mut skills = Skills::default();
skills.skills.insert(Skill::Melee, level);
skills.skills.insert(Skill::Defense, level);
skills.skills.insert(Skill::Magic, level);
skills
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Pool {
pub max: i32,
pub current: i32,
}
#[derive(Component, Debug, Serialize, Deserialize, Clone)]
pub struct Pools {
pub hit_points: Pool,
pub mana: Pool,
pub xp: i32,
pub level: i32,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct NaturalAttack {
pub name: String,
pub damage_n_dice: i32,
pub damage_die_type: i32,
pub damage_bonus: i32,
pub hit_bonus: i32,
}
#[derive(Component, Serialize, Deserialize, Clone)]
pub struct NaturalAttackDefense {
pub armor_class: Option<i32>,
pub attacks: Vec<NaturalAttack>,
}
#[derive(Component, Debug, Serialize, Deserialize, Clone)]
pub struct LootTable {
pub table: String,
}
#[derive(Component, Serialize, Deserialize, Clone)]
pub struct OtherLevelPosition {
pub x: i32,
pub y: i32,
pub depth: i32,
}
// Serialization helper code. We need to implement ConvertSaveLoad for each type that contains an
// Entity.
pub struct SerializeMe;
// Special component that exists to help serialize the game data
#[derive(Component, Serialize, Deserialize, Clone)]
pub struct SerializationHelper {
pub map: crate::map::Map,
}
#[derive(Component, Serialize, Deserialize, Clone)]
pub struct DMSerializationHelper {
pub map: super::map::MasterDungeonMap,
}