diff --git a/MissileCommander/Assets/Game/Core/Missile.prefab b/MissileCommander/Assets/Game/Core/Missile.prefab index d7c6f93..d0bc9cd 100644 --- a/MissileCommander/Assets/Game/Core/Missile.prefab +++ b/MissileCommander/Assets/Game/Core/Missile.prefab @@ -104,7 +104,6 @@ GameObject: m_Component: - component: {fileID: 5711166925691360735} - component: {fileID: 5711166925691360734} - - component: {fileID: 1619056898089133577} m_Layer: 0 m_Name: Missile_0 m_TagString: Untagged @@ -176,15 +175,3 @@ SpriteRenderer: m_WasSpriteAssigned: 1 m_MaskInteraction: 0 m_SpriteSortPoint: 0 ---- !u!114 &1619056898089133577 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 5711166925691360728} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 42124bdd7fda48d3becb8731b89690f6, type: 3} - m_Name: - m_EditorClassIdentifier: diff --git a/MissileCommander/Assets/Scenes/SampleScene.unity b/MissileCommander/Assets/Scenes/SampleScene.unity index 07b332a..40c338a 100644 --- a/MissileCommander/Assets/Scenes/SampleScene.unity +++ b/MissileCommander/Assets/Scenes/SampleScene.unity @@ -364,7 +364,7 @@ MonoBehaviour: - {fileID: 598110664} missilePrefab: {fileID: 5711166924668602170, guid: 6e3ed30d88f130b488476553a9ef130c, type: 3} - destroyEffect: {fileID: 8157270428527755281, guid: 460346ccc206af549931e9ccb77609ba, + destroyEffect: {fileID: 6117580419327657503, guid: 460346ccc206af549931e9ccb77609ba, type: 3} --- !u!4 &1776773259 Transform: diff --git a/MissileCommander/Assets/_Scripts/BuildingManager.cs b/MissileCommander/Assets/_Scripts/BuildingManager.cs index 33c4500..228018b 100644 --- a/MissileCommander/Assets/_Scripts/BuildingManager.cs +++ b/MissileCommander/Assets/_Scripts/BuildingManager.cs @@ -12,6 +12,8 @@ public class BuildingManager private List _buildings = new List(); + public bool HasBuilding => _buildings.Count > 0; + public BuildingManager(Building buildingPrefab, Transform[] buildingLocators, Factory effectFactory) { this._buildingPrefab = buildingPrefab; diff --git a/MissileCommander/Assets/_Scripts/GameManager.cs b/MissileCommander/Assets/_Scripts/GameManager.cs index 9997e41..a6fbef2 100644 --- a/MissileCommander/Assets/_Scripts/GameManager.cs +++ b/MissileCommander/Assets/_Scripts/GameManager.cs @@ -23,6 +23,8 @@ public class GameManager : MonoBehaviour [Header("Missile & Effect")] [SerializeField] private Missile missilePrefab; [SerializeField] private DestroyEffect destroyEffect; + [SerializeField] private float missileSpawnInterval = 0.5f; + [SerializeField] private int maxMissileCount = 20; private MouseGameController _mouseGameController; private BulletLauncher _launcher; @@ -40,7 +42,7 @@ private void Start() _buildingMgr = new BuildingManager(buildingPrefab, buildingLocators, new Factory(destroyEffect, 2)); _missileMgr = gameObject.AddComponent(); - _missileMgr.Initialize(new Factory(missilePrefab), _buildingMgr); + _missileMgr.Initialize(new Factory(missilePrefab), _buildingMgr, maxMissileCount, missileSpawnInterval); _mouseGameController = gameObject.AddComponent(); diff --git a/MissileCommander/Assets/_Scripts/Missile.cs b/MissileCommander/Assets/_Scripts/Missile.cs index fcb0d95..984a9b7 100644 --- a/MissileCommander/Assets/_Scripts/Missile.cs +++ b/MissileCommander/Assets/_Scripts/Missile.cs @@ -32,6 +32,14 @@ private void OnTriggerEnter2D(Collider2D other) if (other.TryGetComponent(out Building building)) { DestroySelf(); + return; + } + + if (other.TryGetComponent(out Explosion explosion)) + { + Debug.Log("Intercepted by explosion!"); + DestroySelf(); + return; } } diff --git a/MissileCommander/Assets/_Scripts/MissileManager.cs b/MissileCommander/Assets/_Scripts/MissileManager.cs index ea40edd..d7bf557 100644 --- a/MissileCommander/Assets/_Scripts/MissileManager.cs +++ b/MissileCommander/Assets/_Scripts/MissileManager.cs @@ -1,4 +1,4 @@ -using System; +using System.Collections; using UnityEngine; using Random = UnityEngine.Random; @@ -11,6 +11,12 @@ public class MissileManager : MonoBehaviour private BuildingManager _buildingMgr; private bool _isInitialized = false; + private int _maxMissileCount = 20; + private float _missileSpawnInterval = 0.5f; + private int _currentMissileCount; + + private Coroutine _spawningMissile; + private Camera _mainCamera; private void Awake() @@ -18,12 +24,14 @@ private void Awake() _mainCamera = Camera.main != null ? Camera.main : FindObjectOfType(); } - public void Initialize(Factory missileFactory, BuildingManager buildingMgr) + public void Initialize(Factory missileFactory, BuildingManager buildingMgr, int maxMissileCount, float missileSpawnInterval) { if (_isInitialized) { return; } this._missileFactory = missileFactory; this._buildingMgr = buildingMgr; + this._maxMissileCount = maxMissileCount; + this._missileSpawnInterval = missileSpawnInterval; Debug.Assert(_missileFactory != null, "MissileManager : Missile Factory is null!"); Debug.Assert(_buildingMgr != null, "MissileManager : BuildingManager is null!"); @@ -33,7 +41,8 @@ public void Initialize(Factory missileFactory, BuildingManager buildingMgr) public void OnGameStart() { - SpawnMissile(); + _currentMissileCount = 0; + _spawningMissile = StartCoroutine(AutoSpawnMissile()); } private void SpawnMissile() @@ -45,6 +54,8 @@ private void SpawnMissile() missile.Activate(GetMissileSpawnPosition(), _buildingMgr.GetRandomBuildingPosition()); missile.onDestroyed += this.OnMissileDestroyed; + + _currentMissileCount++; } private Vector3 GetMissileSpawnPosition() @@ -58,6 +69,23 @@ private Vector3 GetMissileSpawnPosition() return spawnPosition; } + private IEnumerator AutoSpawnMissile() + { + WaitForSeconds spawnInterval = new WaitForSeconds(_missileSpawnInterval); + while (_currentMissileCount < _maxMissileCount) + { + yield return spawnInterval; + + if (!_buildingMgr.HasBuilding) + { + Debug.LogWarning("There is no building left to target!"); + yield break; + } + + SpawnMissile(); + } + } + private void OnMissileDestroyed(RecyclableObject missile) { missile.onDestroyed -= this.OnMissileDestroyed;