From f75d7e8fee1f44719c038400a8703e85153bd42c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nu=C8=9Biu?= Date: Tue, 4 Jun 2019 20:20:53 +0300 Subject: [PATCH] Implement Destructible Wall & Fix deathzone bug --- Assets/DevMocks/bman_destructible_wall.prefab | 13 +++++ Assets/Scripts/src/Helpers/ListExtensions.cs | 11 +++- Assets/Scripts/src/Managers/LevelManager.cs | 57 ++++++++++++++----- Assets/Scripts/src/Wall.meta | 3 + Assets/Scripts/src/Wall/DestructibleWall.cs | 32 +++++++++++ .../Scripts/src/Wall/DestructibleWall.cs.meta | 3 + 6 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 Assets/Scripts/src/Wall.meta create mode 100644 Assets/Scripts/src/Wall/DestructibleWall.cs create mode 100644 Assets/Scripts/src/Wall/DestructibleWall.cs.meta diff --git a/Assets/DevMocks/bman_destructible_wall.prefab b/Assets/DevMocks/bman_destructible_wall.prefab index 74ddcc2..8f3bab4 100644 --- a/Assets/DevMocks/bman_destructible_wall.prefab +++ b/Assets/DevMocks/bman_destructible_wall.prefab @@ -12,6 +12,7 @@ GameObject: - component: {fileID: 4177940729222266820} - component: {fileID: 4177940729222266823} - component: {fileID: 4177940729222266822} + - component: {fileID: 2823577682722293689} m_Layer: 0 m_Name: bman_destructible_wall m_TagString: Untagged @@ -128,3 +129,15 @@ BoxCollider2D: serializedVersion: 2 m_Size: {x: 1, y: 1} m_EdgeRadius: 0 +--- !u!114 &2823577682722293689 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4177940729222266681} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b30e7124fcdf4e1f92fc7dd42d8c7298, type: 3} + m_Name: + m_EditorClassIdentifier: diff --git a/Assets/Scripts/src/Helpers/ListExtensions.cs b/Assets/Scripts/src/Helpers/ListExtensions.cs index a8eaa33..16cbb50 100644 --- a/Assets/Scripts/src/Helpers/ListExtensions.cs +++ b/Assets/Scripts/src/Helpers/ListExtensions.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.Collections.Generic; using UnityEngine; namespace src.Helpers @@ -12,12 +13,20 @@ namespace src.Helpers for (var i = min; i < max; i++) { var randomPos = Mathf.FloorToInt(Random.Range(min, max)); - + /* Swap elements in list */ var aux = list[randomPos]; list[randomPos] = list[i]; list[i] = aux; } } + + public static T PopRandom(this IList list) + { + var randomIndex = Mathf.FloorToInt(Random.Range(0, list.Count - 1)); + var elem = list[randomIndex]; + list.RemoveAt(randomIndex); + return elem; + } } } \ No newline at end of file diff --git a/Assets/Scripts/src/Managers/LevelManager.cs b/Assets/Scripts/src/Managers/LevelManager.cs index e0943e0..8180ab4 100644 --- a/Assets/Scripts/src/Managers/LevelManager.cs +++ b/Assets/Scripts/src/Managers/LevelManager.cs @@ -1,7 +1,7 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using src.Base; using src.Helpers; +using src.Wall; using UnityEngine; using Random = UnityEngine.Random; @@ -19,6 +19,11 @@ namespace src.Managers Min = min; Max = max; } + + public int RandomIntRange() + { + return Mathf.FloorToInt(Random.Range(Min, Max)); + } } public int columns = 30; @@ -26,6 +31,7 @@ namespace src.Managers /* Specifies how many objects we want per level. */ public Count destructibleWallCount = new Count(150, 350); + public Count upgradesCount = new Count(0, 5); public Count enemyCount = new Count(20, 50); /* Holds the starting position of the player */ @@ -37,18 +43,39 @@ namespace src.Managers public Transform boardHolder; /* Holds the available positions */ - private readonly List _gridPositions = new List(); + private readonly List _freeGridPositions = new List(); + private readonly List _destructibleWalls = new List(); /* Test only */ public void Awake() { InitBoard(); SetupLevel(); + SetupExit(); + SetupUpgrades(); + } + + private void SetupUpgrades() + { + var count = upgradesCount.RandomIntRange(); + for (var i = 0; i < count; i++) + { + /* Get the destructible wall script and make it to spawn the upgrade */ + var wall = _destructibleWalls.PopRandom().GetComponent(); + wall.SpawnsUpgrade(); + } + } + + private void SetupExit() + { + /* Get the destructible wall script and make it to spawn the exit */ + var wall = _destructibleWalls.PopRandom().GetComponent(); + wall.SpawnsExit(); } public void InitBoard() { - _gridPositions.Clear(); + _freeGridPositions.Clear(); /* We want to iterate over the X axis taking into consideration the startPosition's offset */ for (var x = startPosition.position.x; x < columns; x++) { @@ -56,19 +83,19 @@ namespace src.Managers { /* We want the following positions to be a safe zone. */ /* Don't place anything on starting position */ - if (Mathf.FloorToInt(x) == 0 && Mathf.FloorToInt(y) == 0) + if (Mathf.RoundToInt(x) == 0 && Mathf.RoundToInt(y) == 0) { continue; } /* Don't place anything on X=1 and Y=0 */ - if (Mathf.FloorToInt(x) == 1 && Mathf.FloorToInt(y) == 0) + if (Mathf.RoundToInt(x) == 1 && Mathf.RoundToInt(y) == 0) { continue; } /* Don't place anything on X=0 and Y=1 */ - if (Mathf.FloorToInt(x) == 0 && Mathf.FloorToInt(y) == 1) + if (Mathf.RoundToInt(x) == 0 && Mathf.RoundToInt(y) == -1) { continue; } @@ -80,7 +107,7 @@ namespace src.Managers } /* Add position to _gridPositions */ - _gridPositions.Add(new Vector3(x, y, 0f)); + _freeGridPositions.Add(new Vector3(x, y, 0f)); } } } @@ -88,16 +115,16 @@ namespace src.Managers public void SetupLevel() { var random = new Random(); - var numberOfDestructilbeWallsToPlace = - Mathf.FloorToInt(Random.Range(destructibleWallCount.Min, destructibleWallCount.Max)); + var numberOfDestructilbeWallsToPlace = destructibleWallCount.RandomIntRange(); - _gridPositions.ShuffleList(); - foreach (var nextPosition in _gridPositions) + _freeGridPositions.ShuffleList(); + foreach (var nextPosition in _freeGridPositions) { if (numberOfDestructilbeWallsToPlace == 0) { break; } + PlaceDestructibleTile(nextPosition); numberOfDestructilbeWallsToPlace -= 1; } @@ -106,13 +133,15 @@ namespace src.Managers private void PlaceDestructibleTile(Vector3 position) { var instance = Instantiate(destructibleWallPrefab, position, Quaternion.identity); + _destructibleWalls.Add(instance); instance.transform.SetParent(boardHolder); } private bool PlaceIndestructibleTile(float x, float y) { - var absX = Mathf.FloorToInt(x); - var absY = Mathf.FloorToInt(y); + Debug.Log($"PlaceIndestructibleTile: x:{x} y:{y}"); + var absX = Mathf.RoundToInt(x); + var absY = Mathf.RoundToInt(y); if (absX % 2 == 0 || absY % 2 == 0) return false; diff --git a/Assets/Scripts/src/Wall.meta b/Assets/Scripts/src/Wall.meta new file mode 100644 index 0000000..f77ba1f --- /dev/null +++ b/Assets/Scripts/src/Wall.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5a3a413dae6e40f2967fc143c723dce6 +timeCreated: 1559667182 \ No newline at end of file diff --git a/Assets/Scripts/src/Wall/DestructibleWall.cs b/Assets/Scripts/src/Wall/DestructibleWall.cs new file mode 100644 index 0000000..33bc1c4 --- /dev/null +++ b/Assets/Scripts/src/Wall/DestructibleWall.cs @@ -0,0 +1,32 @@ +using src.Base; + +namespace src.Wall +{ + public class DestructibleWall : GameplayComponent + { + private bool _spawnExit; + private bool _spawnUpgrade; + + public void SpawnsExit() + { + _spawnExit = true; + } + + public void SpawnsUpgrade() + { + _spawnUpgrade = true; + } + + public void OnDestroy() + { + if (_spawnExit) + { + // TODO Spawn an exit + } else if (_spawnUpgrade) + { + // TODO Spawn an upgrade, use composition to UpgradeManager + // to get random / desired upgrade + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/src/Wall/DestructibleWall.cs.meta b/Assets/Scripts/src/Wall/DestructibleWall.cs.meta new file mode 100644 index 0000000..dedf3c9 --- /dev/null +++ b/Assets/Scripts/src/Wall/DestructibleWall.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b30e7124fcdf4e1f92fc7dd42d8c7298 +timeCreated: 1559667193 \ No newline at end of file