Removed dependencies we dont need

This commit is contained in:
Stedd 2022-09-17 16:06:52 +02:00
parent 9c14b0cb3f
commit 7f07511100
3 changed files with 24 additions and 158 deletions

View File

@ -4,11 +4,6 @@ using UnityEngine;
public class EnemyHandler : MonoBehaviour public class EnemyHandler : MonoBehaviour
{ {
[Header("Assigned on start")]
[SerializeField] PathFinder pathFinder;
[SerializeField] GridManager gridManager;
[Header("Parameters")] [Header("Parameters")]
[SerializeField] [Range(0.1f, 60f)] float spawnRate = 60f; [SerializeField] [Range(0.1f, 60f)] float spawnRate = 60f;
[SerializeField] int objectPoolSize = 15; [SerializeField] int objectPoolSize = 15;
@ -18,55 +13,20 @@ public class EnemyHandler : MonoBehaviour
[SerializeField] List<GameObject> enemyPrefabs = new List<GameObject>(); [SerializeField] List<GameObject> enemyPrefabs = new List<GameObject>();
[Header("Lists")] [Header("Lists")]
[SerializeField] List<Node> path = new List<Node>();
[SerializeField] List<GameObject> enemyPools = new List<GameObject>(); [SerializeField] List<GameObject> enemyPools = new List<GameObject>();
[SerializeField] List<GameObject> allEnemies = new List<GameObject>(); [SerializeField] List<GameObject> allEnemies = new List<GameObject>();
public List<Node> Path { get { return path; } }
private void Awake()
{
gridManager = FindObjectOfType<GridManager>();
}
private void Start() private void Start()
{ {
//gridManager.CalculateNewPath(); //gridManager.CalculateNewPath();
PopulateObjectPools(); PopulateObjectPools();
StartCoroutine(Spawner());
} }
private void Update() private void Update()
{ {
if (Input.GetKeyDown(KeyCode.N)) if (Input.GetKeyDown(KeyCode.N))
{ {
SpawnNewEnemy();
}
}
public void SetPath(List<Node> _nodes)
{
path.Clear();
foreach (Node _node in _nodes)
{
path.Add(_node);
}
UpdateEnemyPath();
}
void UpdateEnemyPath()
{
foreach (GameObject _pool in enemyPools)
{
foreach (EnemyMovement _enemy in _pool.GetComponentsInChildren<EnemyMovement>())
{
_enemy.SetPath(path);
}
} }
} }
@ -102,21 +62,6 @@ public class EnemyHandler : MonoBehaviour
return allEnemies; return allEnemies;
} }
void SpawnNewEnemy()
{
int spawnEnemyIndex = Mathf.RoundToInt(Random.Range(-0.49f, 2.49f));
enemyPools[spawnEnemyIndex].GetComponent<ObjectPool>().EnableFirstAvailableObject();
}
IEnumerator Spawner()
{
while (true)
{
SpawnNewEnemy();
yield return new WaitForSeconds(spawnRate);
}
}
public Vector2Int GetCoordinatesFromPosition(Vector3 position) public Vector2Int GetCoordinatesFromPosition(Vector3 position)
{ {
Vector2Int coordinates = new Vector2Int(); Vector2Int coordinates = new Vector2Int();

View File

@ -11,8 +11,7 @@ public class EnemyMovement : MonoBehaviour
[SerializeField] EnemyHandler enemyHandler; [SerializeField] EnemyHandler enemyHandler;
[SerializeField] ScoreHandler scoreHandler; [SerializeField] ScoreHandler scoreHandler;
[SerializeField] PathFinder pathFinder;
[SerializeField] List<Node> path;
Vector3 startPosition; Vector3 startPosition;
Vector3 endPosition; Vector3 endPosition;
@ -24,17 +23,12 @@ public class EnemyMovement : MonoBehaviour
{ {
enemyHandler = FindObjectOfType<EnemyHandler>(); enemyHandler = FindObjectOfType<EnemyHandler>();
scoreHandler = FindObjectOfType<ScoreHandler>(); scoreHandler = FindObjectOfType<ScoreHandler>();
pathFinder = FindObjectOfType<PathFinder>();
} }
void OnEnable() void OnEnable()
{ {
transform.localPosition = GetVector3(pathFinder.GetStartPosition());
SetPath(pathFinder.CalculateNewPath(enemyHandler.GetCoordinatesFromPosition(gameObject.transform.position)));
transform.LookAt(GetVector3(path[1].coordinates));
enemyHandler.AddEnemyToAllEnemies(gameObject); enemyHandler.AddEnemyToAllEnemies(gameObject);
CoroutineStarter();
} }
@ -46,53 +40,6 @@ public class EnemyMovement : MonoBehaviour
StopCoroutine(followPath); StopCoroutine(followPath);
} }
//Debug.Log($"{this.name} Recalculating path");
SetPath(pathFinder.CalculateNewPath(enemyHandler.GetCoordinatesFromPosition(gameObject.transform.position)));
CoroutineStarter();
}
private void CoroutineStarter()
{
followPath = FollowPath();
StartCoroutine(followPath);
}
public void SetPath(List<Node> _path)
{
path.Clear();
foreach (Node _node in _path)
{
path.Add(_node);
}
//regenerate start to finish path to not interfere with building
pathFinder.CalculateNewPath();
}
IEnumerator FollowPath()
{
for (int i = 1; i < path.Count; i++)
{
startPosition = transform.position;
endPosition = GetVector3(path[i].coordinates);
travelPercent = 0;
transform.LookAt(endPosition);
//float distance = Vector3.Distance(startPosition, endPosition);
//if (Vector3.Distance(startPosition, endPosition) < 10)
//{
// travelPercent = 1 - (distance / 10);
//}
// Debug.Log($"start: {startPosition}. end: {endPosition}");
while (travelPercent < 1f)
{
travelPercent += Time.deltaTime * speed;
transform.position = Vector3.Lerp(startPosition, endPosition, travelPercent);
yield return new WaitForEndOfFrame();
}
}
HandleReachedEndOfPath();
} }
void HandleReachedEndOfPath() void HandleReachedEndOfPath()

View File

@ -3,70 +3,44 @@ using UnityEngine;
public class BuildingHandler : MonoBehaviour public class BuildingHandler : MonoBehaviour
{ {
[Header("Assigned on start")] [Header("Assigned on start")] [SerializeField]
[SerializeField] ScoreHandler scoreHandler; ScoreHandler scoreHandler;
[SerializeField] GridManager gridManager;
[Header("Assigned on start")] [Header("Assigned on start")] [SerializeField]
[SerializeField] int buildingSelector = 0; int buildingSelector = 0;
[Header("Prefabs")] [SerializeField] List<Tower> buildings = new List<Tower>();
[Header("Prefabs")]
[SerializeField] List<Tower> buildings = new List<Tower>();
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
scoreHandler = FindObjectOfType<ScoreHandler>(); scoreHandler = FindObjectOfType<ScoreHandler>();
gridManager = FindObjectOfType<GridManager>();
} }
public void BuildTower(GameObject _tileGO) public void BuildTower(GameObject _tileGO)
{ {
//Tile _tile = _tileGO.GetComponentInChildren<Tile>(); //Tile _tile = _tileGO.GetComponentInChildren<Tile>();
Node _node = gridManager.GetNode(GetVector2(_tileGO));
//Debug.Log($"Placing tower on Tile: {_tileGO.transform.position}"); //Debug.Log($"Placing tower on Tile: {_tileGO.transform.position}");
//Debug.Log($"Placing tower on Node: {_node.coordinates}"); //Debug.Log($"Placing tower on Node: {_node.coordinates}");
if (_node.isBuildable) if (scoreHandler.CurrentBalance - buildings[buildingSelector].Cost < 0)
{ {
if (scoreHandler.CurrentBalance - buildings[buildingSelector].Cost < 0) print("Insufficient Funds!");
{ return;
print("Insufficient Funds!"); }
return; else
} {
else scoreHandler.ModifyWealth(-buildings[buildingSelector].Cost);
{ Instantiate(buildings[buildingSelector], _tileGO.transform.position, Quaternion.identity, transform);
if (_node.isPath)
{
_node.isBuildable = false;
_node.isWalkable = false;
gridManager.SetNode(_node);
//gridManager.CalculateNewPath();
if (!gridManager.CheckForValidPath())
{
_node.isBuildable = true;
_node.isWalkable = true;
gridManager.SetNode(_node);
print("Not allowed to block path!");
gridManager.CalculateNewPath();
return;
}
}
scoreHandler.ModifyWealth(-buildings[buildingSelector].Cost);
Instantiate(buildings[buildingSelector], _tileGO.transform.position, Quaternion.identity, transform);
_node.isWalkable = false;
_node.isBuildable = false;
gridManager.SetNode(_node);
}
} }
} }
public Vector2Int GetVector2(GameObject _o) public Vector2Int GetVector2(GameObject _o)
{ {
return new Vector2Int((Mathf.RoundToInt(_o.transform.position.x) / 10), (Mathf.RoundToInt(_o.transform.position.z / 10))); return new Vector2Int((Mathf.RoundToInt(_o.transform.position.x) / 10),
(Mathf.RoundToInt(_o.transform.position.z / 10)));
} }
} }