An unofficial collection of APIs used in FreeJam games and mods
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.5KB

  1. use crate::techblox::{hashname, brute_force, Parsable};
  2. use libfj_parsable_macro_derive::*;
  3. /// An entity's header information.
  4. ///
  5. /// This holds entity data common to all entities, such as entity type and ID.
  6. #[derive(Clone, Copy, Parsable)]
  7. pub struct EntityHeader {
  8. /// Entity type hash
  9. pub hash: u32,
  10. /// Entity identifier
  11. pub entity_id: u32,
  12. /// Entity group identifier
  13. pub group_id: u32,
  14. /// Count of serialized components after this header (this is not the size in bytes)
  15. pub component_count: u8,
  16. }
  17. impl EntityHeader {
  18. /// Guess the original name from the hashed value by brute-force.
  19. ///
  20. /// This is slow and cannot guarantee a correct result. Use is discouraged.
  21. pub fn guess_name(&self) -> String {
  22. brute_force(self.hash)
  23. }
  24. /// Create an entity header using the hash of `name`.
  25. pub fn from_name(name: &str, entity_id: u32, group_id: u32, component_count: u8) -> Self {
  26. Self {
  27. hash: hashname(name),
  28. entity_id,
  29. group_id,
  30. component_count,
  31. }
  32. }
  33. }
  34. impl std::convert::Into<EntityGroupID> for EntityHeader {
  35. fn into(self) -> EntityGroupID {
  36. EntityGroupID {
  37. entity_id: self.entity_id,
  38. group_id: self.group_id,
  39. }
  40. }
  41. }
  42. /// Entity identifier common among all components in the same entity
  43. #[derive(Clone, Copy, Parsable)]
  44. pub struct EntityGroupID {
  45. /// Entity identifier
  46. pub entity_id: u32,
  47. /// Entity group identifier
  48. pub group_id: u32
  49. }