fgm24/Assets/Scripts/Enemy/InstantiateOverTime.cs

114 lines
3.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
[System.Serializable]
public class BulletPattern
{
[SerializeField]
public float Arc = 15;
[SerializeField]
public int Amount = 3;
[SerializeField]
public float Speed = 800;
[SerializeField]
public bool Burst = true;
[SerializeField]
public float Cooldown = 1f;
}
[System.Serializable]
public struct BulletPatternSequence
{
[SerializeField]
public List<BulletPattern> sequence;
[SerializeField]
public int currentPattern;
}
public class InstantiateOverTime : MonoBehaviour
{
public int rng = 1000;
private float CanShootTime;
public GameObject projectile;
public List<BulletPatternSequence> Patterns;
BulletPatternSequence _CurrentSequence;
private void Start()
{
_CurrentSequence = getRandomPattern();
}
private void Update()
{
// i kknow very ismart!:)
int radm = Random.Range(0, rng);
if (radm != 0) return;
if (CanShootTime < Time.time) // We are allowed to shoot
{
BulletPattern currentPattern = _CurrentSequence.sequence[_CurrentSequence.currentPattern];
// Instantiate boolets for patters
Vector2 arcStart = -transform.up;
for (int i = 0; i < currentPattern.Amount; i++)
{
float stepSize = currentPattern.Arc / ((float)currentPattern.Amount);
Vector2 cArc = rotate(arcStart, stepSize * i + stepSize / 2f - currentPattern.Arc / 2f);
GameObject p = Instantiate(projectile, transform.position, Quaternion.identity);
p.transform.LookAt((Vector2)p.transform.position + cArc, new Vector3(0, 0, 1));
Vector3 r = p.transform.rotation.eulerAngles;
p.transform.rotation = Quaternion.Euler(0, 0, r.z);
p.GetComponent<Rigidbody2D>().velocity = (GetShortestPlayer() - transform.position).normalized * currentPattern.Speed;
p.AddComponent<DestroyAfter>().Init(20f);
}
CanShootTime = Time.time + currentPattern.Cooldown;
_CurrentSequence.currentPattern++;
if (_CurrentSequence.currentPattern >= _CurrentSequence.sequence.Count)
{ // Done shooting this sequence
_CurrentSequence = getRandomPattern();
_CurrentSequence.currentPattern = 0;
}
}
}
Vector3 GetShortestPlayer()
{
GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
float dist = Mathf.Infinity;
GameObject closestPlayer = players[0];
for (int i = 0; i < players.Length; i++)
{
float distance = Vector2.Distance(transform.position, players[i].transform.position);
if (distance < dist)
{
dist = distance;
closestPlayer = players[i];
}
}
return closestPlayer.transform.position;
}
BulletPatternSequence getRandomPattern()
{
return Patterns[0];
}
public static Vector2 rotate(Vector2 v, float deg)
{
float delta = deg * (float)Mathf.Deg2Rad;
return new Vector2(
v.x * Mathf.Cos(delta) - v.y * Mathf.Sin(delta),
v.x * Mathf.Sin(delta) + v.y * Mathf.Cos(delta)
);
}
}