From ec550ad369531dd12ffb4c1c1d1f7a5b3236abee Mon Sep 17 00:00:00 2001 From: SEUNGHU LEE Date: Mon, 2 Dec 2019 15:54:34 +0900 Subject: [PATCH] [Update] Update Missile movement, Bind missile event --- .../Assets/_Scripts/BuildingManager.cs | 11 ++++- MissileCommander/Assets/_Scripts/Bullet.cs | 1 + .../Assets/_Scripts/GameManager.cs | 2 + MissileCommander/Assets/_Scripts/Missile.cs | 26 ++++++++++- .../Assets/_Scripts/MissileManager.cs | 43 ++++++++++++++++++- .../Assets/_Scripts/RecyclableObject.cs | 6 +-- 6 files changed, 82 insertions(+), 7 deletions(-) diff --git a/MissileCommander/Assets/_Scripts/BuildingManager.cs b/MissileCommander/Assets/_Scripts/BuildingManager.cs index 0b44fbe..970d39d 100644 --- a/MissileCommander/Assets/_Scripts/BuildingManager.cs +++ b/MissileCommander/Assets/_Scripts/BuildingManager.cs @@ -19,6 +19,11 @@ public BuildingManager(Building buildingPrefab, Transform[] buildingLocators) Debug.Assert(this._buildingLocators != null, "BuildingManager : Locators are null!"); } + public void OnGameStart() + { + SpawnBuildings(); + } + public void SpawnBuildings() { if (_buildings.Count > 0) @@ -35,9 +40,11 @@ public void SpawnBuildings() } } - public void OnGameStart() + public Vector3 GetRandomBuildingPosition() { - SpawnBuildings(); + Debug.Assert(_buildings.Count > 0, "BuildingManager : No Building left in the list!"); + Building building = _buildings[Random.Range(0, _buildings.Count)]; + return building.transform.position; } } } \ No newline at end of file diff --git a/MissileCommander/Assets/_Scripts/Bullet.cs b/MissileCommander/Assets/_Scripts/Bullet.cs index ff781c7..c0aa676 100644 --- a/MissileCommander/Assets/_Scripts/Bullet.cs +++ b/MissileCommander/Assets/_Scripts/Bullet.cs @@ -6,6 +6,7 @@ namespace MissileCommander public class Bullet : RecyclableObject { [SerializeField] private float moveSpeed = 5f; + // squApproximateOffset is squared value. [SerializeField] private float sqrApproximateOffset = 0.01f; private void Update() diff --git a/MissileCommander/Assets/_Scripts/GameManager.cs b/MissileCommander/Assets/_Scripts/GameManager.cs index 1c3322a..2ef12c3 100644 --- a/MissileCommander/Assets/_Scripts/GameManager.cs +++ b/MissileCommander/Assets/_Scripts/GameManager.cs @@ -52,6 +52,7 @@ private void BindEvents() _mouseGameController.OnFireButtonPressed += _launcher.OnFireButtonPressed; _timeMgr.onGameStart += _buildingMgr.OnGameStart; _timeMgr.onGameStart += _launcher.OnGameStart; + _timeMgr.onGameStart += _missileMgr.OnGameStart; } private void UnbindEvents() @@ -59,6 +60,7 @@ private void UnbindEvents() _mouseGameController.OnFireButtonPressed -= _launcher.OnFireButtonPressed; _timeMgr.onGameStart -= _buildingMgr.OnGameStart; _timeMgr.onGameStart -= _launcher.OnGameStart; + _timeMgr.onGameStart -= _missileMgr.OnGameStart; } } } \ No newline at end of file diff --git a/MissileCommander/Assets/_Scripts/Missile.cs b/MissileCommander/Assets/_Scripts/Missile.cs index 41a2569..fcb0d95 100644 --- a/MissileCommander/Assets/_Scripts/Missile.cs +++ b/MissileCommander/Assets/_Scripts/Missile.cs @@ -1,10 +1,13 @@ -using UnityEngine; +using System; +using UnityEngine; namespace MissileCommander { [RequireComponent(typeof(Rigidbody2D), typeof(BoxCollider2D))] public class Missile : RecyclableObject { + [SerializeField] private float moveSpeed = 3f; + private Rigidbody2D _rigidbody2D; private BoxCollider2D _boxCollider2D; @@ -16,5 +19,26 @@ private void Awake() _rigidbody2D.bodyType = RigidbodyType2D.Kinematic; _boxCollider2D.isTrigger = true; } + + private void Update() + { + if (!isActivated) { return; } + + CachedTransform.position += Time.deltaTime * moveSpeed * transform.up; + } + + private void OnTriggerEnter2D(Collider2D other) + { + if (other.TryGetComponent(out Building building)) + { + DestroySelf(); + } + } + + private void DestroySelf() + { + isActivated = false; + onDestroyed?.Invoke(this); + } } } \ No newline at end of file diff --git a/MissileCommander/Assets/_Scripts/MissileManager.cs b/MissileCommander/Assets/_Scripts/MissileManager.cs index ef33231..ea40edd 100644 --- a/MissileCommander/Assets/_Scripts/MissileManager.cs +++ b/MissileCommander/Assets/_Scripts/MissileManager.cs @@ -1,4 +1,6 @@ -using UnityEngine; +using System; +using UnityEngine; +using Random = UnityEngine.Random; namespace MissileCommander { @@ -9,6 +11,12 @@ public class MissileManager : MonoBehaviour private BuildingManager _buildingMgr; private bool _isInitialized = false; + private Camera _mainCamera; + + private void Awake() + { + _mainCamera = Camera.main != null ? Camera.main : FindObjectOfType(); + } public void Initialize(Factory missileFactory, BuildingManager buildingMgr) { @@ -22,5 +30,38 @@ public void Initialize(Factory missileFactory, BuildingManager buildingMgr) _isInitialized = true; } + + public void OnGameStart() + { + SpawnMissile(); + } + + private void SpawnMissile() + { + Debug.Assert(_missileFactory != null, "MissileManager : Missile Factory is null!"); + Debug.Assert(_buildingMgr != null, "MissileManager : BuildingManager is null!"); + + RecyclableObject missile = _missileFactory.Get(); + missile.Activate(GetMissileSpawnPosition(), _buildingMgr.GetRandomBuildingPosition()); + + missile.onDestroyed += this.OnMissileDestroyed; + } + + private Vector3 GetMissileSpawnPosition() + { + Vector3 spawnPosition = Vector3.zero; + spawnPosition.x = Random.Range(0f, 1f); + spawnPosition.y = 1f; + + spawnPosition = _mainCamera.ViewportToWorldPoint(spawnPosition); + spawnPosition.z = 0f; + return spawnPosition; + } + + private void OnMissileDestroyed(RecyclableObject missile) + { + missile.onDestroyed -= this.OnMissileDestroyed; + _missileFactory.ReturnToPool(missile); + } } } \ No newline at end of file diff --git a/MissileCommander/Assets/_Scripts/RecyclableObject.cs b/MissileCommander/Assets/_Scripts/RecyclableObject.cs index f588253..6083b72 100644 --- a/MissileCommander/Assets/_Scripts/RecyclableObject.cs +++ b/MissileCommander/Assets/_Scripts/RecyclableObject.cs @@ -12,7 +12,6 @@ public class RecyclableObject : MonoBehaviour private Transform _cachedTransform; public Transform CachedTransform => _cachedTransform ? _cachedTransform : (_cachedTransform = transform); - public virtual void Activate(Vector3 position) { isActivated = true; @@ -24,8 +23,9 @@ public virtual void Activate(Vector3 startPos, Vector3 targetPos) this.targetPosition = targetPos; Vector3 direction = (targetPos - startPos).normalized; - CachedTransform.SetPositionAndRotation(startPos, - Quaternion.LookRotation(CachedTransform.forward, direction)); + CachedTransform.SetPositionAndRotation + (startPos, + Quaternion.LookRotation(CachedTransform.forward, direction)); isActivated = true; } }