An unofficial collection of APIs used in FreeJam games and mods
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

66 Zeilen
2.0KB

  1. use crate::techblox::{hashname, brute_force, Parsable, blocks::lookup_name_by_hash};
  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. /// Lookup the name from the header's hash from a list of known entity names.
  25. ///
  26. /// This is much faster than guess_name() and is guaranteed to return a correct result if one exists.
  27. /// If the hash has no known correct name, None is returned instead.
  28. pub fn lookup_name(&self) -> Option<String> {
  29. if let Some(name) = lookup_name_by_hash(self.hash) {
  30. return Some(name.to_string());
  31. }
  32. None
  33. }
  34. /// Create an entity header using the hash of `name`.
  35. pub fn from_name(name: &str, entity_id: u32, group_id: u32, component_count: u8) -> Self {
  36. Self {
  37. hash: hashname(name),
  38. entity_id,
  39. group_id,
  40. component_count,
  41. }
  42. }
  43. }
  44. impl std::convert::Into<EntityGroupID> for EntityHeader {
  45. fn into(self) -> EntityGroupID {
  46. EntityGroupID {
  47. entity_id: self.entity_id,
  48. group_id: self.group_id,
  49. }
  50. }
  51. }
  52. /// Entity identifier common among all components in the same entity
  53. #[derive(Clone, Copy, Parsable)]
  54. pub struct EntityGroupID {
  55. /// Entity identifier
  56. pub entity_id: u32,
  57. /// Entity group identifier
  58. pub group_id: u32
  59. }