MAAC3601-TD-Game/Assets/Scripts/ArtistController.cs

48 lines
1.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2023-02-13 17:50:51 -04:00
using UnityEngine;
public class ArtistController : MonoBehaviour {
public int range = 50;
public int projectileSpeed = 5;
public GameObject projectile;
2023-02-16 19:07:21 -04:00
private Queue<GameObject> enemyQueue;
void Start() {
InvokeRepeating(nameof(fireAttack), 0, 1);
}
public void setEnemyQueue(Queue<GameObject> enemyQueueIn) {
enemyQueue = enemyQueueIn;
}
// Look through all of the queue, and find the distance between each enemy and the artist.
// Keep track of the enemy with the lowest distance, and after you have looped through the entire queue,
// Initiate a new instance of the projectile game object, from the artists position, in the direction of the enemy.
private void fireAttack() {
if (enemyQueue.Count == 0) return; // If there are no enemies in the queue, return.
if (enemyQueue.Peek() == null) return; // If the first enemy in the queue is null, return.
float lowestDistance = range;
GameObject enemyTarget = null;
foreach (var enemy in enemyQueue) {
var distance = Vector3.Distance(enemy.transform.position, transform.position);
if (distance < lowestDistance) {
lowestDistance = distance;
enemyTarget = enemy;
}
}
if (enemyTarget == null) return; // If there are no enemies in range, return.
//Find the vector that points from the artist to the enemy.
var heading = enemyTarget.transform.position - transform.position;
//Find the angle between the artist and the enemy.
var angle = Mathf.Atan2(heading.y, heading.x) * Mathf.Rad2Deg;
//Create a quaternion (rotation) based on the angle.
var q = Quaternion.Euler(0,0,angle);
var projectileInstance = Instantiate(projectile, transform.position, q);
projectileInstance.GetComponent<StandardProjectileController>().setSpeed(projectileSpeed);
}
2023-02-16 19:07:21 -04:00
}