Add artist shooting, and refactor generation movment

This commit is contained in:
2023-02-16 23:44:32 -04:00
parent 2dad7631dd
commit 08ea2e0dcf
12 changed files with 273 additions and 24 deletions

View File

@ -1,5 +1,48 @@
using System;
using System.Collections.Generic;
using UnityEngine;
public class ArtistController : MonoBehaviour {
public int range = 50;
public int projectileSpeed = 5;
public GameObject projectile;
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);
}
}

View File

@ -4,37 +4,43 @@ using System.Collections.Generic;
using UnityEngine;
public class GenerationController : MonoBehaviour {
internal GameObject[] waypoints;
public float speed;
private GameObject[] waypoints;
private int waypointIndex = 1;
private Rigidbody2D rb;
// Start is called before the first frame update
private void Start() {
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update() { }
private void FixedUpdate() {
if (waypointIndex < waypoints.Length) {
var target = waypoints[waypointIndex].transform.position;
var direction = target - transform.position;
var f = transform.position + direction.normalized * 0.1f;
rb.MovePosition(f);
} else if (waypointIndex >= waypoints.Length) {
Destroy(gameObject);
}
moveToNextWaypoint();
}
private void OnTriggerEnter2D(Collider2D col) {
if (col.gameObject == waypoints[waypointIndex]) {
waypointIndex++;
moveToNextWaypoint();
}
}
public void setWaypoints(GameObject[] waypointsIn) {
waypoints = waypointsIn;
}
private void moveToNextWaypoint() {
if (waypointIndex < waypoints.Length) {
// Point towards the next waypoint
var heading = waypoints[waypointIndex].transform.position - transform.position;
var distance = heading.magnitude;
var direction = heading / distance;
var angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
var q = Quaternion.Euler(0,0,angle);
transform.rotation = q;
//Move towards the next waypoint, taking into account the speed variable
rb.velocity = direction * speed;
} else if (waypointIndex >= waypoints.Length) {
Destroy(gameObject);
}
}
}

View File

@ -18,7 +18,8 @@ public class LevelController : MonoBehaviour {
// Update is called once per frame
void Update() {
if (Input.GetMouseButtonDown(0)) {
Instantiate(artist, Camera.main.ScreenToWorldPoint(Input.mousePosition) + Vector3.forward, Quaternion.identity);
var a = Instantiate(artist, Camera.main.ScreenToWorldPoint(Input.mousePosition) + (Vector3.forward * 10), Quaternion.identity);
a.GetComponent<ArtistController>().setEnemyQueue(enemies);
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StandardProjectileController : MonoBehaviour {
private float speed;
private Rigidbody2D rb;
void Start() {
rb = GetComponent<Rigidbody2D>();
rb.velocity = transform.right * speed ;
}
void Update() {
}
public void setSpeed( float speedIn) {
speed = speedIn;
}
private void OnTriggerEnter2D(Collider2D col) {
Destroy(col.gameObject);
Destroy(gameObject);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7cdb7de3f3533494eae1b220ca0aebe1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: