Skip to content

Commit cd2f16c

Browse files
committed
Adds TrailingMonsterManager to update/remove all trails/monsters for each player
1 parent 83f6a30 commit cd2f16c

File tree

6 files changed

+248
-15
lines changed

6 files changed

+248
-15
lines changed

Assets/_Project/Prefabs/UI/ControlsCanvas.prefab

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/_Project/Scripts/Data/DataManager.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ public string GetLocalFileString ()
142142
string unitSampleStr = unitSampleArr [0] + "-" + unitSampleArr [1];
143143
// loop through array (drag all files into inspector)
144144
for (int i = 0; i < localDataFiles.Length; i++) {
145-
// Debug.Log (localDataFiles [i].name);
145+
//Debug.Log (localDataFiles [i].name);
146+
146147
// if name is contained then return
147148
if (localDataFiles [i].name.Contains (unitSampleStr)) {
148149
return localDataFiles [i].text;
@@ -272,7 +273,7 @@ void PopulateDropdown (TMP_Dropdown dropdown, Enum e)
272273
*/
273274
public void OnChangeModeDropdown (int _status)
274275
{
275-
Debug.Log ("OnChangeModeDropdown() _status = " + _status);
276+
//Debug.Log ("OnChangeModeDropdown() _status = " + _status);
276277
// cast as Enum
277278
selectedMode = (ModeType)_status;
278279
}
@@ -302,7 +303,7 @@ public void EnableEndpointDropdown (bool _status)
302303

303304
public void GetNewData ()
304305
{
305-
Debug.Log ("DataManager.GetNewData() [1]");
306+
//Debug.Log ("DataManager.GetNewData()");
306307

307308
// disable dropdown temporarily
308309
EnableEndpointDropdown (false);
@@ -317,9 +318,6 @@ public void GetNewData ()
317318
// trigger data updated event
318319
EventManager.TriggerEvent ("DataManagerUpdated");
319320

320-
321-
Debug.Log ("DataManager.GetNewData() [2]");
322-
323321
// if using local archive file
324322
if (selectedMode == ModeType.localArchive) {
325323

@@ -379,7 +377,7 @@ public IEnumerator GetRequest (string uri)
379377
/// <param name="text">JSON data as string</param>
380378
public void HandleJsonResponse (string text)
381379
{
382-
Debug.Log ("HandleJsonResponse() text.Length = " + text.Length);
380+
//Debug.Log ("HandleJsonResponse() text.Length = " + text.Length);
383381

384382
// set status
385383
dataRequestStatus = DataRequestStatus.receiving;

Assets/_Project/Scripts/Data/Timeline.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public void SetTimelineStatus (TimelineStatus _status, bool fromUI = false)
249249
*/
250250
public void StartBufferLoop ()
251251
{
252-
Debug.Log ("Timeline.StartBufferLoop()");
252+
//Debug.Log ("Timeline.StartBufferLoop()");
253253

254254
// if coroutine running
255255
if (bufferCoroutine != null) StopCoroutine (bufferCoroutine);
@@ -261,7 +261,7 @@ public void StartBufferLoop ()
261261
*/
262262
public void StopBufferLoop ()
263263
{
264-
Debug.Log ("Timeline.StopBufferLoop()");
264+
//Debug.Log ("Timeline.StopBufferLoop()");
265265

266266
// if coroutine running
267267
if (bufferCoroutine != null) StopCoroutine (bufferCoroutine);
@@ -276,7 +276,7 @@ public void StopBufferLoop ()
276276
*/
277277
public void StartHistoryLoop ()
278278
{
279-
Debug.Log ("Timeline.StartHistoryLoop()");
279+
//Debug.Log ("Timeline.StartHistoryLoop()");
280280

281281
// if coroutine running
282282
if (historyCoroutine != null) StopCoroutine (historyCoroutine);
@@ -288,7 +288,7 @@ public void StartHistoryLoop ()
288288
*/
289289
public void StopHistoryLoop ()
290290
{
291-
Debug.Log ("Timeline.StopHistoryLoop()");
291+
//Debug.Log ("Timeline.StopHistoryLoop()");
292292

293293
// if coroutine running
294294
if (historyCoroutine != null) StopCoroutine (historyCoroutine);

Assets/_Project/Scripts/Players/MonsterIndex.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ protected MonsterIndex () { }
2222
[Tooltip ("Array of sprites with the duplicate names removed")]
2323
public Sprite [] monstersFromSheetsDistinct;
2424

25+
// adding new monsters:
26+
// 1. this array is exported from tally-utilities/images-composite
27+
// 2. drag sprites into array
2528
[Tooltip ("Array of mids used in the game - index * 3 = sprite position")]
2629
public int [] gameMids = {
27-
6,15,48,63,82,86,87,89,91,92,96,100,102,110,111,118,121,122,132,135,137,151,154,155,158,161,165,169,170,171,172,174,176,177,178,179,181,184,202,204,205,207,209,210,211,212,215,216,217,218,220,221,222,224,229,231,241,242,243,244,269,271,272,274,277,278,279,281,282,283,300,301,310,315,316,324,325,326,327,329,330,331,332,333,334,335,336,343,346,347,348,350,357,359,360,362,363,370,371,376,381,383,384,385,386,410,418,420,425,443,450,456,466,469,470,472,477,482,492,499,521,545,563,582,584,589,594,596,599,600,601,614,618,620,625,628,630,632,633,635,637,638,640,653,655,673,677,681,683,684
30+
6,15,48,63,82,86,87,89,91,92,96,100,102,110,111,118,121,122,132,135,137,151,154,155,158,161,165,169,170,171,172,174,176,177,178,179,181,184,202,204,205,207,209,210,211,212,215,216,217,218,220,221,222,224,229,231,241,242,243,244,269,271,272,274,277,278,279,281,282,283,300,301,310,315,316,324,325,326,327,329,330,331,332,333,334,335,336,343,346,347,348,350,357,359,360,362,363,370,371,376,381,383,384,385,386,410,418,420,425,443,450,456,466,469,470,472,477,482,492,499,521,545,563,582,584,589,594,596,599,600,601,607,614,618,620,625,627,628,630,632,633,635,637,638,640,653,655,673,677,681,683,684
2831
};
2932

3033
[Tooltip ("Total sprites")]
@@ -35,7 +38,7 @@ private void Awake ()
3538
{
3639
// remove duplicates from the spritesheet array
3740
monstersFromSheetsDistinct = monstersFromSheets.Distinct ().ToArray ();
38-
// set the lenth
41+
// set the length
3942
monstersFromSheetsDistinctLength = monstersFromSheetsDistinct.Length;
4043
}
4144

@@ -52,6 +55,14 @@ public int GetGameMidIndex (int _mid)
5255
}
5356

5457

58+
public int GetRandomMid (int min = 0, int max = 200)
59+
{
60+
// keep max in range
61+
max = Math.Min (max, gameMids.Length);
62+
int index = (int)UnityEngine.Random.Range (0, max);
63+
return gameMids [index];
64+
}
65+
5566

5667

5768
}
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using UnityEngine;
5+
6+
7+
/**
8+
* Each mid and corresponding trail and monster animation for each monster found in feed data
9+
*/
10+
public class TrailingMonster {
11+
// index
12+
public int mid;
13+
public int passes = 0;
14+
// scene obj references
15+
public Trail trail;
16+
public Monster monster;
17+
// constructor
18+
public TrailingMonster (int mid)
19+
{
20+
this.mid = mid;
21+
//this.trail = ReturnTrail ();
22+
}
23+
// increment each update unless this monster still exists in feed data
24+
public void Touch ()
25+
{
26+
passes++;
27+
}
28+
public void ResetPasses ()
29+
{
30+
passes = 0;
31+
}
32+
// Remove monster and trail from scene
33+
public void Remove ()
34+
{
35+
// trailManager.remove(mid) - something like this
36+
}
37+
}
38+
39+
40+
/**
41+
* Attached to Player GameObject
42+
* - Stores, adds, removes collection of monsters provided by feed data
43+
* - Calls add / remove on TrailManager and MonsterManager
44+
*/
45+
public class TrailingMonstersManager : MonoBehaviour {
46+
47+
// min, max, count of trailing monsters
48+
public int min = 3;
49+
public int max = 10;
50+
public int count;
51+
52+
// dictionary: mid->TrailingMonster
53+
public Dictionary<int, TrailingMonster> trailingMonstersDict;
54+
// mids only
55+
public List<int> mids = new List<int> ();
56+
public List<string> midsPasses = new List<string> ();
57+
// the highest number of passes in each loop
58+
public List<int> highestPassesToDelete = new List<int> ();
59+
60+
private void Awake ()
61+
{
62+
trailingMonstersDict = new Dictionary<int, TrailingMonster> ();
63+
count = trailingMonstersDict.Count;
64+
65+
StartCoroutine (UpdateTrailingMonsters (1f, true));
66+
}
67+
68+
public void Start ()
69+
{
70+
// placeholder to turn on/off in inspector
71+
}
72+
73+
74+
75+
/**
76+
* Check and update dictionary
77+
*/
78+
IEnumerator UpdateTrailingMonsters (float wait, bool random)
79+
{
80+
// wait a second
81+
yield return new WaitForSeconds (wait);
82+
83+
// testing
84+
if (random) {
85+
// use random number for length
86+
count = (int)Random.Range (min, max);
87+
} else {
88+
// use mids in Player feedData
89+
// ... count = ...
90+
}
91+
92+
// if there are trailing monsters already
93+
if (trailingMonstersDict.Count > 0) {
94+
// touch all existing
95+
foreach (KeyValuePair<int, TrailingMonster> t in trailingMonstersDict) {
96+
trailingMonstersDict [t.Key].Touch ();
97+
}
98+
}
99+
100+
//PrintDict ();
101+
102+
103+
104+
// create and add (or update) each new trailing monster
105+
for (int i = 0; i < count; i++) {
106+
107+
// random
108+
int mid = i;
109+
110+
// random mid
111+
mid = MonsterIndex.Instance.GetRandomMid (0, 20);
112+
113+
// if new mid already exists in dict
114+
if (trailingMonstersDict.ContainsKey (mid)) {
115+
// touch to reset passes
116+
trailingMonstersDict [mid].ResetPasses ();
117+
} else {
118+
// add to dict
119+
trailingMonstersDict.Add (mid, new TrailingMonster (mid));
120+
}
121+
122+
123+
124+
125+
// create each trail from random ID
126+
//GameObject obj = CreateTrail (i, false);
127+
128+
// create each trail from random MID
129+
//GameObject obj = CreateTrail (MonsterIndex.Instance.GetRandomMid (), false);
130+
131+
132+
}
133+
134+
// prune old monsters
135+
PruneTrailingMonsters ();
136+
137+
// tests so we can watch in the inspector
138+
if (trailingMonstersDict.Count > 0) {
139+
// update mids
140+
mids.Clear ();
141+
mids = trailingMonstersDict.Keys.ToList ();
142+
143+
// clear list
144+
midsPasses.Clear ();
145+
// update strings
146+
foreach (KeyValuePair<int, TrailingMonster> t in trailingMonstersDict) {
147+
midsPasses.Add (t.Key.ToString () + "-" + trailingMonstersDict [t.Key].passes.ToString ());
148+
}
149+
}
150+
151+
152+
count = trailingMonstersDict.Count;
153+
154+
// test
155+
StartCoroutine (UpdateTrailingMonsters (4f, true));
156+
}
157+
158+
159+
void PrintDict (string header = "*** trailingMonstersDict ***")
160+
{
161+
Debug.Log (header);
162+
foreach (KeyValuePair<int, TrailingMonster> t in trailingMonstersDict) {
163+
Debug.Log (trailingMonstersDict [t.Key].mid + " = " + trailingMonstersDict [t.Key].passes);
164+
}
165+
}
166+
167+
/**
168+
* Remove old monsters
169+
*/
170+
void PruneTrailingMonsters ()
171+
{
172+
// check that there are monsters to remove
173+
if (trailingMonstersDict.Count < 0 || trailingMonstersDict.Count < max) return;
174+
175+
176+
// 1. GET HIGHEST # PASSES
177+
178+
// starting with first
179+
KeyValuePair<int, TrailingMonster> highestPasses = trailingMonstersDict.First ();
180+
// loop and get highest # passes
181+
foreach (KeyValuePair<int, TrailingMonster> t in trailingMonstersDict) {
182+
if (t.Value.passes > highestPasses.Value.passes) highestPasses = t;
183+
}
184+
185+
// 2. UPDATE DELETE COLLECTION
186+
187+
// sort dict by # passes ascending so we keep the newest ones (descending use: OrderByDescending())
188+
trailingMonstersDict = trailingMonstersDict.OrderBy (x => x.Value.passes).ToDictionary (x => x.Key, x => x.Value);
189+
// reset list
190+
highestPassesToDelete.Clear ();
191+
int i = 0;
192+
// loop and
193+
foreach (KeyValuePair<int, TrailingMonster> t in trailingMonstersDict) {
194+
i++;
195+
// add those with passes >= highestPasses
196+
if (trailingMonstersDict [t.Key].passes >= highestPasses.Value.passes || i >= max) {
197+
//Debug.Log (transform.parent.name + " REMOVE " + t.Key);
198+
highestPassesToDelete.Add (t.Key);
199+
}
200+
}
201+
202+
// 3. REMOVE
203+
204+
// delete loop
205+
foreach (int mid in highestPassesToDelete) {
206+
trailingMonstersDict.Remove (mid);
207+
}
208+
}
209+
210+
211+
212+
213+
}

Assets/_Project/Scripts/Players/TrailingMonstersManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)