-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockClient.cs
75 lines (66 loc) · 2.19 KB
/
BlockClient.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
using System;
using System.Collections.Generic;
using SwinGameSDK;
namespace BroadcastStorm
{
public sealed class BlockClient
{
/// <summary>
/// This Class handles Block allocation, it is a Singleton Class, only one instance can exist.
/// The 'Sealed' keyword allows for special JIT performance during compilation.
///
/// The Class interfaces with the Block Pool and is part of the Object Pool Design Pattern
/// that has been implemented in this Program.
/// </summary>
private static readonly BlockClient _instance = new BlockClient ();
private List<Block> _activeBlocks = new List<Block> ();
private Stack<Block> _blockBuffer = new Stack<Block> ();
private BlockPool _pool = BlockPool.Instance;
private Random rnd = new Random ();
private int _rowCounter = 0;
// 'static' contructor informs the Compiler this type does not need
// to be initialised until the first field or method is accessed.
static BlockClient ()
{
}
// Private Constructor, only this Class can initialise an instance of itself
private BlockClient()
{
}
// Public Accessor, returns one private instance
public static BlockClient Instance
{
get
{
return _instance;
}
}
public void Update()
{
if (_rowCounter > 10)
{
_activeBlocks.Add (_pool.GetBlock (rnd.Next(800)));
_rowCounter = 0;
} else
{
_rowCounter++;
}
foreach (Block b in _activeBlocks)
{
b.Update ();
b.Draw ();
if (b.Y > 800)
{
_blockBuffer.Push (b);
}
}
foreach (Block b in _blockBuffer)
{
b.Reset ();
_activeBlocks.Remove (b);
_pool.RecycleBlock (b);
}
_blockBuffer.Clear ();
}
}
}