104 lines
3.4 KiB
C#
104 lines
3.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class EnemyController : MonoBehaviour, IPooledObject{
|
|
|
|
public float speed;
|
|
public GameObject enemy;
|
|
public GameObject spawnParticles;
|
|
public Color level1Color;
|
|
public Color level2Color;
|
|
public Color level3Color;
|
|
|
|
GameController gameController;
|
|
Animator anim;
|
|
bool ready;
|
|
bool spawned;
|
|
BoxCollider2D boxCol;
|
|
Rigidbody2D rb2d;
|
|
Transform target;
|
|
Vector2 targetPos;
|
|
|
|
public void OnObjectSpawn(){
|
|
gameController = FindObjectOfType<GameController>();
|
|
if(gameController.level == 1 || gameController.level == 0){
|
|
GetComponentInChildren<SpriteRenderer>().color = level1Color;
|
|
ParticleSystem ps = spawnParticles.GetComponent<ParticleSystem>();
|
|
var main = ps.main;
|
|
main.startColor = level1Color;
|
|
}
|
|
if (gameController.level == 2){
|
|
GetComponentInChildren<SpriteRenderer>().color = level2Color;
|
|
ParticleSystem ps = spawnParticles.GetComponent<ParticleSystem>();
|
|
var main = ps.main;
|
|
main.startColor = level2Color;
|
|
}
|
|
if (gameController.level == 3){
|
|
GetComponentInChildren<SpriteRenderer>().color = level3Color;
|
|
ParticleSystem ps = spawnParticles.GetComponent<ParticleSystem>();
|
|
var main = ps.main;
|
|
main.startColor = level3Color;
|
|
}
|
|
anim = GetComponent<Animator>();
|
|
anim.SetBool("Spawned", false);
|
|
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
|
|
rb2d = GetComponent<Rigidbody2D>();
|
|
boxCol = GetComponentInChildren<BoxCollider2D>();
|
|
boxCol.enabled = false;
|
|
spawnParticles.SetActive(true);
|
|
spawnParticles.GetComponent<ParticleSystem>().Play();
|
|
ready = false;
|
|
spawned = false;
|
|
Invoke(nameof(StopAnimation), 1f);
|
|
Invoke(nameof(Ready), 2f);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update(){
|
|
if (ready){
|
|
targetPos = target.position - transform.position;
|
|
float rotZ = Mathf.Atan2(targetPos.y, targetPos.x) * Mathf.Rad2Deg;
|
|
enemy.transform.rotation = Quaternion.Euler(0f, 0f, rotZ);
|
|
}else if (spawned){
|
|
Vector2 targetPos = target.transform.position - transform.position;
|
|
float rotZ = Mathf.Atan2(targetPos.y, targetPos.x) * Mathf.Rad2Deg;
|
|
enemy.transform.rotation = Quaternion.Euler(0f, 0f, rotZ);
|
|
}
|
|
}
|
|
|
|
void FixedUpdate(){
|
|
if (ready){
|
|
rb2d.MovePosition(rb2d.position + (targetPos.normalized * speed * Time.fixedDeltaTime));
|
|
}
|
|
}
|
|
|
|
void OnTriggerEnter2D(Collider2D col){
|
|
if (col.CompareTag("Bullet") || col.CompareTag("BigBullet")){
|
|
ScreenShake.Shake(1f, .15f);
|
|
AudioManager.instance.Play("EnemyDeath");
|
|
ObjectPooler.Instance.SpawnFromPool("EnemyDeath", col.transform.position, Quaternion.identity);
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
#region OnSpawn
|
|
void StopAnimation(){
|
|
anim.SetBool("Spawned", true);
|
|
}
|
|
|
|
void Ready(){
|
|
spawned = true;
|
|
Invoke(nameof(StartRunning), 0.5f);
|
|
Invoke(nameof(EnableCollider), 0.25f);
|
|
}
|
|
|
|
void EnableCollider(){
|
|
boxCol.enabled = true;
|
|
}
|
|
|
|
void StartRunning(){
|
|
ready = true;
|
|
}
|
|
#endregion
|
|
}
|