123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- 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<CharacterController>();
- 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<ItemRef> 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<CharacterData>();
- 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<DataBone>();
- DataManager.instance.currentSceneData.characterDataList.Add(data);
- }
- private List<DataBone> GetBones(CharacterController character)
- {
- List<DataBone> bones = new List<DataBone>();
- Transform[] transforms;
- if(character.puppetMaster != null) transforms = character.puppetMaster.GetComponentsInChildren<Transform>(true);
- else transforms = gameObject.GetComponentsInChildren<Transform>();
- 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<DataBone> bones)
- {
- PuppetMaster puppetMaster = gameObject.transform.root.GetComponentInChildren<PuppetMaster>();
- CharacterController character = gameObject.GetComponent<CharacterController>();
- if(character != null) character.animator.enabled = false;
- Destroy(puppetMaster.gameObject);
- Transform[] transforms = transform.root.GetComponentsInChildren<Transform>();
- 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;
- }
- }
- }
- }
- */
- }
- }
|