-
Notifications
You must be signed in to change notification settings - Fork 1
/
SafeZone.cs
114 lines (94 loc) · 2.25 KB
/
SafeZone.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
using UnityEngine;
using System.Collections;
/// <summary>
/// Encapsulates an area of space where ships can move around freely, controlled from top down view.
/// Used to implement colony territories
/// </summary>
public class SafeZone : MonoBehaviour {
float radius;
ILine ring;
SelectableBody source;
bool isEntered = false;
public static SafeZone Spawn(float zoneRadius, GameObject origin)
{
GameObject obj = new GameObject("SafeZone", typeof(SafeZone), typeof(LineDrawer), typeof(SphereCollider));
obj.transform.parent = origin.transform;
obj.transform.position = Vector3.zero;
obj.layer = LayerMask.NameToLayer("Ignore Raycast");
SafeZone safeZone = obj.GetComponent<SafeZone>();
SphereCollider collider = obj.GetComponent<SphereCollider>();
safeZone.radius = zoneRadius;
collider.radius = zoneRadius;
collider.isTrigger = true;
safeZone.source = origin.GetComponent<SelectableBody>();
return safeZone;
}
void OnTriggerExit(Collider other)
{
Ship ship = other.GetComponent<Ship>();
if(ship != null)
{
ship.EnterFTL();
OnExit();
}
else if(source is Planet)
OnExit();
}
void OnTriggerEnter(Collider other)
{
if(other.name == "CameraFocus" && source is Planet)
{
Game.gui.planetMenu.Open(source.gameObject);
}
else
{
Ship ship = other.GetComponent<Ship>();
if(ship != null)
{
if(ship.GetLaunchState() == "FTL")
{
ship.LeaveFTL();
source.SelfSelect(); //TODO: Maybe center on the planet instead or in addition?
}
OnEnter();
}
}
}
public void OnExit()
{
if(!isEntered) return;
isEntered = false;
if(source is Planet)
Game.gui.planetMenu.Close();
}
public void OnEnter()
{
if(isEntered) return;
isEntered = true;
Game.mainCamera.RestoreDefault();
if(source is Planet)
Game.gui.planetMenu.Open(source.gameObject);
}
public void SetRadius(float radius)
{
this.radius = radius;
}
public void ShowRing()
{
if(ring == null)
DrawRing();
ring.Show();
}
public bool Contains(Vector3 point)
{
return (transform.position - point).magnitude < radius;
}
public void HideRing()
{
ring.Hide();
}
void DrawRing()
{
ring = GetComponent<LineDrawer>().CreateCircle(gameObject, radius ,Color.green, 5, radius);
}
}