-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnemyWarning.cs
77 lines (65 loc) · 2.72 KB
/
EnemyWarning.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyWarning : MonoBehaviour
{
private SpriteRenderer myRenderer;
private SpriteRenderer parentRenderer;
private float topEdge;
private float rightEdge;
private Vector3 initialScale;
public float scalingFactor;
public float enlargeningStartDistance;
void Start()
{
myRenderer = gameObject.GetComponent<SpriteRenderer>();
parentRenderer = transform.parent.GetComponent<SpriteRenderer>();
myRenderer.enabled = !parentRenderer.isVisible;
topEdge = Camera.main.orthographicSize;
rightEdge = topEdge * Camera.main.aspect;
initialScale = transform.localScale;
}
// Update is called once per frame
void Update()
{
// If the parent is visible, we don't need to see this warning, so hide
// its renderer and do nothing else
if (parentRenderer.isVisible) {
myRenderer.enabled = false;
return;
// If the enemy ends up going back out of bounds, we need to reactivate the renderer
} else if (!myRenderer.enabled) {
myRenderer.enabled = true;
}
Vector3 parentPosition = transform.parent.position;
Vector2 mySize = myRenderer.bounds.size;
// On the right side
if (parentPosition.x >= rightEdge) {
transform.position = new Vector3(rightEdge - mySize.x / 2, parentPosition.y, parentPosition.z);
transform.rotation = Quaternion.Euler(0, 0, 270);
// On the left side
} else if (parentPosition.x <= -rightEdge) {
transform.position = new Vector3(-rightEdge + mySize.x / 2, parentPosition.y, parentPosition.z);
transform.rotation = Quaternion.Euler(0, 0, 90);
// On the top side
} else if (parentPosition.y >= topEdge) {
transform.position = new Vector3(parentPosition.x, topEdge - mySize.y / 2, parentPosition.z);
transform.rotation = Quaternion.Euler(0, 0, 0);
// On the bottom side
} else if (parentPosition.y <= -topEdge) {
transform.position = new Vector3(parentPosition.x, -topEdge + mySize.y / 2, parentPosition.z);
transform.rotation = Quaternion.Euler(0, 0, 180);
}
// Enlarge the warning as the enemy gets closer
Enlarge(Vector3.Distance(parentPosition, transform.position));
}
private void Enlarge(float dist)
{
if (dist >= enlargeningStartDistance) {
transform.localScale = initialScale;
return;
}
float factor = (enlargeningStartDistance - dist) * scalingFactor;
transform.localScale = initialScale + factor * new Vector3(1, 1, 1);
}
}