Skip to content

Commit

Permalink
Kinda working
Browse files Browse the repository at this point in the history
  • Loading branch information
Redtricity committed Apr 11, 2024
1 parent ea64f6e commit bca10f9
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 100 deletions.
42 changes: 10 additions & 32 deletions Assets/Code/Scripts/C#/Managers/EnemyShipManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.Events;
using Random = UnityEngine.Random;

public class EnemyShipManager : MonoBehaviour
{
[SerializeField] GameObject[] _enemyShipPrefabs;
[SerializeField] int _maxEnemies = 1;
[SerializeField] float _firstSpawnInterval = 10f, _spawnInterval = 30f;
[SerializeField] float _minSpawnRange = 500f, _maxSpawnRange = 1000f;
[SerializeField] float _minSpawnRange = 500f, _maxSpawnRange = 2000f;

List<EnemyShipController> _enemyShips;
List<GameObject> _enemyShips;
float _spawnDelay;
Transform _transform;
int MaxEnemies { get; set; }
float SpawnInterval { get; set; }
int ActiveEnemies => _enemyShips.Count(e => e.gameObject.activeSelf);
int ActiveEnemies => _enemyShips.Count(e => e.activeSelf);

bool CanSpawnEnemyShip
{
Expand All @@ -39,15 +40,10 @@ void Awake()

void OnEnable()
{
_enemyShips = new List<EnemyShipController>();
_enemyShips = new List<GameObject>();
_spawnDelay = _firstSpawnInterval;
}

private void Start()
{
GameManager.Instance.GameStateChanged += OnGameStateChanged;
}

void Update()
{
if (CanSpawnEnemyShip)
Expand All @@ -58,12 +54,10 @@ void Update()

void SpawnEnemyShip()
{
if (GameManager.Instance.GameState == GameState.GameOver) return;
var spawnPosition = Random.insideUnitSphere * Random.Range(_minSpawnRange, _maxSpawnRange);
var enemy = Instantiate(RandomPrefab, _transform);
EnemyShipController enemyShipController = enemy.GetComponent<EnemyShipController>();
enemyShipController.ShipDestroyed.AddListener(OnShipDestroyed);
_enemyShips.Add(enemyShipController);
enemy.GetComponent<EnemyShipController>().ShipDestroyed.AddListener(OnShipDestroyed);
_enemyShips.Add(enemy);
enemy.transform.position = spawnPosition;
_spawnDelay = SpawnInterval;
}
Expand All @@ -73,30 +67,14 @@ void OnShipDestroyed(int id)
for (var i = 0; i < _enemyShips.Count; ++i)
{
var ship = _enemyShips[i];
if (ship.gameObject.GetInstanceID() != id) continue;

if (ship.GetInstanceID() != id) continue;
_enemyShips.RemoveAt(i);
ship.GetComponent<EnemyShipController>().ShipDestroyed.RemoveListener(OnShipDestroyed);
Destroy(ship.gameObject);

if (_enemyShips.Count == 0) // Check if all enemy ships are destroyed
{
GameManager.Instance.PlayerWon();
}
return;
}
}

private void OnGameStateChanged(GameState gameState)
{
if (gameState != GameState.GameOver) return;
while (_enemyShips.Any())
{
var ship = _enemyShips[0];
ship.ShipDestroyed.RemoveListener(OnShipDestroyed);
ship.gameObject.SetActive(false);
_enemyShips.Remove(ship);
}

++MaxEnemies;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

public class AIShipWeaponControls : WeaponControlsBase
{

public override bool PrimaryFired => _firePrimary;
public override bool SecondaryFired => _fireSecondary;

Expand Down
26 changes: 4 additions & 22 deletions Assets/Code/Scripts/C#/ShipControls/EnemyShipController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ enum EnemyShipState
Transform _target;

public UnityEvent<int> ShipDestroyed = new UnityEvent<int>();

#region Public data for debugging

public string ShipState => _state.ToString();
Expand All @@ -43,13 +43,12 @@ public string DistanceToTarget
{
distance = $"{Vector3.Distance(_target.position, _transform.position):F2}";
}

return distance;
}
}

public string HealthLevel => $"{_damageHandler.Health}/{_damageHandler.MaxHealth}";

#endregion

bool InAttackRange => Vector3.Distance(PlayerShip.transform.position, _transform.position) <= _attackRange;
Expand Down Expand Up @@ -80,11 +79,6 @@ public override void Update()
EnemyShipState state = GetNextState();
SetState(state);
base.Update();

if (_state == EnemyShipState.Patrol || _state == EnemyShipState.Attack)
{
Debug.Log($"{_transform.name} is {_state} at distance {DistanceToTarget} to target {_target.name}");
}
}

EnemyShipState GetNextState()
Expand All @@ -95,23 +89,21 @@ EnemyShipState GetNextState()
EnemyShipState.Attack => Attack(),
EnemyShipState.Reposition => Reposition(),
EnemyShipState.Retreat => Retreat(),
_ => EnemyShipState.None
_ => EnemyShipState.None
};
return newState;
}

EnemyShipState Patrol()
{

if (ShouldRetreat) return EnemyShipState.Retreat;
if (InAttackRange) return EnemyShipState.Attack;
if (ReachedPatrolTarget)
{
_target.position = Random.insideUnitSphere * _patrolRange;
}

return EnemyShipState.Patrol;

}

EnemyShipState Attack()
Expand All @@ -136,9 +128,6 @@ EnemyShipState Retreat()
void SetState(EnemyShipState state)
{
if (_state == state) return;

Debug.Log($"State Change: {_transform.name} from {_state} to {state} at position {_transform.position}");

_state = state;
switch (state)
{
Expand All @@ -148,10 +137,7 @@ void SetState(EnemyShipState state)
_target = new GameObject("Patrol Target").transform;
_target.position = Random.insideUnitSphere * _patrolRange;
_aiShipMovementControls.SetTarget(_target);

Debug.Log("Patrol Target Set: " + _target.position);
}

break;
case EnemyShipState.Attack:
if (_target)
Expand All @@ -162,9 +148,6 @@ void SetState(EnemyShipState state)
_target = PlayerShip.transform;
_aiShipMovementControls.SetTarget(_target);
SetWeaponsTarget(_target, _attackRange, _targetMask);

Debug.Log($"{_transform.name} has the player in attack range at distance {Vector3.Distance(PlayerShip.transform.position, _transform.position)}");
Debug.Log("Attacking Player Ship: " + _target.name);
break;
case EnemyShipState.Reposition:
_aiShipMovementControls.SetTarget(_target = GetRepositionTarget());
Expand All @@ -173,7 +156,6 @@ void SetState(EnemyShipState state)
case EnemyShipState.Retreat:
_aiShipMovementControls.SetTarget(_target = GetRetreatTarget());
SetWeaponsTarget(null, 0, 0);
Debug.Log($"{_transform.name} is retreating due to low health at position {_transform.position}");
break;
}
}
Expand Down
Loading

0 comments on commit bca10f9

Please sign in to comment.