using System.Collections; using System.Collections.Generic; using UnityEngine; //using KairoEngine.MaxRaider; using RootMotion.Dynamics; using KairoEngine.Core; namespace KairoEngine.CharacterSystem { public class SerializerCharacter : MonoBehaviour { //public string characterPrefabName; //[HideInInspector] public bool loadedFromData = false; private CharacterController character; //private DamageController damageController; void Start() { // Get Character Controller character = gameObject.GetComponent(); if(character == null) Debug.LogError("No CharacterController found for SerializeWorldItem in GameObject " + gameObject.name); } void OnEnable() { SerializationEvents.OnSerializeData += Serialize; } void OnDisable() { SerializationEvents.OnSerializeData -= Serialize; } /* private void Serialize() { string name = character.unique_name; Vector3 position = transform.position; Quaternion rotation = transform.rotation; int hp = damageController.HP; int maxHp = damageController.maxHP; bool isDead = character.IsDead(); ItemRef equipedItem = character.GetEquipedItem(); ItemFirearmRef equipedItemRef = null; if(equipedItem != null) { if(equipedItem.item.category == ItemType.firearm) { equipedItemRef = (ItemFirearmRef)character.GetEquipedItem(); } else if(equipedItem.item.category == ItemType.handWeapon) { // Maybe do something here? } } List inventory = itemContainer.inventory; DataCharacter dataCharacter = new DataCharacter(name, characterPrefabName, position, rotation, hp, maxHp, isDead, equipedItemRef, inventory); if(characterRandomizer != null) { string body = "", hair = "", beard = ""; if(characterRandomizer.currentBody != null) body = characterRandomizer.currentBody.name; if(characterRandomizer.currentHair != null) hair = characterRandomizer.currentHair.name; if(characterRandomizer.currentBeard != null) beard = characterRandomizer.currentBeard.name; int material = characterRandomizer.currentBodyMaterial; dataCharacter.SetCharacterVisuals(body, hair, beard, material); } dataCharacter.bones = GetBones(); //DataManager.instance.currentSceneData.charactersData.Add(dataCharacter); } */ private void Serialize() { CharacterData data = ScriptableObject.CreateInstance(); data.characterName = character.characterName; data.id = character.id; data.race = character.race; data.faction = character.faction; data.gender = character.gender; data.age = character.age; data.hitpoints = character.damageController.HP; data.maxHitpoints = character.damageController.maxHP; data.armor = character.damageController.armor; data.bodyModel = character.bodyModelName; data.bodyMaterial = character.bodyMaterialName; data.headAttachment = character.headAttachmentName; data.faceAttachment = character.faceAttachmentName; data.hasSpawned = true; data.isDead = character.IsDead(); data.position = character.transform.position; data.rotation = character.transform.rotation; data.itemList = character.itemContainer.inventory; if(data.isDead) data.bones = GetBones(character); else data.bones = new List(); DataManager.instance.currentSceneData.characterDataList.Add(data); } private List GetBones(CharacterController character) { List bones = new List(); Transform[] transforms; if(character.puppetMaster != null) transforms = character.puppetMaster.GetComponentsInChildren(true); else transforms = gameObject.GetComponentsInChildren(); for (int i = 0; i < transforms.Length; i++) { DataBone bone = new DataBone(transforms[i].gameObject.name, transforms[i].position, transforms[i].rotation); bones.Add(bone); } return bones; } /* public void SetBones(List bones) { PuppetMaster puppetMaster = gameObject.transform.root.GetComponentInChildren(); CharacterController character = gameObject.GetComponent(); if(character != null) character.animator.enabled = false; Destroy(puppetMaster.gameObject); Transform[] transforms = transform.root.GetComponentsInChildren(); for (int i = 0; i < bones.Count; i++) { for (int a = 0; a < transforms.Length; a++) { if(bones[i].name == transforms[a].gameObject.name) { transforms[a].position = bones[i].position; transforms[a].rotation = bones[i].rotation; } } } } */ } }