forked from ToadsworthLP/desktoptale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DistractionManager.cs
210 lines (172 loc) · 7.24 KB
/
DistractionManager.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using Desktoptale.Distractions;
using Desktoptale.Messages;
using Desktoptale.Messaging;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using SharpDX;
using Rectangle = Microsoft.Xna.Framework.Rectangle;
using Vector2 = Microsoft.Xna.Framework.Vector2;
namespace Desktoptale
{
public class DistractionManager : IDistractionManager
{
public const int MaxDistractionLevel = 5;
private const float ScaleDivisor = 600f;
private static readonly float[] DistractionSpawnDelays = new[] { 128f, 32f, 8f, 2f, 0.5f };
private ContentManager contentManager;
private GameWindow window;
private IList<IDistractionPattern> patterns;
private ISet<IDistraction> distractions;
private Vector2 scale;
private IList<IDistraction> removalScheduled;
private int distractionLevel;
private Random random;
private int lastDistractionIndex = -1;
private WindowTracker windowTracker;
private TrackedWindow trackedWindow;
private TimeSpan nextScheduledDistractionTime = TimeSpan.MinValue;
public DistractionManager(ContentManager contentManager, GameWindow window, WindowTracker windowTracker)
{
this.contentManager = contentManager;
this.window = window;
this.windowTracker = windowTracker;
patterns = new List<IDistractionPattern>();
distractions = new HashSet<IDistraction>();
removalScheduled = new List<IDistraction>();
random = new Random();
MessageBus.Subscribe<SetDistractionLevelMessage>(OnSetDistractionLevelMessage);
MessageBus.Subscribe<SetDistractionScaleMessage>(OnSetDistractionScaleMessage);
MessageBus.Subscribe<SetDistractionTrackedWindowMessage>(OnSetDistractionTrackedWindowMessage);
}
public void Initialize()
{
scale = new Vector2(2, 2);
patterns.Add(new UpDownOppositeBonesPattern(6, 300f, 120f, 100));
patterns.Add(new LeftRightOppositeBonesPattern(6, 300f, 120f, 100));
patterns.Add(new ScreenEdgeBonesPattern(6, 250f, 120f, true));
patterns.Add(new ScreenEdgeBonesPattern(6, 250f, 120f, false));
patterns.Add(new RandomGasterBlasterPattern(5, 0.5f));
patterns.Add(new SideGasterBlasterPattern(10, 0.25f));
}
public void Update(GameTime gameTime)
{
Rectangle bounds;
if (trackedWindow?.Bounds != null)
{
bounds = trackedWindow.Bounds;
}
else
{
bounds = window.ClientBounds;
if (bounds.X < 0) bounds.X = 0;
if (bounds.Y < 0) bounds.Y = 0;
}
if (distractionLevel > 0)
{
if (nextScheduledDistractionTime < gameTime.TotalGameTime)
{
int index = random.Next(0, patterns.Count);
if (index == lastDistractionIndex) index = (index + 1) % patterns.Count;
lastDistractionIndex = index;
float waitMultiplier = patterns[index].Spawn(this, bounds, scale);
float delay = DistractionSpawnDelays[distractionLevel - 1];
delay *= random.NextFloat(0.8f, 1.2f) * waitMultiplier;
nextScheduledDistractionTime = gameTime.TotalGameTime + TimeSpan.FromSeconds(delay);
}
}
foreach (IDistraction distraction in distractions)
{
if (distraction.Disposed)
{
removalScheduled.Add(distraction);
}
}
if (removalScheduled.Count > 0)
{
distractions.ExceptWith(removalScheduled);
removalScheduled.Clear();
}
foreach (IDistraction distraction in distractions)
{
distraction.Update(gameTime, bounds);
}
}
public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
foreach (IDistraction distraction in distractions)
{
distraction.Draw(gameTime, spriteBatch, window.ClientBounds);
}
}
public void AddDistraction(IDistraction distraction)
{
distraction.Scale *= scale;
distraction.LoadContent(contentManager);
distraction.Initialize();
distractions.Add(distraction);
}
public void RemoveDistraction(IDistraction distraction)
{
removalScheduled.Add(distraction);
}
public void RemoveAllDistractions()
{
foreach (IDistraction distraction in distractions)
{
removalScheduled.Add(distraction);
}
}
private void OnSetDistractionLevelMessage(SetDistractionLevelMessage message)
{
if(message.Level > MaxDistractionLevel && message.Level < 0) return;
if (message.Level != distractionLevel)
{
RemoveAllDistractions();
nextScheduledDistractionTime = TimeSpan.MinValue;
}
distractionLevel = message.Level;
}
private void OnSetDistractionScaleMessage(SetDistractionScaleMessage message)
{
scale = new Vector2(message.Scale, message.Scale);
RemoveAllDistractions();
nextScheduledDistractionTime = TimeSpan.MinValue;
}
private void OnSetDistractionTrackedWindowMessage(SetDistractionTrackedWindowMessage message)
{
if ((trackedWindow != null && message.Window != null && trackedWindow.Window.hWnd != message.Window.hWnd) ||
(trackedWindow == null && message.Window != null) ||
(trackedWindow != null && message.Window == null))
{
RemoveAllDistractions();
nextScheduledDistractionTime = TimeSpan.MinValue;
}
if (message.Window == null)
{
if (trackedWindow != null)
{
windowTracker.Unsubscribe(trackedWindow.Window);
trackedWindow.WindowDestroyed -= OnContainingWindowDestroyed;
trackedWindow = null;
}
}
else
{
if (trackedWindow != null)
{
windowTracker.Unsubscribe(trackedWindow.Window);
trackedWindow.WindowDestroyed -= OnContainingWindowDestroyed;
}
trackedWindow = windowTracker.Subscribe(message.Window);
trackedWindow.WindowDestroyed += OnContainingWindowDestroyed;
}
}
private void OnContainingWindowDestroyed()
{
MessageBus.Send(new SetDistractionTrackedWindowMessage() { Window = null });
}
}
}