components.rs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. // See wrapper below for serialization
  100. #[derive(Component, Debug)]
  101. pub struct WantsToRemoveItem {
  102. pub item : Entity
  103. }
  104. #[derive(PartialEq, Copy, Clone, Serialize, Deserialize)]
  105. pub enum EquipmentSlot { Melee, Shield }
  106. #[derive(Component, Serialize, Deserialize, Clone)]
  107. pub struct Equippable {
  108. pub slot : EquipmentSlot
  109. }
  110. // See wrapper below for serialization
  111. #[derive(Component)]
  112. pub struct Equipped {
  113. pub owner : Entity,
  114. pub slot : EquipmentSlot
  115. }
  116. #[derive(Component, Serialize, Deserialize, Clone)]
  117. pub struct MeleePowerBonus {
  118. pub power : i32
  119. }
  120. #[derive(Component, Serialize, Deserialize, Clone)]
  121. pub struct DefenseBonus {
  122. pub defense : i32
  123. }
  124. // Serialization helper code. We need to implement ConvertSaveLoad for each type that contains an
  125. // Entity.
  126. pub struct SerializeMe;
  127. // Special component that exists to help serialize the game data
  128. #[derive(Component, Serialize, Deserialize, Clone)]
  129. pub struct SerializationHelper {
  130. pub map : super::map::Map
  131. }
  132. // WantsToMelee wrapper
  133. #[derive(Serialize, Deserialize, Clone)]
  134. pub struct WantsToMeleeData<M>(M);
  135. impl<M: Marker + Serialize> ConvertSaveload<M> for WantsToMelee
  136. where
  137. for<'de> M: Deserialize<'de>,
  138. {
  139. type Data = WantsToMeleeData<M>;
  140. type Error = NoError;
  141. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  142. where
  143. F: FnMut(Entity) -> Option<M>,
  144. {
  145. let marker = ids(self.target).unwrap();
  146. Ok(WantsToMeleeData(marker))
  147. }
  148. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  149. where
  150. F: FnMut(M) -> Option<Entity>,
  151. {
  152. let entity = ids(data.0).unwrap();
  153. Ok(WantsToMelee{target: entity})
  154. }
  155. }
  156. // InBackpack wrapper
  157. #[derive(Serialize, Deserialize, Clone)]
  158. pub struct InBackpackData<M>(M);
  159. impl<M: Marker + Serialize> ConvertSaveload<M> for InBackpack
  160. where
  161. for<'de> M: Deserialize<'de>,
  162. {
  163. type Data = InBackpackData<M>;
  164. type Error = NoError;
  165. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  166. where
  167. F: FnMut(Entity) -> Option<M>,
  168. {
  169. let marker = ids(self.owner).unwrap();
  170. Ok(InBackpackData(marker))
  171. }
  172. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  173. where
  174. F: FnMut(M) -> Option<Entity>,
  175. {
  176. let entity = ids(data.0).unwrap();
  177. Ok(InBackpack{owner: entity})
  178. }
  179. }
  180. // WantsToPickupItem wrapper
  181. #[derive(Serialize, Deserialize, Clone)]
  182. pub struct WantsToPickupItemData<M>(M, M);
  183. impl<M: Marker + Serialize> ConvertSaveload<M> for WantsToPickupItem
  184. where
  185. for<'de> M: Deserialize<'de>,
  186. {
  187. type Data = WantsToPickupItemData<M>;
  188. type Error = NoError;
  189. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  190. where
  191. F: FnMut(Entity) -> Option<M>,
  192. {
  193. let marker = ids(self.collected_by).unwrap();
  194. let marker2 = ids(self.item).unwrap();
  195. Ok(WantsToPickupItemData(marker, marker2))
  196. }
  197. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  198. where
  199. F: FnMut(M) -> Option<Entity>,
  200. {
  201. let collected_by = ids(data.0).unwrap();
  202. let item = ids(data.1).unwrap();
  203. Ok(WantsToPickupItem{collected_by, item})
  204. }
  205. }
  206. // WantsToUseItem wrapper
  207. #[derive(Serialize, Deserialize, Clone)]
  208. pub struct WantsToUseItemData<M>(M, Option<rltk::Point>);
  209. impl<M: Marker + Serialize> ConvertSaveload<M> for WantsToUseItem
  210. where
  211. for<'de> M: Deserialize<'de>,
  212. {
  213. type Data = WantsToUseItemData<M>;
  214. type Error = NoError;
  215. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  216. where
  217. F: FnMut(Entity) -> Option<M>,
  218. {
  219. let marker = ids(self.item).unwrap();
  220. Ok(WantsToUseItemData(marker, self.target))
  221. }
  222. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  223. where
  224. F: FnMut(M) -> Option<Entity>,
  225. {
  226. let item = ids(data.0).unwrap();
  227. let target = data.1;
  228. Ok(WantsToUseItem{item, target})
  229. }
  230. }
  231. // WantsToDropItem wrapper
  232. #[derive(Serialize, Deserialize, Clone)]
  233. pub struct WantsToDropItemData<M>(M);
  234. impl<M: Marker + Serialize> ConvertSaveload<M> for WantsToDropItem
  235. where
  236. for<'de> M: Deserialize<'de>,
  237. {
  238. type Data = WantsToDropItemData<M>;
  239. type Error = NoError;
  240. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  241. where
  242. F: FnMut(Entity) -> Option<M>,
  243. {
  244. let marker = ids(self.item).unwrap();
  245. Ok(WantsToDropItemData(marker))
  246. }
  247. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  248. where
  249. F: FnMut(M) -> Option<Entity>,
  250. {
  251. let entity = ids(data.0).unwrap();
  252. Ok(WantsToDropItem{item: entity})
  253. }
  254. }
  255. // WantsToRemoveItem wrapper
  256. #[derive(Serialize, Deserialize, Clone)]
  257. pub struct WantsToRemoveItemData<M>(M);
  258. impl<M: Marker + Serialize> ConvertSaveload<M> for WantsToRemoveItem
  259. where
  260. for<'de> M: Deserialize<'de>,
  261. {
  262. type Data = WantsToRemoveItemData<M>;
  263. type Error = NoError;
  264. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  265. where
  266. F: FnMut(Entity) -> Option<M>,
  267. {
  268. let marker = ids(self.item).unwrap();
  269. Ok(WantsToRemoveItemData(marker))
  270. }
  271. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  272. where
  273. F: FnMut(M) -> Option<Entity>,
  274. {
  275. let entity = ids(data.0).unwrap();
  276. Ok(WantsToRemoveItem{item: entity})
  277. }
  278. }
  279. // Equipped wrapper
  280. #[derive(Serialize, Deserialize, Clone)]
  281. pub struct EquippedData<M>(M, EquipmentSlot);
  282. impl<M: Marker + Serialize> ConvertSaveload<M> for Equipped
  283. where
  284. for<'de> M: Deserialize<'de>,
  285. {
  286. type Data = EquippedData<M>;
  287. type Error = NoError;
  288. fn convert_into<F>(&self, mut ids: F) -> Result<Self::Data, Self::Error>
  289. where
  290. F: FnMut(Entity) -> Option<M>,
  291. {
  292. let marker = ids(self.owner).unwrap();
  293. Ok(EquippedData(marker, self.slot))
  294. }
  295. fn convert_from<F>(data: Self::Data, mut ids: F) -> Result<Self, Self::Error>
  296. where
  297. F: FnMut(M) -> Option<Entity>,
  298. {
  299. let entity = ids(data.0).unwrap();
  300. Ok(Equipped{owner: entity, slot : data.1})
  301. }
  302. }