using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Core; using KairoEngine.Inventory; namespace KairoEngine.CharacterSystem { public class SerializerWorldItem : MonoBehaviour { [HideInInspector] public bool loadedFromData = false; private WorldItem worldItem; void Start() { worldItem = gameObject.GetComponent(); if(worldItem == null) Debug.LogError("No WorldItem found for SerializeWorldItem in GameObject " + gameObject.name); ItemBase item = ItemLibrary.GetItemByName(worldItem.itemName); if(item.category == ItemType.firearm || item.category == ItemType.handWeapon) { // Don't destroy this object if its in the hand of a character Firearm firearm = gameObject.GetComponent(); if(firearm == null) return; if(firearm.character != null) return; } if(DataManager.instance == null) return; if(DataManager.instance.currentSceneData.visited > 1 && loadedFromData == false) { Destroy(gameObject); } } void OnEnable() { SerializationEvents.OnSerializeData += Serialize; } void OnDisable() { SerializationEvents.OnSerializeData -= Serialize; } private void Serialize() { Firearm firearm = gameObject.GetComponent(); if(firearm != null) { // Don't serialzie this object if its in the hand of a character if(firearm.character != null) return; } string name = worldItem.itemName; int quantity = worldItem.quantity; string ammoName = worldItem.itemAmmoName; int ammo = worldItem.ammoQuantity; float lastUsed = worldItem.lastUsed; Vector3 position = transform.position; Quaternion rotation = transform.rotation; DataWorldItem dataWorldItem = new DataWorldItem(name, quantity, position, rotation, ammoName, ammo, lastUsed); DataManager.instance.currentSceneData.worldItemsData.Add(dataWorldItem); } } }