-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnemyController.cs
149 lines (135 loc) · 4.79 KB
/
EnemyController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour
{
public Transform PlayerPos;
private bool viuPlayer = false;
public float speed;
private float auxSpeed;
private float waitTime;
public float startWaitTime;
private Vector2 direction;
public Transform[] PatrolPoints;
private int RandomSpot;
[SerializeField]
private Stat health;
[SerializeField]
private GameObject Base;
private float timing = 3f;
private bool canAttack = false;
public bool isAlien01 = false;
public bool isAlien02 = false;
public bool isAlien03 = false;
public bool isAlien04 = false;
private bool atacando = false;
/*Quando quiser dar dano na base use isso
Base.GetComponent<Base>().TakeDamage(insira o dano desejado inteiro ); */
private Animator animator;
public float vida;
public bool Vivo = true;
// Start is called before the first frame update
void Start()
{
waitTime = startWaitTime;
auxSpeed = speed;
RandomSpot = Random.Range(0, PatrolPoints.Length);
animator = GetComponent<Animator>();
health.Inicialize(vida,vida);
}
// Update is called once per frame
void Update()
{
print("COOLDOWN: " + timing);
animator.SetFloat("Horizontal",-direction.x);
animator.SetFloat("Vertical",-direction.y);
health.MyCurrentValue = vida;
Morreu();
if(Vivo){
if(viuPlayer){
ChasePlayer();
}else{
direction = (transform.position-PatrolPoints[RandomSpot].position);
transform.position = Vector2.MoveTowards(transform.position, PatrolPoints[RandomSpot].position, speed * Time.deltaTime);
if(Vector2.Distance(transform.position,PatrolPoints[RandomSpot].position) < 0.2f){
if(waitTime <= 0){
int AuxRandomSpot = RandomSpot;
RandomSpot = Random.Range(0, PatrolPoints.Length);
if(RandomSpot == AuxRandomSpot){
RandomSpot = Random.Range(0, PatrolPoints.Length);
}
waitTime = startWaitTime;
speed = auxSpeed;
}else{
waitTime -= Time.deltaTime;
speed = 0;
}
}
}
}
if(canAttack){
if(timing == 3f){
print("ATACANDO");
Base.GetComponent<Base>().TakeDamage(50);
atacando = true;
}
if(timing >0){
timing -= Time.deltaTime;
}
if(timing <= 0){
timing = 3;
}
}
}
void ChasePlayer(){
viuPlayer = true;
waitTime = 0;
if(Vector2.Distance(transform.position,PlayerPos.position) < 4){
direction = (transform.position-PlayerPos.position);
transform.position = Vector2.MoveTowards(transform.position, PlayerPos.position, speed * Time.deltaTime);
}else{
viuPlayer = false;
}
}
public void Morreu()
{
if(vida <=0)
{
Vivo =false;
}
if(!Vivo)
{
Destroy(gameObject);
}
}
public void OnCollisionStay2D(Collision2D collision)
{
// print("Tag do Colisor: " + collision.gameObject.tag);
if(collision.gameObject.tag == "Player"){
collision.gameObject.SendMessage("Die");
}else if(collision.gameObject.tag == "Interable"){
if(collision.gameObject.GetComponent<Interacao>().isBase){
if( atacando){
if(isAlien01){
collision.collider.SendMessage("TakeDamage",25);
}else if(isAlien02){
collision.collider.SendMessage("TakeDamage",50);
}else if(isAlien03){
collision.collider.SendMessage("TakeDamage",100);
}else if(isAlien04){
collision.collider.SendMessage("TakeDamage",200);
}
atacando = false;
}
canAttack = true;
}
}
}public void OnCollisionExit2D(Collision2D collision)
{
if(collision.gameObject.tag == "Interable"){
if(collision.gameObject.GetComponent<Interacao>().isBase){
canAttack = false;
}
}
}
}