Advertisement
Guest User

Untitled

a guest
Dec 1st, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.98 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace ProcGen {
  6.         public class BlockTemplate : ScriptableObject, IBlockConfig {
  7.  
  8.         [SerializeField] private LevelComponents _type = LevelComponents.Block;
  9.         [SerializeField] private List<Entry> _entries = new List<Entry>();
  10.         [SerializeField] private List<Entry> _entriesBackup = new List<Entry>();
  11.         [SerializeField] private List<BlockCell> _cells = new List<BlockCell>();
  12.         [SerializeField] private List<Door> _doors = new List<Door>();
  13.  
  14.         public List<Door> Doors {get { return _doors; } }
  15.         public List<BlockCell> Cells {get { return _cells; } }
  16.         public string Name { get { return name; } }
  17.         public int EntryCount { get { return _entries.Count; } }
  18.         public  LevelComponents ComponentType { get { return _type; } set { _type = value; } }
  19.  
  20.         public void Save() {
  21. #if UNITY_EDITOR
  22.             var parent = GetWorldTransform();
  23.             if (parent == null) {
  24.                 return;
  25.             }
  26.             _entriesBackup.Clear();
  27.             for (int i = 0; i < _entries.Count; i++) {
  28.                 _entriesBackup.Add(new Entry(_entries[i]));
  29.             }
  30.             _entries.Clear();
  31.             foreach (Transform child in parent) {
  32.                 var prefab = UnityEditor.PrefabUtility.GetPrefabParent(child.gameObject) as GameObject;
  33.                 if (prefab == null) {
  34.                     Debug.LogError(string.Format("{0} is not a prefab", child.name));
  35.                     continue;
  36.                 }
  37.                 _entries.Add(new Entry(prefab, child));
  38.             }
  39.             UnityEditor.EditorUtility.SetDirty(this);
  40. #endif
  41.         }
  42.  
  43.         public bool ContainsCell(Point3 pos) {
  44.             for (int i = 0; i < _cells.Count; i++) {
  45.                 if (_cells[i].LocalPosition == pos) {
  46.                     return true;
  47.                 }
  48.             }
  49.             return false;
  50.         }
  51.  
  52.         public BlockCell GetCell(Point3 pos) {
  53.             for (int i = 0; i < _cells.Count; i++) {
  54.                 if (_cells[i].LocalPosition == pos) {
  55.                     return _cells[i];
  56.                 }
  57.             }
  58.             return null;
  59.         }
  60.  
  61.         public void DeleteCell(Point3 pos) {
  62.             for (int i = 0; i < _cells.Count; i++) {
  63.                 if (_cells[i].LocalPosition == pos) {
  64.                     _cells.RemoveAt(i);
  65.                     ClearDoors(pos);
  66.                     return;
  67.                 }
  68.             }
  69.         }
  70.  
  71.         public List<Door> GetDoors(Point3 pos) {
  72.             List<Door> doors = new List<Door>();
  73.             for (int i = 0; i < _doors.Count; i++) {
  74.                 if (_doors[i].CellPos == pos) {
  75.                     doors.Add(_doors[i]);
  76.                 }
  77.             }
  78.             return doors;
  79.         }
  80.  
  81.         public void DeleteDoor(Door door) {
  82.             for (int i = 0; i < _doors.Count; i++) {
  83.                 if (ReferenceEquals(_doors[i], door)) {
  84.                     var pos = door.CellPos;
  85.                     _doors.RemoveAt(i);
  86.                     CheckCellDoors(pos);
  87.                     return;
  88.                 }
  89.             }
  90.         }
  91.  
  92.         private void CheckCellDoors(Point3 pos) {
  93.             var cell = GetCell(pos);
  94.             if (cell == null) {
  95.                 return;
  96.             }
  97.             cell.HasDoor = false;
  98.             for (int i = 0; i < _doors.Count; i++) {
  99.                 if (_doors[i].CellPos == pos) {
  100.                     cell.HasDoor = true;
  101.                     return;
  102.                 }
  103.             }
  104.         }
  105.  
  106.         private void ClearDoors(Point3 pos) {
  107.             for (int i = _doors.Count - 1; i >= 0; i--) {
  108.                 if (_doors[i].CellPos == pos) {
  109.                     _doors.RemoveAt(i);
  110.                 }
  111.             }
  112.         }
  113.  
  114.         public void SetEntries(List<Entry> newEntries) {
  115.             _entriesBackup.Clear();
  116.             for (int i = 0; i < _entries.Count; i++) {
  117.                 _entriesBackup.Add(new Entry(_entries[i]));
  118.             }
  119.             _entries.Clear();
  120.             for (int i = 0; i < newEntries.Count; i++) {
  121.                 _entries.Add(new Entry(newEntries[i]));
  122.             }
  123.            
  124. #if UNITY_EDITOR
  125.             UnityEditor.EditorUtility.SetDirty(this);
  126. #endif
  127.         }
  128.  
  129.         public void RestoreBackup() {
  130.             if (_entriesBackup.Count == 0) {
  131.                 return;
  132.             }
  133.             var newList = new List<Entry>();
  134.             newList.AddRange(_entriesBackup);
  135.             SetEntries(newList);
  136.         }
  137.  
  138.         public void Edit() {
  139.             var tr = GetWorldTransform();
  140.             if (tr == null) {
  141.                 return;
  142.             }
  143.             SpawnOnTr(tr, false);
  144.         }
  145.  
  146.         public void SpawnOnTr(Transform tr, bool pool) {
  147.             for (int i = 0; i < _entries.Count; i++) {
  148.                 _entries[i].Spawn(tr, pool);
  149.             }
  150.         }
  151.  
  152.         public LevelBlock Spawn() {
  153.             var newBlock = ItemPool.Create<LevelBlock>(string.Format("LevelBlock{0}", name), true);
  154.             for (int i = 0; i < _entries.Count; i++) {
  155.                 _entries[i].Spawn(newBlock.transform, true);
  156.             }
  157.             newBlock.Init(this);
  158.             return newBlock;
  159.         }
  160.  
  161.         public Transform GetWorldTransform() {
  162.             var target = GameObject.FindObjectOfType<BlockGenerator>();
  163.             return target != null ? target.transform : null;
  164.         }
  165.  
  166.         [System.Serializable]
  167.         public class Entry {
  168.             public GameObject Prefab;
  169.             public Vector3 Position;
  170.             public Quaternion Rotation;
  171.             public Vector3 Scale = Vector3.one;
  172.  
  173.             public Entry(Entry entry) {
  174.                 Prefab = entry.Prefab;
  175.                 Position = entry.Position;
  176.                 Rotation = entry.Rotation;
  177.                 Scale = entry.Scale;
  178.             }
  179.  
  180.             public Entry(GameObject prefab, Transform tr) {
  181.                 Prefab = prefab;
  182.                 Position = tr.localPosition;
  183.                 Rotation = tr.localRotation;
  184.                 Scale = tr.localScale;
  185.             }
  186.  
  187.             public void Spawn(Transform parent, bool pool) {
  188.                 GameObject child;
  189.                 if (pool) {
  190.                     child = Instantiate(Prefab);
  191.                     //child = ItemPool.SpawnPrefab(Prefab);
  192.                 }
  193.                 else {
  194. #if UNITY_EDITOR
  195.                     child = UnityEditor.PrefabUtility.InstantiatePrefab(Prefab) as GameObject;
  196. #else
  197.                     child = Instantiate(Prefab);
  198. #endif
  199.                 }
  200.                 if (child == null) {
  201.                     return;
  202.                 }
  203.                 child.transform.parent = parent;
  204.                 child.transform.localPosition = Position;
  205.                 child.transform.localRotation = Rotation;
  206.                 child.transform.localScale = Scale;
  207.             }
  208.         }
  209.     }
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement