Skip to content

Commit

Permalink
[Update] Update missile spawning logic, Update collision between miss…
Browse files Browse the repository at this point in the history
…ile and explosion
  • Loading branch information
SeungHuLee committed Dec 4, 2019
1 parent 805b9c2 commit d822994
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 18 deletions.
13 changes: 0 additions & 13 deletions MissileCommander/Assets/Game/Core/Missile.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
2 changes: 1 addition & 1 deletion MissileCommander/Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions MissileCommander/Assets/_Scripts/BuildingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class BuildingManager

private List<Building> _buildings = new List<Building>();

public bool HasBuilding => _buildings.Count > 0;

public BuildingManager(Building buildingPrefab, Transform[] buildingLocators, Factory effectFactory)
{
this._buildingPrefab = buildingPrefab;
Expand Down
4 changes: 3 additions & 1 deletion MissileCommander/Assets/_Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,7 +42,7 @@ private void Start()
_buildingMgr = new BuildingManager(buildingPrefab, buildingLocators, new Factory(destroyEffect, 2));

_missileMgr = gameObject.AddComponent<MissileManager>();
_missileMgr.Initialize(new Factory(missilePrefab), _buildingMgr);
_missileMgr.Initialize(new Factory(missilePrefab), _buildingMgr, maxMissileCount, missileSpawnInterval);

_mouseGameController = gameObject.AddComponent<MouseGameController>();

Expand Down
8 changes: 8 additions & 0 deletions MissileCommander/Assets/_Scripts/Missile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
34 changes: 31 additions & 3 deletions MissileCommander/Assets/_Scripts/MissileManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System.Collections;
using UnityEngine;
using Random = UnityEngine.Random;

Expand All @@ -11,19 +11,27 @@ 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()
{
_mainCamera = Camera.main != null ? Camera.main : FindObjectOfType<Camera>();
}

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!");
Expand All @@ -33,7 +41,8 @@ public void Initialize(Factory missileFactory, BuildingManager buildingMgr)

public void OnGameStart()
{
SpawnMissile();
_currentMissileCount = 0;
_spawningMissile = StartCoroutine(AutoSpawnMissile());
}

private void SpawnMissile()
Expand All @@ -45,6 +54,8 @@ private void SpawnMissile()
missile.Activate(GetMissileSpawnPosition(), _buildingMgr.GetRandomBuildingPosition());

missile.onDestroyed += this.OnMissileDestroyed;

_currentMissileCount++;
}

private Vector3 GetMissileSpawnPosition()
Expand All @@ -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;
Expand Down

0 comments on commit d822994

Please sign in to comment.