123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- extern crate specs;
- use specs::prelude::*;
- extern crate specs_derive;
- extern crate rltk;
- use rltk::{RGB};
- use serde::{Serialize, Deserialize};
- use specs::saveload::{Marker, ConvertSaveload};
- use specs::error::NoError;
- #[derive(Component, Serialize, Deserialize, Clone)]
- pub struct Position {
- pub x: i32,
- pub y: i32,
- }
- #[derive(Component, Serialize, Deserialize, Clone)]
- pub struct Renderable {
- pub glyph: u8,
- pub fg: RGB,
- pub bg: RGB,
- pub render_order : i32
- }
-
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct Player {}
- #[derive(Component, Serialize, Deserialize, Clone)]
- pub struct Viewshed {
- pub visible_tiles : Vec<rltk::Point>,
- pub range : i32,
- pub dirty : bool
- }
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct Monster {}
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct Name {
- pub name : String
- }
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct BlocksTile {}
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct CombatStats {
- pub max_hp : i32,
- pub hp : i32,
- pub defense : i32,
- pub power : i32
- }
- // See wrapper below for serialization
- #[derive(Component, Debug)]
- pub struct WantsToMelee {
- pub target : Entity
- }
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct SufferDamage {
- pub amount : i32
- }
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct Item {}
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct Consumable {}
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct Ranged {
- pub range : i32
- }
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct InflictsDamage {
- pub damage : i32
- }
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct AreaOfEffect {
- pub radius : i32
- }
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct Confusion {
- pub turns : i32
- }
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct ProvidesHealing {
- pub heal_amount : i32
- }
- // See wrapper below for serialization
- #[derive(Component, Debug)]
- pub struct InBackpack {
- pub owner : Entity
- }
- // See wrapper below for serialization
- #[derive(Component, Debug)]
- pub struct WantsToPickupItem {
- pub collected_by : Entity,
- pub item : Entity
- }
- // See wrapper below for serialization
- #[derive(Component, Debug)]
- pub struct WantsToUseItem {
- pub item : Entity,
- pub target : Option<rltk::Point>
- }
- // See wrapper below for serialization
- #[derive(Component, Debug)]
- pub struct WantsToDropItem {
- pub item : Entity
- }
- // See wrapper below for serialization
- #[derive(Component, Debug)]
- pub struct WantsToRemoveItem {
- pub item : Entity
- }
- #[derive(PartialEq, Copy, Clone, Serialize, Deserialize)]
- pub enum EquipmentSlot { Melee, Shield }
- #[derive(Component, Serialize, Deserialize, Clone)]
- pub struct Equippable {
- pub slot : EquipmentSlot
- }
- // See wrapper below for serialization
- #[derive(Component)]
- pub struct Equipped {
- pub owner : Entity,
- pub slot : EquipmentSlot
- }
- #[derive(Component, Serialize, Deserialize, Clone)]
- pub struct MeleePowerBonus {
- pub power : i32
- }
- #[derive(Component, Serialize, Deserialize, Clone)]
- pub struct DefenseBonus {
- pub defense : i32
- }
- #[derive(Component, Serialize, Deserialize, Clone)]
- pub struct ParticleLifetime {
- pub lifetime_ms : f32
- }
- #[derive(Serialize, Deserialize, Copy, Clone, PartialEq)]
- pub enum HungerState { WellFed, Normal, Hungry, Starving }
- #[derive(Component, Serialize, Deserialize, Clone)]
- pub struct HungerClock {
- pub state : HungerState,
- pub duration : i32
- }
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct ProvidesFood {}
- #[derive(Component, Debug, Serialize, Deserialize, Clone)]
- pub struct MagicMapper {}
- // 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 : super::map::Map
- }
- // WantsToMelee wrapper
- #[derive(Serialize, Deserialize, Clone)]
- pub struct WantsToMeleeData<M>(M);
- impl<M: Marker + Serialize> ConvertSaveload<M> for WantsToMelee
- where
- for<'de> M: Deserialize<'de>,
- {
- type Data = WantsToMeleeData<M>;
- type Error = NoError;
- fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
- where
- F: FnMut(Entity) -> Option<M>,
- {
- let marker = ids(self.target).unwrap();
- Ok(WantsToMeleeData(marker))
- }
- fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
- where
- F: FnMut(M) -> Option<Entity>,
- {
- let entity = ids(data.0).unwrap();
- Ok(WantsToMelee{target: entity})
- }
- }
- // InBackpack wrapper
- #[derive(Serialize, Deserialize, Clone)]
- pub struct InBackpackData<M>(M);
- impl<M: Marker + Serialize> ConvertSaveload<M> for InBackpack
- where
- for<'de> M: Deserialize<'de>,
- {
- type Data = InBackpackData<M>;
- type Error = NoError;
- fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
- where
- F: FnMut(Entity) -> Option<M>,
- {
- let marker = ids(self.owner).unwrap();
- Ok(InBackpackData(marker))
- }
- fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
- where
- F: FnMut(M) -> Option<Entity>,
- {
- let entity = ids(data.0).unwrap();
- Ok(InBackpack{owner: entity})
- }
- }
- // WantsToPickupItem wrapper
- #[derive(Serialize, Deserialize, Clone)]
- pub struct WantsToPickupItemData<M>(M, M);
- impl<M: Marker + Serialize> ConvertSaveload<M> for WantsToPickupItem
- where
- for<'de> M: Deserialize<'de>,
- {
- type Data = WantsToPickupItemData<M>;
- type Error = NoError;
- fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
- where
- F: FnMut(Entity) -> Option<M>,
- {
- let marker = ids(self.collected_by).unwrap();
- let marker2 = ids(self.item).unwrap();
- Ok(WantsToPickupItemData(marker, marker2))
- }
- fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
- where
- F: FnMut(M) -> Option<Entity>,
- {
- let collected_by = ids(data.0).unwrap();
- let item = ids(data.1).unwrap();
- Ok(WantsToPickupItem{collected_by, item})
- }
- }
- // WantsToUseItem wrapper
- #[derive(Serialize, Deserialize, Clone)]
- pub struct WantsToUseItemData<M>(M, Option<rltk::Point>);
- impl<M: Marker + Serialize> ConvertSaveload<M> for WantsToUseItem
- where
- for<'de> M: Deserialize<'de>,
- {
- type Data = WantsToUseItemData<M>;
- type Error = NoError;
- fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
- where
- F: FnMut(Entity) -> Option<M>,
- {
- let marker = ids(self.item).unwrap();
- Ok(WantsToUseItemData(marker, self.target))
- }
- fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
- where
- F: FnMut(M) -> Option<Entity>,
- {
- let item = ids(data.0).unwrap();
- let target = data.1;
- Ok(WantsToUseItem{item, target})
- }
- }
- // WantsToDropItem wrapper
- #[derive(Serialize, Deserialize, Clone)]
- pub struct WantsToDropItemData<M>(M);
- impl<M: Marker + Serialize> ConvertSaveload<M> for WantsToDropItem
- where
- for<'de> M: Deserialize<'de>,
- {
- type Data = WantsToDropItemData<M>;
- type Error = NoError;
- fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
- where
- F: FnMut(Entity) -> Option<M>,
- {
- let marker = ids(self.item).unwrap();
- Ok(WantsToDropItemData(marker))
- }
- fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
- where
- F: FnMut(M) -> Option<Entity>,
- {
- let entity = ids(data.0).unwrap();
- Ok(WantsToDropItem{item: entity})
- }
- }
- // WantsToRemoveItem wrapper
- #[derive(Serialize, Deserialize, Clone)]
- pub struct WantsToRemoveItemData<M>(M);
- impl<M: Marker + Serialize> ConvertSaveload<M> for WantsToRemoveItem
- where
- for<'de> M: Deserialize<'de>,
- {
- type Data = WantsToRemoveItemData<M>;
- type Error = NoError;
- fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
- where
- F: FnMut(Entity) -> Option<M>,
- {
- let marker = ids(self.item).unwrap();
- Ok(WantsToRemoveItemData(marker))
- }
- fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
- where
- F: FnMut(M) -> Option<Entity>,
- {
- let entity = ids(data.0).unwrap();
- Ok(WantsToRemoveItem{item: entity})
- }
- }
- // Equipped wrapper
- #[derive(Serialize, Deserialize, Clone)]
- pub struct EquippedData<M>(M, EquipmentSlot);
- impl<M: Marker + Serialize> ConvertSaveload<M> for Equipped
- where
- for<'de> M: Deserialize<'de>,
- {
- type Data = EquippedData<M>;
- type Error = NoError;
- fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
- where
- F: FnMut(Entity) -> Option<M>,
- {
- let marker = ids(self.owner).unwrap();
- Ok(EquippedData(marker, self.slot))
- }
- fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
- where
- F: FnMut(M) -> Option<Entity>,
- {
- let entity = ids(data.0).unwrap();
- Ok(Equipped{owner: entity, slot : data.1})
- }
- }
|