Skip to content

Commit

Permalink
Merge pull request #57 from BerceniSoft/andreea/spawn-enemies-around-…
Browse files Browse the repository at this point in the history
…player

Andreea/spawn enemies around player
  • Loading branch information
brown121407 committed Dec 29, 2022
2 parents 1b293e3 + 6f1161b commit 3c28a86
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using UnityEngine;
using Random = UnityEngine.Random;
using UnityEngine.SceneManagement;
using UnityEngine.Serialization;

public class GameManager : MonoBehaviour
{
Expand All @@ -12,29 +13,34 @@ public class GameManager : MonoBehaviour

[Header("Enemy settings")]
[SerializeField] private GameObject enemyPrefab;
[SerializeField] private int maxWidth = 10;
[SerializeField] private int maxDistance = 5;
[SerializeField] private float maxWidth;
[SerializeField] private float secondsTillSpawn = 3f;
[SerializeField] private float percentToNextWave = .75f;

[Header("Level settings")]
[SerializeField] private int level = 1;

private int _numberOfWaves;
private int _constEnemies = 5;
private int _constEnemies = 1;
private bool _spawning = false;
private int _pause = 0;
private int _waveCount = 0;
private int _waveConst = 3;
private HudManager _ui;
private Player _player;
private Vector3 _playerPosition;
private Vector3 _floorSize;
private Camera _camera;

private void Awake()
{
_numberOfWaves = _waveConst * (SceneManager.GetActiveScene().buildIndex - 2);
_ui = GameObject.Find("HUD").GetComponent<HudManager>();
_player = GetComponentInChildren<Player>();
_player.UnlockBulletsForLevel(level);
_floorSize = GameObject.FindWithTag("Floor").GetComponent<MeshRenderer>().bounds.size;
maxWidth = _floorSize.x / 3;
_camera = Camera.main;
}

private void Start()
Expand All @@ -49,6 +55,7 @@ private void Start()

private void Update()
{
_playerPosition = _player.transform.position;
if (Input.GetKeyDown(KeyCode.Escape))
{
if (_pause == 0)
Expand Down Expand Up @@ -77,18 +84,31 @@ private void CheckNoEnemies()

private Vector3 GetRandomPosition()
{

while (true)
{
// get random point around the player
var randomPosition =
new Vector3(Random.Range(-maxWidth, maxWidth), 0.5f, Random.Range(-maxWidth, maxWidth));
var distance = Vector3.Distance(transform.position, randomPosition);
new Vector3(Random.Range(_playerPosition.x - maxWidth, _playerPosition.x + maxWidth), 0.5f,
Random.Range(_playerPosition.z - maxWidth, _playerPosition.z + maxWidth));

// check if the point is inside the map
if (randomPosition.x < -_floorSize.x / 2 || randomPosition.x > _floorSize.x / 2 ||
randomPosition.z < -_floorSize.z / 2 || randomPosition.z > _floorSize.z / 2)
{
continue;
}

// check if the point is outside camera view
if (randomPosition.x - _playerPosition.x < _camera.orthographicSize ||
randomPosition.z - _playerPosition.z < _camera.orthographicSize * _camera.aspect)
{
continue;
}

if (distance < maxDistance)
if (Physics.CheckSphere(randomPosition, 0.7f, 7))
{
if (Physics.CheckSphere(randomPosition, 0.7f, (int)Layers.Floor))
{
continue;
}
continue;
}

return randomPosition;
Expand Down

0 comments on commit 3c28a86

Please sign in to comment.