-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlyingEye.cs
144 lines (120 loc) · 3.4 KB
/
FlyingEye.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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using Unity.VisualScripting;
public class FlyingEye : MonoBehaviour
{
Animator animator;
Rigidbody2D rb;
public Collider2D deathCollider;
public DetectionZone biteDetectionZone;
public float flightSpeed = 5f;
private float waypointReachedDistance = 0.1f;
public List<Transform> waypoints = new List<Transform>();
public bool _hasTarget = false;
Damageable damageable;
Transform nextWaypoint;
int waypointNum = 0;
public bool HasTarget
{
get { return _hasTarget; }
private set
{
_hasTarget = value;
animator.SetBool(AnimationStrings.hasTarget, value);
}
}
public bool CanMove
{
get
{
return animator.GetBool(AnimationStrings.canMove);
}
}
private void Awake()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
damageable = GetComponent<Damageable>();
}
private void Start()
{
nextWaypoint = waypoints[waypointNum];
}
private void OnEnable()
{
damageable.damageableDeath.AddListener(OnDeath);
}
// Update is called once per frame
void Update()
{
HasTarget = biteDetectionZone.detectedColliders.Count > 0;
}
private void FixedUpdate()
{
if (damageable.IsAlive)
{
if (CanMove)
{
Flight();
}
else
{
rb.linearVelocity = Vector2.zero;
}
}
}
private void Flight()
{
// Fly to the Waypoint
Vector2 directionToWaypoint = (nextWaypoint.position - transform.position).normalized;
// Check if we have reached the waypoint already
float distance = Vector2.Distance(nextWaypoint.position, transform.position);
rb.linearVelocity = directionToWaypoint * flightSpeed;
UpdateDirection();
// See if we need to switch waypoints
if (distance <= waypointReachedDistance)
{
// swtich to the next waypoint
waypointNum++;
if (waypointNum >= waypoints.Count)
{
// Loop back to the original waypoint
waypointNum = 0;
}
nextWaypoint = waypoints[waypointNum];
}
}
private void UpdateDirection()
{
Vector3 locScale = transform.localScale;
if (transform.localScale.x > 0)
{
//Facing the Right
if (rb.linearVelocity.x < 0)
{
//Flip
transform.localScale = new Vector3(-1 * locScale.x, locScale.y, locScale.z);
}
}
else
{
//Facing the Left
if (rb.linearVelocity.x > 0)
{
//Flip
transform.localScale = new Vector3(-1 * locScale.x, locScale.y, locScale.z);
}
}
}
public void OnDeath()
{
{
//Dead Flyer falls to ground
rb.gravityScale = 1.5f;
rb.linearVelocity = new Vector2(0, rb.linearVelocity.y);
deathCollider.enabled = true;
}
}
}