-
Notifications
You must be signed in to change notification settings - Fork 3
/
ColumnsManager.cs
67 lines (60 loc) · 2.43 KB
/
ColumnsManager.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
using System;
using System.Collections.Generic;
namespace MatrixScreen
{
/// <summary>
/// This class manages the instantiation and cleanup of an appropriate number of columns.
/// </summary>
internal class ColumnsManager : IRenderable
{
private const float _columnCountFactor = 0.75f;
private LinkedList<Column> _columns;
private int _columnCount = 0;
private ShuffleInts _indexShuffler;
internal ColumnsManager()
{
_columns = new LinkedList<Column>();
_indexShuffler = new ShuffleInts(Console.WindowWidth);
}
public void Draw(long elapsedTime)
{
foreach (Column column in _columns)
{
column.Draw(elapsedTime);
}
}
public void Update(long elapsedTime)
{
_columnCount = (int)(Console.WindowWidth * _columnCountFactor);
if (_columnCount < 1)
{
_columnCount = 1;
}
// Update our list of available columns positions if the window size changed.
if (_indexShuffler.SetSize != Console.WindowWidth)
{
_indexShuffler = new ShuffleInts(Console.WindowWidth);
}
// Iterate over the list manually because we can delete nodes directly.
LinkedListNode<Column> itemNode = _columns.First;
while (itemNode != null && itemNode.Value != null)
{
itemNode.Value.Update(elapsedTime); // Update the column
if (itemNode.Value.IsDone)
{
_columns.Remove(itemNode); // Get rid of the column if it's done.
_indexShuffler.CheckinInteger(itemNode.Value.ColumnIndex); // Make the column index available to the pool of candidate columns
}
itemNode = itemNode.Next; // Even if the node was deleted from the list, the next pointer still points to the next node in the list.
}
while (_columns.Count < _columnCount)
{
int columnIndex = _indexShuffler.CheckoutRandomInteger();
if (columnIndex > -1)
{
_columns.AddLast(new Column(columnIndex));
}
}
}
}
}