Skip to content

Commit

Permalink
[Update] Update Missile movement, Bind missile event
Browse files Browse the repository at this point in the history
  • Loading branch information
SeungHuLee committed Dec 2, 2019
1 parent 7d30a45 commit ec550ad
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 7 deletions.
11 changes: 9 additions & 2 deletions MissileCommander/Assets/_Scripts/BuildingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
}
}
1 change: 1 addition & 0 deletions MissileCommander/Assets/_Scripts/Bullet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions MissileCommander/Assets/_Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ private void BindEvents()
_mouseGameController.OnFireButtonPressed += _launcher.OnFireButtonPressed;
_timeMgr.onGameStart += _buildingMgr.OnGameStart;
_timeMgr.onGameStart += _launcher.OnGameStart;
_timeMgr.onGameStart += _missileMgr.OnGameStart;
}

private void UnbindEvents()
{
_mouseGameController.OnFireButtonPressed -= _launcher.OnFireButtonPressed;
_timeMgr.onGameStart -= _buildingMgr.OnGameStart;
_timeMgr.onGameStart -= _launcher.OnGameStart;
_timeMgr.onGameStart -= _missileMgr.OnGameStart;
}
}
}
26 changes: 25 additions & 1 deletion MissileCommander/Assets/_Scripts/Missile.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
}
}
43 changes: 42 additions & 1 deletion MissileCommander/Assets/_Scripts/MissileManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using UnityEngine;
using System;
using UnityEngine;
using Random = UnityEngine.Random;

namespace MissileCommander
{
Expand All @@ -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<Camera>();
}

public void Initialize(Factory missileFactory, BuildingManager buildingMgr)
{
Expand All @@ -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);
}
}
}
6 changes: 3 additions & 3 deletions MissileCommander/Assets/_Scripts/RecyclableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand Down

0 comments on commit ec550ad

Please sign in to comment.