using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System; | |
public class SensePowerGlove : WeaponClass | |
{ | |
// This is a special power weapon that instansiates a prefab in form of an sphere that moves from target to target and knocks them out. | |
//This is made by using a overlapshere to check the closest enemy and moving towards it. | |
// it does this every time it hits a enemy until it has reached a certain amount of hits. | |
public float checkRadius; | |
public LayerMask checkLayers; | |
Collider closestTar, currentTar; | |
public GameObject flowerSpore; | |
private bool hasSomethingTohit = false; | |
private GameObject test; | |
private int hitCap = 0; | |
[SerializeField] int AmountOfBounces; | |
[SerializeField] float lerpSpeed; | |
private Player player; | |
private void Start() | |
{ | |
player = GetComponentInParent<Player>(); | |
} | |
void NextTarget (GameObject MoveAgain) // This is to find out the next target to move to. | |
{ | |
Collider[] colliders = Physics.OverlapSphere(MoveAgain.transform.position, checkRadius, checkLayers, QueryTriggerInteraction.Ignore); // Checks colliders in the overlapSphere,but only if they are colliders and not triggers and are enemys. | |
Array.Sort(colliders, new DistancePowerGlove(transform)); //Sorts the colliders transforms in distance order from closest to furthest. | |
for (int i = 0; i < colliders.Length; i++) | |
{ | |
if (colliders[i] != null && !colliders[i].GetComponent<DamageableObjects>().IsKnockedDown && hitCap <= AmountOfBounces) // check that the array is not empty and the enemy is alive and not knocked out, and the maximum amount of hits it not reached | |
{ | |
closestTar = colliders[i]; // Sets the closest target. | |
hasSomethingTohit = true; | |
hitCap++; | |
i = colliders.Length; | |
Array.Clear(colliders, 0, colliders.Length); | |
StartCoroutine(MoveObject(MoveAgain, MoveAgain.transform.position, closestTar.transform, 1)); // Calls Einumerator "MoveAgain" and sends the closest target transform. | |
} | |
else | |
{ | |
hasSomethingTohit = false; | |
} | |
} | |
if (hasSomethingTohit == false) | |
{ | |
Destroy(MoveAgain); // destroy the instansiated "weapon sphere" | |
} | |
#region | |
//for (int i = 0; i < colliders.Length; i++) | |
//{ | |
// if (colliders[i] != null || colliders[i].GetComponent<DamageableObjects>().IsKnockedDown) | |
// { | |
// closestTar = colliders[i]; | |
// hasSomethingTohit = true; | |
// StartCoroutine(MoveObject(MoveAgain, MoveAgain.transform.position, closestTar.transform.position, 1)); | |
// } | |
// else | |
// { | |
// hasSomethingTohit = false; | |
// } | |
// Array.Clear(colliders, 0, colliders.Length); | |
//} | |
#endregion | |
} | |
public override void Attack() | |
{ | |
Collider[] colliders = Physics.OverlapSphere(transform.position, checkRadius, checkLayers, QueryTriggerInteraction.Ignore); | |
Array.Sort(colliders, new DistancePowerGlove(transform)); | |
hitCap = 1; | |
player.UseAmmo(0, 0, 1); | |
for (int i = 0; i < colliders.Length; i++) | |
{ | |
if (colliders[i] != null && !colliders[i].GetComponent<DamageableObjects>().IsKnockedDown) | |
{ | |
closestTar = colliders[i]; | |
hitCap++; | |
hasSomethingTohit = true; | |
Array.Clear(colliders, 0, colliders.Length); | |
GameObject Spores = Instantiate(flowerSpore, gameObject.transform.position, gameObject.transform.rotation) as GameObject; // Instansiate the prefab | |
StartCoroutine(MoveObject(Spores, Spores.transform.position, closestTar.transform, lerpSpeed)); // Calls move object. | |
} | |
else | |
{ | |
hasSomethingTohit = false; | |
} | |
} | |
if (colliders.Length <= 0) | |
{ | |
// This instantiates a prefab that will rotate around the player, this is to show that there are no enemys to move to. | |
GameObject SporesRot = Instantiate(flowerSpore, transform.localPosition/* new Vector3(transform.position.x , transform.position.y, transform.position.z)*/, transform.rotation) as GameObject; | |
Debug.Log(gameObject.transform.position); | |
StartCoroutine(MoveAround(SporesRot)); // calls movearound. | |
} | |
#region | |
//for (int i = 0; i < colliders.Length; i++) | |
//{ | |
// if (colliders[i] != null || colliders[i].GetComponent<DamageableObjects>().IsKnockedDown ) { | |
// closestTar = colliders[0]; | |
// hasSomethingTohit = true; | |
// GameObject Spores = Instantiate(flowerSpore, gameObject.transform.position, gameObject.transform.rotation) as GameObject; | |
// StartCoroutine(MoveObject(Spores, Spores.transform.position, closestTar.transform.position, 1)); | |
// } | |
// else { hasSomethingTohit = false; } | |
// Array.Clear(colliders, 0 , colliders.Length); | |
//} | |
#endregion | |
} | |
IEnumerator MoveAround(GameObject sporeRotMove)// rotates the spore prefab around the player | |
{ | |
float duration = 10f; | |
float elapsed = 0f; | |
float spinSpeed = 40.0f; | |
while (elapsed < duration && sporeRotMove != null) | |
{ | |
elapsed += Time.deltaTime; | |
var v = Quaternion.AngleAxis(Time.time * spinSpeed * -10, Vector3.up) * new Vector3(1, 0, 0); | |
sporeRotMove.transform.position = player.transform.position + v; | |
Destroy(sporeRotMove, 5); | |
yield return new WaitForEndOfFrame(); | |
} | |
yield return null; | |
} | |
IEnumerator MoveObject(GameObject spore, Vector3 source, Transform target, float overTime) // moves the spore prefab from source position to a target position | |
{ | |
float startTime = Time.time; | |
while (Time.time < startTime + overTime) | |
{ | |
spore.transform.position = Vector3.Lerp(source, new Vector3(target.position.x, target.position.y + 1.0f, target.position.z), (Time.time - startTime) / overTime); | |
yield return null; | |
} | |
spore.transform.position = new Vector3(target.position.x,target.position.y + 1.0f, target.position.z); | |
if ( spore.transform.position.x == target.position.x && spore.transform.position.z == target.position.z) // if target is reached, move to next target | |
{ | |
target.GetComponent<DamageableObjects>().TakeDamage(1000); // give damage to the target that is reached | |
NextTarget(spore); | |
} | |
} | |
private void OnDrawGizmos() // developer tools to show the sphere and the line from enemys | |
{ | |
Gizmos.DrawWireSphere(transform.position, checkRadius); | |
if (hasSomethingTohit == true) | |
{ | |
Gizmos.DrawLine(this.transform.position, closestTar.transform.position ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment