-
Notifications
You must be signed in to change notification settings - Fork 1
/
Looter.cs
146 lines (119 loc) · 3.25 KB
/
Looter.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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
public class LootReport
{
Dictionary<Loot.DropType, float> quantityPerType;
StringBuilder stringBuilder;
public LootReport()
{
quantityPerType = new Dictionary<Loot.DropType, float>();
stringBuilder = new StringBuilder();
}
public void Add(Loot.DropType type, float quantity)
{
if(quantityPerType.ContainsKey(type))
quantityPerType[type] += quantity;
else quantityPerType.Add(type, quantity);
}
public void Add(Loot loot)
{
Add(loot.dropType, loot.value);
}
public float GetQuantity(Loot.DropType type)
{
if( quantityPerType.ContainsKey(type) )
return quantityPerType[type];
else return 0;
}
public bool HasType(Loot.DropType type)
{
return quantityPerType.ContainsKey(type);
}
public override string ToString ()
{
stringBuilder.Length = 0;
foreach(Loot.DropType type in quantityPerType.Keys)
{
if(stringBuilder.Length > 0)
stringBuilder.Append("\n");
stringBuilder.Append( string.Format ("{0}: {1}\n", type, quantityPerType[type]) );
}
return stringBuilder.ToString();
}
}
public class Looter : MonoBehaviour {
public float lootDistance = 500;
public float collectionSpeed = 1;
LootReport currentBatch;
bool waitingToReport = false;
DynamicLabel label;
float runningTotal;
float resetRunningTotalSeconds = 0;
void Start()
{
currentBatch = new LootReport();
}
void Update()
{
Collider[] nearbyObjects = Physics.OverlapSphere(transform.position, lootDistance);
foreach(Collider thing in nearbyObjects)
{
Loot loot = thing.GetComponent<Loot>();
if(loot != null)
{
Vector3 lootMoveDirection = transform.position - loot.transform.position;
loot.transform.position += lootMoveDirection.normalized * Mathf.Min ( collectionSpeed * Time.deltaTime, lootMoveDirection.magnitude );
if(thing.bounds.Contains(transform.position))
{
Collect (loot);
}
}
}
if(runningTotal > 0)
{
if(label == null)
label = GetLabel();
string labelText = currentBatch.ToString();
label.SetLabelText(labelText);
}
else
{
if(label != null)
{
label.Remove();
label = null;
}
}
if(resetRunningTotalSeconds <= 0)
runningTotal = 0;
resetRunningTotalSeconds -= Time.deltaTime;
}
void Collect(Loot loot)
{
currentBatch.Add(loot);
runningTotal += loot.value;
resetRunningTotalSeconds = 1;
if(!waitingToReport)
StartCoroutine(ReportLoot(currentBatch));
Destroy(loot.gameObject);
}
IEnumerator ReportLoot(LootReport report)
{
waitingToReport = true;
yield return new WaitForSeconds(0.5f); //collect for a bit first, to minimize amount of ineffiecient message passing
BroadcastMessage("OnCollectLoot", report ,SendMessageOptions.DontRequireReceiver);
currentBatch = new LootReport();
waitingToReport = false;
}
private DynamicLabel GetLabel()
{
//Special case for ships with renderers on
if(transform.GetComponent<Engine>() != null)
label = Game.gui.labelPool.Label(transform.Find("ShipMesh").gameObject, Vector3.up * collider.bounds.extents.magnitude * 5);
else //general case
label = Game.gui.labelPool.Label(gameObject, Vector3.up * 10); //place label a little above object
return label;
}
}