using UnityEngine; using System.Collections.Generic; using System.IO; namespace H2Engine { public class Bhv_MapData : MonoBehaviour { [HideInInspector] public int id; public bool showTriggerList = false; public bool showEventList = false; public bool showTransferList = false; [HideInInspector] public List triggerList = new List(); [HideInInspector] public List eventList = new List(); [HideInInspector] public List transferList = new List(); #if UNITY_EDITOR public const string NodeName_Root = "MapEditor"; public const string NodeName_TriggerList = "TriggerList"; public const string NodeName_EventList = "EventList"; public const string NodeName_TransferList = "TransferList"; public const string NodeName_Trigger = "Trigger_"; public const string NodeName_Event = "Event_"; public const string NodeName_Transfer = "Transfer_"; private int m_TriggerSeed; private int m_EventSeed; public int GetTriggerSeed() { return m_TriggerSeed; } public int GetEventSeed() { return m_EventSeed; } public int RequestTriggerID() { return m_TriggerSeed++; } public int RequestEventID() { return m_EventSeed++; } public void SetTriggerID(int id) { m_TriggerSeed = id; } public void SetEventID(int id) { m_EventSeed = id; } public void Save(BinaryWriter bw) { bw.Write(id); bw.Write(triggerList.Count); foreach (var _trigger in triggerList) { _trigger.Save(bw); } bw.Write(eventList.Count); foreach (var _event in eventList) { _event.Save(bw); } bw.Write(transferList.Count); foreach (var _transfer in transferList) { _transfer.Save(bw); } } public string SaveJson() { var _stringBuilder = new System.Text.StringBuilder(); _stringBuilder.Append("{"); _stringBuilder.Append("\"id\":").Append(id).Append(","); _stringBuilder.Append("\"triggerList\": ["); if (triggerList.Count > 0) { triggerList[0].SaveJson(_stringBuilder); for (int i = 1; i < triggerList.Count; ++i) { _stringBuilder.Append(","); triggerList[i].SaveJson(_stringBuilder); } } _stringBuilder.Append("],"); _stringBuilder.Append("\"eventList\": ["); if (eventList.Count > 0) { eventList[0].SaveJson(_stringBuilder); for (int i = 1; i < eventList.Count; ++i) { _stringBuilder.Append(","); eventList[i].SaveJson(_stringBuilder); } } _stringBuilder.Append("],"); _stringBuilder.Append("\"transferList\": ["); if (transferList.Count > 0) { transferList[0].SaveJson(_stringBuilder); for (int i = 1; i < transferList.Count; ++i) { _stringBuilder.Append(","); transferList[i].SaveJson(_stringBuilder); } } _stringBuilder.Append("]"); _stringBuilder.Append("}"); return _stringBuilder.ToString(); } public void Export(BinaryWriter bw) { bw.Write(id); Debug.LogFormat("id: {0}", id); bw.Write(triggerList.Count); Debug.LogFormat("triggerList.Count: {0}", triggerList.Count); foreach (var _trigger in triggerList) { _trigger.Export(bw); } bw.Write(eventList.Count); Debug.LogFormat("eventList.Count: {0}", eventList.Count); foreach (var _event in eventList) { _event.Export(bw); } bw.Write(transferList.Count); Debug.LogFormat("transferList.Count: {0}", transferList.Count); foreach (var _transfer in transferList) { _transfer.Export(bw); } } public void LoadJson(LitJson.JsonData json) { id = (int)json["id"]; var _triggerList = json["triggerList"]; GameObject _go = null; if (_triggerList.Count > 0) { for (int i = 0; i < _triggerList.Count; ++i) { _go = new GameObject(NodeName_Trigger + i); _go.transform.position = Vector3.zero; _go.transform.eulerAngles = Vector3.zero; _go.transform.localScale = Vector3.one; var _triggerBhv = _go.AddComponent(); _triggerBhv.boxCollider = _go.AddComponent(); _triggerBhv.transform.SetParent(transform.Find(NodeName_TriggerList)); triggerList.Add(_triggerBhv); _triggerBhv.LoadJson(_triggerList[i]); } } var _eventList = json["eventList"]; if (_eventList.Count > 0) { for (int i = 0; i < _eventList.Count; ++i) { _go = new GameObject(NodeName_Event + i); _go.transform.position = Vector3.zero; _go.transform.eulerAngles = Vector3.zero; _go.transform.localScale = Vector3.one; var _type = (Evt.E_EventType)(byte)_eventList[i]["type"];//br.ReadByte(); if (_type == Evt.E_EventType.Enemy) { var _eventBhv = _go.AddComponent(); _eventBhv.type = _type; _eventBhv.transform.SetParent(transform.Find(NodeName_EventList)); eventList.Add(_eventBhv); _eventBhv.LoadJson(_eventList[i]); } else if (_type == Evt.E_EventType.SceneObject) { var _eventBhv = _go.AddComponent(); _eventBhv.type = _type; _eventBhv.transform.SetParent(transform.Find(NodeName_EventList)); eventList.Add(_eventBhv); _eventBhv.LoadJson(_eventList[i]); } } } var _transferList = json["transferList"]; for (int i = 0; i < _transferList.Count; ++i) { _go = new GameObject(NodeName_Transfer + i); _go.transform.position = Vector3.zero; _go.transform.eulerAngles = Vector3.zero; _go.transform.localScale = Vector3.one; var _transferBhv = _go.AddComponent(); _transferBhv.transform.SetParent(transform.Find(NodeName_TransferList)); transferList.Add(_transferBhv); _transferBhv.LoadJson(_transferList[i]); } } public void Load(BinaryReader br) { id = br.ReadInt32(); int _count = br.ReadInt32(); GameObject _go = null; for (int i = 0; i < _count; ++i) { _go = new GameObject(NodeName_Trigger + i); _go.transform.position = Vector3.zero; _go.transform.eulerAngles = Vector3.zero; _go.transform.localScale = Vector3.one; var _triggerBhv = _go.AddComponent(); _triggerBhv.boxCollider = _go.AddComponent(); _triggerBhv.transform.SetParent(transform.Find(NodeName_TriggerList)); triggerList.Add(_triggerBhv); _triggerBhv.Load(br); } _count = br.ReadInt32(); for (int i = 0; i < _count; ++i) { _go = new GameObject(NodeName_Event + i); _go.transform.position = Vector3.zero; _go.transform.eulerAngles = Vector3.zero; _go.transform.localScale = Vector3.one; var _type = (Evt.E_EventType)br.ReadByte(); if (_type == Evt.E_EventType.Enemy) { var _eventBhv = _go.AddComponent(); _eventBhv.type = _type; _eventBhv.transform.SetParent(transform.Find(NodeName_EventList)); eventList.Add(_eventBhv); _eventBhv.Load(br); } else if (_type == Evt.E_EventType.SceneObject) { var _eventBhv = _go.AddComponent(); _eventBhv.type = _type; _eventBhv.transform.SetParent(transform.Find(NodeName_EventList)); eventList.Add(_eventBhv); _eventBhv.Load(br); } } _count = br.ReadInt32(); for (int i = 0; i < _count; ++i) { _go = new GameObject(NodeName_Transfer + i); _go.transform.position = Vector3.zero; _go.transform.eulerAngles = Vector3.zero; _go.transform.localScale = Vector3.one; var _transferBhv = _go.AddComponent(); _transferBhv.transform.SetParent(transform.Find(NodeName_TransferList)); transferList.Add(_transferBhv); _transferBhv.Load(br); } } public void Clear() { showTriggerList = false; showEventList = false; showTriggerList = false; triggerList.Clear(); eventList.Clear(); transferList.Clear(); for (int i = 0; i < transform.childCount; ++i) { for (int j = transform.GetChild(i).childCount - 1; j >= 0; --j) { DestroyImmediate(transform.GetChild(i).GetChild(j).gameObject); } } } public static float GetJsonFloat(LitJson.JsonData jsonData) { if (jsonData.IsDouble) { return (float)(double)jsonData; } else if (jsonData.IsInt) { return (float)(int)jsonData; } else { return 0f; } } #endif } }