SerializerCharacter.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //using KairoEngine.MaxRaider;
  5. using RootMotion.Dynamics;
  6. using KairoEngine.Core;
  7. namespace KairoEngine.CharacterSystem
  8. {
  9. public class SerializerCharacter : MonoBehaviour
  10. {
  11. //public string characterPrefabName;
  12. //[HideInInspector] public bool loadedFromData = false;
  13. private CharacterController character;
  14. //private DamageController damageController;
  15. void Start()
  16. {
  17. // Get Character Controller
  18. character = gameObject.GetComponent<CharacterController>();
  19. if(character == null) Debug.LogError("No CharacterController found for SerializeWorldItem in GameObject " + gameObject.name);
  20. }
  21. void OnEnable()
  22. {
  23. SerializationEvents.OnSerializeData += Serialize;
  24. }
  25. void OnDisable()
  26. {
  27. SerializationEvents.OnSerializeData -= Serialize;
  28. }
  29. /*
  30. private void Serialize()
  31. {
  32. string name = character.unique_name;
  33. Vector3 position = transform.position;
  34. Quaternion rotation = transform.rotation;
  35. int hp = damageController.HP;
  36. int maxHp = damageController.maxHP;
  37. bool isDead = character.IsDead();
  38. ItemRef equipedItem = character.GetEquipedItem();
  39. ItemFirearmRef equipedItemRef = null;
  40. if(equipedItem != null)
  41. {
  42. if(equipedItem.item.category == ItemType.firearm)
  43. {
  44. equipedItemRef = (ItemFirearmRef)character.GetEquipedItem();
  45. }
  46. else if(equipedItem.item.category == ItemType.handWeapon)
  47. {
  48. // Maybe do something here?
  49. }
  50. }
  51. List<ItemRef> inventory = itemContainer.inventory;
  52. DataCharacter dataCharacter = new DataCharacter(name, characterPrefabName, position, rotation, hp, maxHp, isDead, equipedItemRef, inventory);
  53. if(characterRandomizer != null)
  54. {
  55. string body = "", hair = "", beard = "";
  56. if(characterRandomizer.currentBody != null) body = characterRandomizer.currentBody.name;
  57. if(characterRandomizer.currentHair != null) hair = characterRandomizer.currentHair.name;
  58. if(characterRandomizer.currentBeard != null) beard = characterRandomizer.currentBeard.name;
  59. int material = characterRandomizer.currentBodyMaterial;
  60. dataCharacter.SetCharacterVisuals(body, hair, beard, material);
  61. }
  62. dataCharacter.bones = GetBones();
  63. //DataManager.instance.currentSceneData.charactersData.Add(dataCharacter);
  64. }
  65. */
  66. private void Serialize()
  67. {
  68. CharacterData data = ScriptableObject.CreateInstance<CharacterData>();
  69. data.characterName = character.characterName;
  70. data.id = character.id;
  71. data.race = character.race;
  72. data.faction = character.faction;
  73. data.gender = character.gender;
  74. data.age = character.age;
  75. data.hitpoints = character.damageController.HP;
  76. data.maxHitpoints = character.damageController.maxHP;
  77. data.armor = character.damageController.armor;
  78. data.bodyModel = character.bodyModelName;
  79. data.bodyMaterial = character.bodyMaterialName;
  80. data.headAttachment = character.headAttachmentName;
  81. data.faceAttachment = character.faceAttachmentName;
  82. data.hasSpawned = true;
  83. data.isDead = character.IsDead();
  84. data.position = character.transform.position;
  85. data.rotation = character.transform.rotation;
  86. data.itemList = character.itemContainer.inventory;
  87. if(data.isDead) data.bones = GetBones(character);
  88. else data.bones = new List<DataBone>();
  89. DataManager.instance.currentSceneData.characterDataList.Add(data);
  90. }
  91. private List<DataBone> GetBones(CharacterController character)
  92. {
  93. List<DataBone> bones = new List<DataBone>();
  94. Transform[] transforms;
  95. if(character.puppetMaster != null) transforms = character.puppetMaster.GetComponentsInChildren<Transform>(true);
  96. else transforms = gameObject.GetComponentsInChildren<Transform>();
  97. for (int i = 0; i < transforms.Length; i++)
  98. {
  99. DataBone bone = new DataBone(transforms[i].gameObject.name, transforms[i].position, transforms[i].rotation);
  100. bones.Add(bone);
  101. }
  102. return bones;
  103. }
  104. /*
  105. public void SetBones(List<DataBone> bones)
  106. {
  107. PuppetMaster puppetMaster = gameObject.transform.root.GetComponentInChildren<PuppetMaster>();
  108. CharacterController character = gameObject.GetComponent<CharacterController>();
  109. if(character != null) character.animator.enabled = false;
  110. Destroy(puppetMaster.gameObject);
  111. Transform[] transforms = transform.root.GetComponentsInChildren<Transform>();
  112. for (int i = 0; i < bones.Count; i++)
  113. {
  114. for (int a = 0; a < transforms.Length; a++)
  115. {
  116. if(bones[i].name == transforms[a].gameObject.name)
  117. {
  118. transforms[a].position = bones[i].position;
  119. transforms[a].rotation = bones[i].rotation;
  120. }
  121. }
  122. }
  123. }
  124. */
  125. }
  126. }