Last active
October 3, 2018 06:56
-
-
Save LeviVisser/6d285ea3f1179812036d283266bab961 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
/// <summary> | |
/// Zone data asset which can be created by the designer | |
/// </summary> | |
public class ZoneDataAsset | |
{ | |
[MenuItem("Assets/Create/Zone")] | |
public static void Create() | |
{ | |
Zone asset = ScriptableObject.CreateInstance<Zone>(); | |
AssetDatabase.CreateAsset(asset, "Assets/Resources/Settings/Zones/NewZone.asset"); | |
AssetDatabase.SaveAssets(); | |
EditorUtility.FocusProjectWindow(); | |
Selection.activeObject = asset; | |
} | |
} | |
using System; | |
using UnityEngine; | |
public class ZoneManager : MonoBehaviour { | |
public int CurrentZoneIdex; | |
private Zone[] _zones; | |
private bool _loopingZone; | |
public Zone CurrentZone { get; private set; } | |
// Use this for initialization | |
private void Start() { | |
_zones = Resources.LoadAll<Zone>("Settings/Zones"); | |
CurrentZone = _zones[0]; | |
ValidateSpawnPercentage(); | |
} | |
public Zone GetNextZone() { | |
if (_loopingZone) { | |
return _zones[0]; | |
} | |
if (CurrentZoneIdex < _zones.Length - 1) { | |
return _zones[GetCurrentZoneIndex() + 1]; | |
} | |
return _zones[GetCurrentZoneIndex()]; | |
} | |
public int GetCurrentZoneIndex() { | |
CurrentZoneIdex = Array.IndexOf(_zones, CurrentZone); | |
return CurrentZoneIdex; | |
} | |
public void SwitchCurrentZone() { | |
CurrentZone = GetNextZone(); | |
} | |
public Zone GetCurrentZone() { | |
return CurrentZone; | |
} | |
public void SetLoopZone(bool loop) { | |
_loopingZone = loop; | |
} | |
public void ValidateSpawnPercentage() { | |
// foreach(Zone zone in Zones) | |
// { | |
// int TotalSpawnPercentage = 0; | |
// foreach(BlockData data in zone.SpawnableBlocks) | |
// { | |
// TotalSpawnPercentage += data.SpawnPercentage; | |
// } | |
// if(TotalSpawnPercentage != 100) | |
// { | |
// Debug.LogError("Total spawning percentage does not == 100% in zone: " + zone.ZoneName); | |
// } | |
// } | |
} | |
} | |
/// | |
/// In the level generation manager | |
/// _zonemanager.SwitchCurrentZone() is called once all the rows have been spawned | |
/// | |
private void SpawnRow(int height) { | |
GameManager.Instance.BlockPool.LowestRow = height; | |
if (height >= _gridHeight) { | |
_zonemanager.SwitchCurrentZone(); | |
PrepareGrid(height); | |
} | |
for (int j = 0; j < _cellMap.GetLength(0); j++) { | |
Grid.Add(new TileSpawnPos(new Vector2(j * 0.5f + _startpos.x, _startpos.y - height), | |
Quaternion.Euler(0, 0, j % 2 == 0 ? 0 : 180), (EBlockTypes) _cellMap[j, _cellIndex])); | |
} | |
_cellIndex += 1; | |
if (_cellMap.GetLength(1) - 1 < _cellIndex) { | |
_cellMap = _levelGenerator.GenerateMap(); | |
_cellIndex = 0; | |
} | |
Grid.ForEach(b => GameManager.Instance.BlockPool.SpawnBlock(b.Position, b.Rotation, b.Type)); | |
Grid.Clear(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment