components.rs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. extern crate specs;
  2. use specs::prelude::*;
  3. extern crate specs_derive;
  4. extern crate rltk;
  5. use rltk::{RGB};
  6. use serde::{Serialize, Deserialize};
  7. use specs::saveload::{Marker, ConvertSaveload};
  8. use specs::error::NoError;
  9. #[derive(Component, Serialize, Deserialize, Clone)]
  10. pub struct Position {
  11. pub x: i32,
  12. pub y: i32,
  13. }
  14. #[derive(Component, Serialize, Deserialize, Clone)]
  15. pub struct Renderable {
  16. pub glyph: u8,
  17. pub fg: RGB,
  18. pub bg: RGB,
  19. pub render_order : i32
  20. }
  21. #[derive(Component, Debug, Serialize, Deserialize, Clone)]
  22. pub struct Player {}
  23. #[derive(Component, Serialize, Deserialize, Clone)]
  24. pub struct Viewshed {
  25. pub visible_tiles : Vec<rltk::Point>,
  26. pub range : i32,
  27. pub dirty : bool
  28. }
  29. #[derive(Component, Debug, Serialize, Deserialize, Clone)]
  30. pub struct Monster {}
  31. #[derive(Component, Debug, Serialize, Deserialize, Clone)]
  32. pub struct Name {
  33. pub name : String
  34. }
  35. #[derive(Component, Debug, Serialize, Deserialize, Clone)]
  36. pub struct BlocksTile {}
  37. #[derive(Component, Debug, Serialize, Deserialize, Clone)]
  38. pub struct CombatStats {
  39. pub max_hp : i32,
  40. pub hp : i32,
  41. pub defense : i32,
  42. pub power : i32
  43. }
  44. // See wrapper below for serialization
  45. #[derive(Component, Debug)]
  46. pub struct WantsToMelee {
  47. pub target : Entity
  48. }
  49. #[derive(Component, Debug, Serialize, Deserialize, Clone)]
  50. pub struct SufferDamage {
  51. pub amount : i32
  52. }
  53. #[derive(Component, Debug, Serialize, Deserialize, Clone)]
  54. pub struct Item {}
  55. #[derive(Component, Debug, Serialize, Deserialize, Clone)]
  56. pub struct Consumable {}
  57. #[derive(Component, Debug, Serialize, Deserialize, Clone)]
  58. pub struct Ranged {
  59. pub range : i32
  60. }
  61. #[derive(Component, Debug, Serialize, Deserialize, Clone)]
  62. pub struct InflictsDamage {
  63. pub damage : i32
  64. }
  65. #[derive(Component, Debug, Serialize, Deserialize, Clone)]
  66. pub struct AreaOfEffect {
  67. pub radius : i32
  68. }
  69. #[derive(Component, Debug, Serialize, Deserialize, Clone)]
  70. pub struct Confusion {
  71. pub turns : i32
  72. }
  73. #[derive(Component, Debug, Serialize, Deserialize, Clone)]
  74. pub struct ProvidesHealing {
  75. pub heal_amount : i32
  76. }
  77. // See wrapper below for serialization
  78. #[derive(Component, Debug)]
  79. pub struct InBackpack {
  80. pub owner : Entity
  81. }
  82. // See wrapper below for serialization
  83. #[derive(Component, Debug)]
  84. pub struct WantsToPickupItem {
  85. pub collected_by : Entity,
  86. pub item : Entity
  87. }
  88. // See wrapper below for serialization
  89. #[derive(Component, Debug)]
  90. pub struct WantsToUseItem {
  91. pub item : Entity,
  92. pub target : Option<rltk::Point>
  93. }
  94. // See wrapper below for serialization
  95. #[derive(Component, Debug)]
  96. pub struct WantsToDropItem {
  97. pub item : Entity
  98. }
  99. #[derive(PartialEq, Copy, Clone, Serialize, Deserialize)]
  100. pub enum EquipmentSlot { Melee, Shield }
  101. #[derive(Component, Serialize, Deserialize, Clone)]
  102. pub struct Equippable {
  103. pub slot : EquipmentSlot
  104. }
  105. // See wrapper below for serialization
  106. #[derive(Component)]
  107. pub struct Equipped {
  108. pub owner : Entity,
  109. pub slot : EquipmentSlot
  110. }
  111. #[derive(Component, Serialize, Deserialize, Clone)]
  112. pub struct MeleePowerBonus {
  113. pub power : i32
  114. }
  115. #[derive(Component, Serialize, Deserialize, Clone)]
  116. pub struct DefenseBonus {
  117. pub defense : i32
  118. }
  119. // Serialization helper code. We need to implement ConvertSaveLoad for each type that contains an
  120. // Entity.
  121. pub struct SerializeMe;
  122. // Special component that exists to help serialize the game data
  123. #[derive(Component, Serialize, Deserialize, Clone)]
  124. pub struct SerializationHelper {
  125. pub map : super::map::Map
  126. }
  127. // WantsToMelee wrapper
  128. #[derive(Serialize, Deserialize, Clone)]
  129. pub struct WantsToMeleeData<M>(M);
  130. impl<M: Marker + Serialize> ConvertSaveload<M> for WantsToMelee
  131. where
  132. for<'de> M: Deserialize<'de>,
  133. {
  134. type Data = WantsToMeleeData<M>;
  135. type Error = NoError;
  136. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  137. where
  138. F: FnMut(Entity) -> Option<M>,
  139. {
  140. let marker = ids(self.target).unwrap();
  141. Ok(WantsToMeleeData(marker))
  142. }
  143. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  144. where
  145. F: FnMut(M) -> Option<Entity>,
  146. {
  147. let entity = ids(data.0).unwrap();
  148. Ok(WantsToMelee{target: entity})
  149. }
  150. }
  151. // InBackpack wrapper
  152. #[derive(Serialize, Deserialize, Clone)]
  153. pub struct InBackpackData<M>(M);
  154. impl<M: Marker + Serialize> ConvertSaveload<M> for InBackpack
  155. where
  156. for<'de> M: Deserialize<'de>,
  157. {
  158. type Data = InBackpackData<M>;
  159. type Error = NoError;
  160. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  161. where
  162. F: FnMut(Entity) -> Option<M>,
  163. {
  164. let marker = ids(self.owner).unwrap();
  165. Ok(InBackpackData(marker))
  166. }
  167. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  168. where
  169. F: FnMut(M) -> Option<Entity>,
  170. {
  171. let entity = ids(data.0).unwrap();
  172. Ok(InBackpack{owner: entity})
  173. }
  174. }
  175. // WantsToPickupItem wrapper
  176. #[derive(Serialize, Deserialize, Clone)]
  177. pub struct WantsToPickupItemData<M>(M, M);
  178. impl<M: Marker + Serialize> ConvertSaveload<M> for WantsToPickupItem
  179. where
  180. for<'de> M: Deserialize<'de>,
  181. {
  182. type Data = WantsToPickupItemData<M>;
  183. type Error = NoError;
  184. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  185. where
  186. F: FnMut(Entity) -> Option<M>,
  187. {
  188. let marker = ids(self.collected_by).unwrap();
  189. let marker2 = ids(self.item).unwrap();
  190. Ok(WantsToPickupItemData(marker, marker2))
  191. }
  192. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  193. where
  194. F: FnMut(M) -> Option<Entity>,
  195. {
  196. let collected_by = ids(data.0).unwrap();
  197. let item = ids(data.1).unwrap();
  198. Ok(WantsToPickupItem{collected_by, item})
  199. }
  200. }
  201. // WantsToUseItem wrapper
  202. #[derive(Serialize, Deserialize, Clone)]
  203. pub struct WantsToUseItemData<M>(M, Option<rltk::Point>);
  204. impl<M: Marker + Serialize> ConvertSaveload<M> for WantsToUseItem
  205. where
  206. for<'de> M: Deserialize<'de>,
  207. {
  208. type Data = WantsToUseItemData<M>;
  209. type Error = NoError;
  210. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  211. where
  212. F: FnMut(Entity) -> Option<M>,
  213. {
  214. let marker = ids(self.item).unwrap();
  215. Ok(WantsToUseItemData(marker, self.target))
  216. }
  217. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  218. where
  219. F: FnMut(M) -> Option<Entity>,
  220. {
  221. let item = ids(data.0).unwrap();
  222. let target = data.1;
  223. Ok(WantsToUseItem{item, target})
  224. }
  225. }
  226. // WantsToDropItem wrapper
  227. #[derive(Serialize, Deserialize, Clone)]
  228. pub struct WantsToDropItemData<M>(M);
  229. impl<M: Marker + Serialize> ConvertSaveload<M> for WantsToDropItem
  230. where
  231. for<'de> M: Deserialize<'de>,
  232. {
  233. type Data = WantsToDropItemData<M>;
  234. type Error = NoError;
  235. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  236. where
  237. F: FnMut(Entity) -> Option<M>,
  238. {
  239. let marker = ids(self.item).unwrap();
  240. Ok(WantsToDropItemData(marker))
  241. }
  242. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  243. where
  244. F: FnMut(M) -> Option<Entity>,
  245. {
  246. let entity = ids(data.0).unwrap();
  247. Ok(WantsToDropItem{item: entity})
  248. }
  249. }
  250. // Equipped wrapper
  251. #[derive(Serialize, Deserialize, Clone)]
  252. pub struct EquippedData<M>(M, EquipmentSlot);
  253. impl<M: Marker + Serialize> ConvertSaveload<M> for Equipped
  254. where
  255. for<'de> M: Deserialize<'de>,
  256. {
  257. type Data = EquippedData<M>;
  258. type Error = NoError;
  259. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  260. where
  261. F: FnMut(Entity) -> Option<M>,
  262. {
  263. let marker = ids(self.owner).unwrap();
  264. Ok(EquippedData(marker, self.slot))
  265. }
  266. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  267. where
  268. F: FnMut(M) -> Option<Entity>,
  269. {
  270. let entity = ids(data.0).unwrap();
  271. Ok(Equipped{owner: entity, slot : data.1})
  272. }
  273. }