components.rs 9.3 KB

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