Skip to content

Commit dfd3021

Browse files
authored
Merge pull request #47 from UCL/km/star-seek-progression
Add star seek adaptive difficulty
2 parents 54ae2e8 + 48bf1be commit dfd3021

6 files changed

Lines changed: 228 additions & 26 deletions

File tree

‎docs/star-seek.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@ The star seek mini-game uses gaze + head position to collect stars that appear a
55
## Main objects / values to edit during play testing
66

77
- **StarSeekManager**: values related to the overall win condition of the game
8-
- Game time limit
8+
- Minimum game time limit (seconds)
9+
- Maximum game time limit (seconds)
10+
- How many seconds to increase the time limit, if the upgrade rate is met
11+
- Upgrade rate: (number of stars collected / game time limit) i.e. average stars collected per second, must be above this value to increase the time limit of future games.
12+
- Number of games in a row that must meet the upgrade rate
13+
14+
- **StarGenerator**: values related to spawning stars
15+
- Min distance between stars and the edge of the screen
16+
- Number of rows + columns in star spawn grid
17+
- Grid positions to exclude from star spawning (e.g. those that overlap with UI elements)
18+
- Min distance between spawned stars
919

1020
- **Prefabs/StarSeekStar**: values related to 'locking on' to a star
1121
- Time required to collect a star (with both gaze + head pose crosshair aligned)

‎projects/AstroBalance/Assets/Scenes/StarSeek.unity‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ MonoBehaviour:
151151
scoreText: {fileID: 730211112}
152152
timer: {fileID: 740804264}
153153
winScreen: {fileID: 159571674}
154-
timeLimit: 120
154+
minTimeLimit: 60
155+
maxTimeLimit: 180
156+
timeLimitIncrement: 60
157+
timeLimitUpgradeRate: 0.3
158+
nGamesToUpgrade: 3
155159
--- !u!4 &5281182
156160
Transform:
157161
m_ObjectHideFlags: 0
@@ -407,6 +411,14 @@ MonoBehaviour:
407411
m_Name:
408412
m_EditorClassIdentifier:
409413
starPrefab: {fileID: 7594402928058972626, guid: 8a1f0e2645d064545887a1468814863e, type: 3}
414+
edgeOffset: 1
415+
nRows: 4
416+
nColumns: 6
417+
gridPositionsToExclude:
418+
- {x: 0, y: 3}
419+
- {x: 5, y: 3}
420+
- {x: 5, y: 0}
421+
minDistance: 7
410422
--- !u!4 &609642610
411423
Transform:
412424
m_ObjectHideFlags: 0

‎projects/AstroBalance/Assets/Scripts/SaveData.cs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ public T GetLastGameData()
4545
return savedGames.LastOrDefault();
4646
}
4747

48+
/// <summary>
49+
/// Get data from the last n played games.
50+
/// </summary>
51+
/// <param name="nGames">Number of games to retrieve</param>
52+
public IEnumerable<T> GetLastNGamesData(int nGames)
53+
{
54+
return savedGames.TakeLast(nGames);
55+
}
56+
4857
public void Save()
4958
{
5059
string json = JsonUtility.ToJson(this, true);

‎projects/AstroBalance/Assets/Scripts/StarSeek/LockedOn.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class LockedOn : MonoBehaviour
66
{
77
[SerializeField, Tooltip("Number of seconds required to collect star")]
8-
private float doubleLockTime = 2;
8+
private float doubleLockTime = 0.5f;
99

1010
[SerializeField, Tooltip("Particle system to be shown on star collection")]
1111
private GameObject collectEffect;
Lines changed: 101 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using Unity.VisualScripting;
@@ -8,55 +9,134 @@ public class StarSeekGenerator : MonoBehaviour
89
[SerializeField, Tooltip("Star prefab to generate")]
910
private GameObject starPrefab;
1011

11-
private List<Vector2> spawnLocations = new List<Vector2>
12-
{
13-
new Vector2(-8, 0), // left
14-
new Vector2(8, 0), // right
15-
new Vector2(0, 4), // up
16-
new Vector2(0, -4), // down
17-
};
12+
[SerializeField, Tooltip("Min distance between stars and the edge of the screen.")]
13+
private int edgeOffset = 1;
14+
15+
[SerializeField, Tooltip("Number of rows in star spawn grid")]
16+
private int nRows = 4;
17+
18+
[SerializeField, Tooltip("Number of columns in star spawn grid")]
19+
private int nColumns = 6;
20+
21+
[
22+
SerializeField,
23+
Tooltip(
24+
"Positions in the spawn grid to exclude (e.g. overlapping with UI elements). (0, 0) is the bottom left star and (nColumns - 1, nRows - 1) is the top right star."
25+
)
26+
]
27+
private List<Vector2> gridPositionsToExclude = new List<Vector2>();
28+
29+
[SerializeField, Tooltip("Min distance between spawned stars"), Range(0, 9)]
30+
private int minDistance = 7;
31+
32+
private List<Vector2> spawnLocations = new List<Vector2>();
1833
private GameObject currentStar;
19-
private int lastSpawnLocationIndex = -1;
34+
private bool firstSpawn = true;
35+
private Vector2 lastSpawnLocation;
2036

2137
// Start is called once before the first execution of Update after the MonoBehaviour is created
2238
void Start()
2339
{
24-
spawnStar();
40+
FillSpawnLocations();
41+
SpawnStar();
2542
}
2643

27-
private void spawnStar()
44+
/// <summary>
45+
/// Create a grid of spawn locations, excluding those in 'gridPositionsToExclude'
46+
/// </summary>
47+
private void FillSpawnLocations()
2848
{
29-
int chosenIndex;
30-
if (lastSpawnLocationIndex == -1)
49+
Vector2 gridBottomLeft =
50+
Camera.main.ViewportToWorldPoint(new Vector2(0, 0))
51+
+ new Vector3(edgeOffset, edgeOffset, 0);
52+
Vector2 gridTopRight =
53+
Camera.main.ViewportToWorldPoint(new Vector2(1, 1))
54+
- new Vector3(edgeOffset, edgeOffset, 0);
55+
56+
float xSpacing = (gridTopRight.x - gridBottomLeft.x) / (nColumns - 1);
57+
float ySpacing = (gridTopRight.y - gridBottomLeft.y) / (nRows - 1);
58+
59+
float xLocation = gridBottomLeft.x;
60+
float yLocation = gridBottomLeft.y;
61+
for (int i = 0; i < nColumns; i++)
62+
{
63+
for (int j = 0; j < nRows; j++)
64+
{
65+
AddSpawnLocation(new Vector2(xLocation, yLocation), new Vector2(i, j));
66+
yLocation += ySpacing;
67+
}
68+
69+
xLocation += xSpacing;
70+
yLocation = gridBottomLeft.y;
71+
}
72+
}
73+
74+
/// <summary>
75+
/// Add spawn location, if it is not in 'gridPositionsToExclude'
76+
/// </summary>
77+
/// <param name="worldPosition">Unity world position to spawn</param>
78+
/// <param name="gridPosition">Position in grid (column, row)</param>
79+
private void AddSpawnLocation(Vector2 worldPosition, Vector2 gridPosition)
80+
{
81+
foreach (Vector2 excludeLocation in gridPositionsToExclude)
82+
{
83+
if (gridPosition == excludeLocation)
84+
{
85+
return;
86+
}
87+
}
88+
89+
spawnLocations.Add(worldPosition);
90+
}
91+
92+
private void SpawnStar()
93+
{
94+
List<Vector2> possibleLocations = new List<Vector2>();
95+
96+
if (firstSpawn)
3197
{
3298
// If this is our first time generating a star, choose from all locations
33-
chosenIndex = Random.Range(0, spawnLocations.Count);
99+
possibleLocations = spawnLocations;
100+
firstSpawn = false;
34101
}
35102
else
36103
{
37-
// If we have spawned a star previously, make sure it moves to a new location
38-
IEnumerable<int> indexes = Enumerable.Range(0, spawnLocations.Count);
39-
indexes = indexes.Except(new int[] { lastSpawnLocationIndex });
104+
// If we have spawned a star previously, choose a new location that is at
105+
// least minDistance away
106+
foreach (Vector2 spawnLocation in spawnLocations)
107+
{
108+
float distance = Vector2.Distance(spawnLocation, lastSpawnLocation);
109+
if (distance >= minDistance)
110+
{
111+
possibleLocations.Add(spawnLocation);
112+
}
113+
}
114+
}
40115

41-
chosenIndex = indexes.ElementAt(Random.Range(0, indexes.Count()));
116+
if (possibleLocations.Count() == 0)
117+
{
118+
throw new InvalidOperationException(
119+
"No valid spawn locations available - try decreasing minDistance."
120+
);
42121
}
43122

44-
// Spawn a star at the randomly chosen location
45-
Vector2 chosenLocation = spawnLocations.ElementAt(chosenIndex);
123+
// Spawn a star at a randomly chosen location
124+
int chosenIndex = UnityEngine.Random.Range(0, possibleLocations.Count);
125+
Vector2 chosenLocation = possibleLocations.ElementAt(chosenIndex);
46126
currentStar = Instantiate(
47127
starPrefab,
48128
new Vector3(chosenLocation.x, chosenLocation.y, 0),
49129
Quaternion.identity
50130
);
51-
lastSpawnLocationIndex = chosenIndex;
131+
lastSpawnLocation = chosenLocation;
52132
}
53133

54134
// Update is called once per frame
55135
void Update()
56136
{
57137
if (currentStar.IsDestroyed())
58138
{
59-
spawnStar();
139+
SpawnStar();
60140
}
61141
}
62142
}

‎projects/AstroBalance/Assets/Scripts/StarSeek/StarSeekManager.cs‎

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
13
using TMPro;
24
using UnityEngine;
35

@@ -12,10 +14,33 @@ public class StarSeekManager : MonoBehaviour
1214
[SerializeField, Tooltip("Screen shown upon winning the game")]
1315
private GameObject winScreen;
1416

15-
[SerializeField, Tooltip("Game time limit in seconds")]
16-
private int timeLimit = 120;
17+
[SerializeField, Tooltip("Minimum game time limit in seconds")]
18+
private int minTimeLimit = 60;
19+
20+
[SerializeField, Tooltip("Maximum game time limit in seconds")]
21+
private int maxTimeLimit = 180;
22+
23+
[SerializeField, Tooltip("Time limit increase if timeLimitUpgradeRate is met")]
24+
private int timeLimitIncrement = 60;
25+
26+
[
27+
SerializeField,
28+
Tooltip(
29+
"(number of stars collected / game time limit) i.e. average stars collected per second - must be above this value to increase the time limit of future games."
30+
)
31+
]
32+
private float timeLimitUpgradeRate = 0.3f;
33+
34+
[
35+
SerializeField,
36+
Tooltip(
37+
"Number of games in a row that must meet timeLimitUpgradeRate to increase the time limit"
38+
)
39+
]
40+
private int nGamesToUpgrade = 3;
1741

1842
private int score;
43+
private int timeLimit;
1944
private TextMeshProUGUI winText;
2045
private bool gameActive = true;
2146
private StarSeekData gameData;
@@ -25,12 +50,62 @@ public class StarSeekManager : MonoBehaviour
2550
void Start()
2651
{
2752
winText = winScreen.GetComponentInChildren<TextMeshProUGUI>();
53+
ChooseGameTimeLimit();
54+
2855
score = 0;
2956
scoreText.text = score.ToString();
3057
gameData = new StarSeekData();
3158
timer.StartCountdown(timeLimit);
3259
}
3360

61+
/// <summary>
62+
/// Load previous game data (if any), and choose time limit for this game based
63+
/// on prior perfomance.
64+
/// </summary>
65+
private void ChooseGameTimeLimit()
66+
{
67+
SaveData<StarSeekData> saveData = new(saveFilename);
68+
IEnumerable<StarSeekData> lastNGamesData = saveData.GetLastNGamesData(nGamesToUpgrade);
69+
70+
if (lastNGamesData.Count() < nGamesToUpgrade)
71+
{
72+
SetTimeLimit(minTimeLimit);
73+
return;
74+
}
75+
76+
// Upgrade if all the last n games have the same time limit + meet the upgrade
77+
// rate. If it's a mix of time limits, then we haven't played enough games at
78+
// this level yet to progress.
79+
int nGamesAtUpgradeRate = 0;
80+
bool allSameTimeLimit = true;
81+
int lastTimeLimit = lastNGamesData.Last().timeLimitSeconds;
82+
83+
foreach (StarSeekData data in lastNGamesData)
84+
{
85+
float starRate = (float)data.nStarsCollected / (float)data.timeLimitSeconds;
86+
87+
if (data.timeLimitSeconds != lastTimeLimit)
88+
{
89+
allSameTimeLimit = false;
90+
break;
91+
}
92+
93+
if (starRate >= timeLimitUpgradeRate)
94+
{
95+
nGamesAtUpgradeRate++;
96+
}
97+
}
98+
99+
if (allSameTimeLimit && nGamesAtUpgradeRate >= nGamesToUpgrade)
100+
{
101+
SetTimeLimit(lastTimeLimit + timeLimitIncrement);
102+
}
103+
else
104+
{
105+
SetTimeLimit(lastTimeLimit);
106+
}
107+
}
108+
34109
// Update is called once per frame
35110
void Update()
36111
{
@@ -77,4 +152,20 @@ private void SaveGameData()
77152
SaveData<StarSeekData> saveData = new(saveFilename);
78153
saveData.SaveGameData(gameData);
79154
}
155+
156+
private void SetTimeLimit(int limit)
157+
{
158+
if (limit > maxTimeLimit)
159+
{
160+
timeLimit = maxTimeLimit;
161+
}
162+
else if (limit < minTimeLimit)
163+
{
164+
timeLimit = minTimeLimit;
165+
}
166+
else
167+
{
168+
timeLimit = limit;
169+
}
170+
}
80171
}

0 commit comments

Comments
 (0)