Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- namespace ProcGen {
- public class BlockTemplate : ScriptableObject, IBlockConfig {
- [SerializeField] private LevelComponents _type = LevelComponents.Block;
- [SerializeField] private List<Entry> _entries = new List<Entry>();
- [SerializeField] private List<Entry> _entriesBackup = new List<Entry>();
- [SerializeField] private List<BlockCell> _cells = new List<BlockCell>();
- [SerializeField] private List<Door> _doors = new List<Door>();
- public List<Door> Doors {get { return _doors; } }
- public List<BlockCell> Cells {get { return _cells; } }
- public string Name { get { return name; } }
- public int EntryCount { get { return _entries.Count; } }
- public LevelComponents ComponentType { get { return _type; } set { _type = value; } }
- public void Save() {
- #if UNITY_EDITOR
- var parent = GetWorldTransform();
- if (parent == null) {
- return;
- }
- _entriesBackup.Clear();
- for (int i = 0; i < _entries.Count; i++) {
- _entriesBackup.Add(new Entry(_entries[i]));
- }
- _entries.Clear();
- foreach (Transform child in parent) {
- var prefab = UnityEditor.PrefabUtility.GetPrefabParent(child.gameObject) as GameObject;
- if (prefab == null) {
- Debug.LogError(string.Format("{0} is not a prefab", child.name));
- continue;
- }
- _entries.Add(new Entry(prefab, child));
- }
- UnityEditor.EditorUtility.SetDirty(this);
- #endif
- }
- public bool ContainsCell(Point3 pos) {
- for (int i = 0; i < _cells.Count; i++) {
- if (_cells[i].LocalPosition == pos) {
- return true;
- }
- }
- return false;
- }
- public BlockCell GetCell(Point3 pos) {
- for (int i = 0; i < _cells.Count; i++) {
- if (_cells[i].LocalPosition == pos) {
- return _cells[i];
- }
- }
- return null;
- }
- public void DeleteCell(Point3 pos) {
- for (int i = 0; i < _cells.Count; i++) {
- if (_cells[i].LocalPosition == pos) {
- _cells.RemoveAt(i);
- ClearDoors(pos);
- return;
- }
- }
- }
- public List<Door> GetDoors(Point3 pos) {
- List<Door> doors = new List<Door>();
- for (int i = 0; i < _doors.Count; i++) {
- if (_doors[i].CellPos == pos) {
- doors.Add(_doors[i]);
- }
- }
- return doors;
- }
- public void DeleteDoor(Door door) {
- for (int i = 0; i < _doors.Count; i++) {
- if (ReferenceEquals(_doors[i], door)) {
- var pos = door.CellPos;
- _doors.RemoveAt(i);
- CheckCellDoors(pos);
- return;
- }
- }
- }
- private void CheckCellDoors(Point3 pos) {
- var cell = GetCell(pos);
- if (cell == null) {
- return;
- }
- cell.HasDoor = false;
- for (int i = 0; i < _doors.Count; i++) {
- if (_doors[i].CellPos == pos) {
- cell.HasDoor = true;
- return;
- }
- }
- }
- private void ClearDoors(Point3 pos) {
- for (int i = _doors.Count - 1; i >= 0; i--) {
- if (_doors[i].CellPos == pos) {
- _doors.RemoveAt(i);
- }
- }
- }
- public void SetEntries(List<Entry> newEntries) {
- _entriesBackup.Clear();
- for (int i = 0; i < _entries.Count; i++) {
- _entriesBackup.Add(new Entry(_entries[i]));
- }
- _entries.Clear();
- for (int i = 0; i < newEntries.Count; i++) {
- _entries.Add(new Entry(newEntries[i]));
- }
- #if UNITY_EDITOR
- UnityEditor.EditorUtility.SetDirty(this);
- #endif
- }
- public void RestoreBackup() {
- if (_entriesBackup.Count == 0) {
- return;
- }
- var newList = new List<Entry>();
- newList.AddRange(_entriesBackup);
- SetEntries(newList);
- }
- public void Edit() {
- var tr = GetWorldTransform();
- if (tr == null) {
- return;
- }
- SpawnOnTr(tr, false);
- }
- public void SpawnOnTr(Transform tr, bool pool) {
- for (int i = 0; i < _entries.Count; i++) {
- _entries[i].Spawn(tr, pool);
- }
- }
- public LevelBlock Spawn() {
- var newBlock = ItemPool.Create<LevelBlock>(string.Format("LevelBlock{0}", name), true);
- for (int i = 0; i < _entries.Count; i++) {
- _entries[i].Spawn(newBlock.transform, true);
- }
- newBlock.Init(this);
- return newBlock;
- }
- public Transform GetWorldTransform() {
- var target = GameObject.FindObjectOfType<BlockGenerator>();
- return target != null ? target.transform : null;
- }
- [System.Serializable]
- public class Entry {
- public GameObject Prefab;
- public Vector3 Position;
- public Quaternion Rotation;
- public Vector3 Scale = Vector3.one;
- public Entry(Entry entry) {
- Prefab = entry.Prefab;
- Position = entry.Position;
- Rotation = entry.Rotation;
- Scale = entry.Scale;
- }
- public Entry(GameObject prefab, Transform tr) {
- Prefab = prefab;
- Position = tr.localPosition;
- Rotation = tr.localRotation;
- Scale = tr.localScale;
- }
- public void Spawn(Transform parent, bool pool) {
- GameObject child;
- if (pool) {
- child = Instantiate(Prefab);
- //child = ItemPool.SpawnPrefab(Prefab);
- }
- else {
- #if UNITY_EDITOR
- child = UnityEditor.PrefabUtility.InstantiatePrefab(Prefab) as GameObject;
- #else
- child = Instantiate(Prefab);
- #endif
- }
- if (child == null) {
- return;
- }
- child.transform.parent = parent;
- child.transform.localPosition = Position;
- child.transform.localRotation = Rotation;
- child.transform.localScale = Scale;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement