-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera2D.cs
361 lines (319 loc) · 12 KB
/
Camera2D.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
// Advanced Camera2D implementation
// Thanks to: http://blog.roboblob.com/2013/07/27/solving-resolution-independent-rendering-and-2d-camera-using-monogame/
// Modified and enhanced by PantheR (http://www.panthernet.ru)
// Simple usage:
// 1. Create camera
// 2. Call Camera2D.Update() method in your update method
// 3. Provide Camera2D.GetViewTransformationMatrix() to all nedded SpriteBatch.Begin() calls to draw using camera
// 4. Setup camera input that deals with such props as Zoom and Position
namespace LightningBug
{
public class Camera2D: IDisposable
{
#region Variables
private float _zoom;
private float _rotation;
private Vector2 _position;
private Matrix _transform = Matrix.Identity;
private bool _isViewTransformationDirty = true;
private Matrix _camTranslationMatrix = Matrix.Identity;
private Matrix _camRotationMatrix = Matrix.Identity;
private Matrix _camScaleMatrix = Matrix.Identity;
private Matrix _resTranslationMatrix = Matrix.Identity;
private Object followTarget;
protected ResolutionRenderer IRR;
private Vector3 _camTranslationVector = Vector3.Zero;
private Vector3 _camScaleVector = Vector3.Zero;
private Vector3 _resTranslationVector = Vector3.Zero;
/// <summary>
/// Current camera position
/// </summary>
public Vector2 Position
{
get { return _position; }
set
{
_position = value;
_isViewTransformationDirty = true;
}
}
/// <summary>
/// Minimum zoom value (can be no less than 0.1f)
/// </summary>
public float MinZoom { get; set; }
/// <summary>
/// Maximum zoom value
/// </summary>
public float MaxZoom { get; set; }
/// <summary>
/// Gets or sets camera zoom value
/// </summary>
public float Zoom
{
get { return _zoom; }
set
{
_zoom = value;
if (_zoom < 0.1f)
_zoom = 0.1f;
if (_zoom < MinZoom) _zoom = MinZoom;
if (_zoom > MaxZoom) _zoom = MaxZoom;
_isViewTransformationDirty = true;
}
}
/// <summary>
/// Gets or sets camera rotation value
/// </summary>
public float Rotation
{
get
{
return _rotation;
}
set
{
_rotation = value;
_isViewTransformationDirty = true;
}
}
#endregion
public Camera2D(ResolutionRenderer irr)
{
IRR = irr;
_zoom = 0.1f;
_rotation = 0.0f;
_position = Vector2.Zero;
MinZoom = 0.1f;
MaxZoom = 999f;
followTarget = null;
}
/*
/// <summary>
/// Center camera and fit the area of specified rectangle
/// </summary>
/// <param name="rec">Rectange</param>
/// <param name="resolution">Screen resolution</param>
public void CenterOnTarget(Rectangle rec)
{
Position = new Vector2(rec.Center.X, rec.Center.Y);
var fat1 = (float)IRR.VirtualWidth / (float)IRR.VirtualHeight;
var fat2 = (float)rec.Width / (float)rec.Height;
float ratio = 0f;
if (fat2 >= fat1) ratio = (float)IRR.VirtualWidth / (float)rec.Width;
else ratio = (float)IRR.VirtualHeight / (float)rec.Height;
Zoom = ratio;
}
*/
/// <summary>
/// Move camera by specified vector
/// </summary>
/// <param name="amount">Vector movement</param>
public void Move(Vector2 amount)
{
Position += amount;
}
/// <summary>
/// Set camera position
/// </summary>
/// <param name="position">Position</param>
public void SetPosition(Vector2 position)
{
Position = position;
}
/// <summary>
/// Get camera transformation matrix
/// </summary>
public Matrix GetViewTransformationMatrix()
{
if (_isViewTransformationDirty)
{
_camTranslationVector.X = -_position.X;
_camTranslationVector.Y = -_position.Y;
Matrix.CreateTranslation(ref _camTranslationVector, out _camTranslationMatrix);
Matrix.CreateRotationZ(_rotation, out _camRotationMatrix);
_camScaleVector.X = _zoom;
_camScaleVector.Y = _zoom;
_camScaleVector.Z = 1;
Matrix.CreateScale(ref _camScaleVector, out _camScaleMatrix);
_resTranslationVector.X = IRR.VirtualWidth * 0.5f;
_resTranslationVector.Y = IRR.VirtualHeight * 0.5f;
_resTranslationVector.Z = 0;
Matrix.CreateTranslation(ref _resTranslationVector, out _resTranslationMatrix);
_transform = _camTranslationMatrix *
_camRotationMatrix *
_camScaleMatrix *
_resTranslationMatrix *
IRR.GetTransformationMatrix();
_isViewTransformationDirty = false;
}
return _transform;
}
public Matrix GetViewTransformationMatrix2()
{
Matrix ret;
//if (_isViewTransformationDirty)
{
_camTranslationVector.X = -_position.X;
_camTranslationVector.Y = -_position.Y;
Matrix.CreateTranslation(ref _camTranslationVector, out _camTranslationMatrix);
Matrix.CreateRotationZ(_rotation, out _camRotationMatrix);
_camScaleVector.X = _zoom;
_camScaleVector.Y = _zoom;
_camScaleVector.Z = 1;
Matrix.CreateScale(ref _camScaleVector, out _camScaleMatrix);
_resTranslationVector.X = IRR.VirtualWidth * 0.5f;
_resTranslationVector.Y = IRR.VirtualHeight * 0.5f;
_resTranslationVector.Z = 0;
Matrix.CreateTranslation(ref _resTranslationVector, out _resTranslationMatrix);
ret = _camTranslationMatrix *
_camRotationMatrix *
_camScaleMatrix *
_resTranslationMatrix;
_isViewTransformationDirty = false;
}
return ret;
}
public void RecalculateTransformationMatrices()
{
_isViewTransformationDirty = true;
}
/// <summary>
/// Convert screen coordinates to virtual
/// </summary>
/// <param name="coord">Coordinates</param>
public Vector2 ToVirtual(Vector2 coord, bool useIRR = true)
{
if (useIRR) coord = coord - new Vector2(IRR.Viewport.X, IRR.Viewport.Y);
return Vector2.Transform(coord, Matrix.Invert(GetViewTransformationMatrix()));
}
public void Dispose()
{
IRR = null;
}
#region SmoothTransition Logic
private int _trDuration;
public bool IsTransitionActive { get; private set; }
private float _trElapsedTime;
private Vector2 _trTargetPosition;
/// <summary>
/// Start camera transition to specified position
/// </summary>
/// <param name="targetPos">Target position</param>
/// <param name="duration">Expected transition duration</param>
public void StartTransition(Vector2 targetPos, int duration = 5000)
{
if (IsTransitionActive)
ResetTransition();
_trTargetPosition = targetPos;
IsTransitionActive = true;
_trDuration = duration;
}
/// <summary>
/// Start camera transition to specified position
/// </summary>
/// <param name="targetPos">Target position</param>
/// <param name="duration">Expected transition duration</param>
public void StartTransition(Point targetPos, int duration = 5000)
{
StartTransition(new Vector2(targetPos.X, targetPos.Y), duration);
}
/// <summary>
/// Update transition target position without cancelling current transition
/// </summary>
/// <param name="targetPos">Target position</param>
public void UpdateTransitionTarget(Vector2 targetPos)
{
_trTargetPosition = targetPos;
}
/// <summary>
/// Stop and reset transition
/// </summary>
public void StopTransition()
{
ResetTransition();
IsTransitionActive = false;
}
/// <summary>
/// Reset current transition
/// </summary>
public void ResetTransition()
{
_trElapsedTime = 0f;
}
private void updateTransition(GameTime gt)
{
_trElapsedTime += (float)gt.ElapsedGameTime.TotalMilliseconds;
var amount = MathHelper.Clamp(_trElapsedTime / _trDuration, 0, 1);
Vector2 result;
Vector2 cpos = Position;
Vector2.Lerp(ref cpos, ref _trTargetPosition, amount, out result);
SetPosition(result);
if (amount >= 1f)
StopTransition();
}
#endregion
/// <summary>
/// Update logic (must be called within main update method)
/// </summary>
/// <param name="gt">Game time</param>
public void Update(GameTime gt, uint levelWidth, uint levelHeight)
{
if (followTarget != null)
{
//if we are already transitioning then just update target coordinates
if (IsTransitionActive)
UpdateTransitionTarget(followTarget.GetPosition());
else
Position = followTarget.GetPosition();
bool changed = false;
// Don't let the camera show parts outside of the level
Vector2 finalPosition = followTarget.GetPosition();
Vector2 screenTopLeft = ToVirtual(Vector2.Zero, false);
Vector2 screenBottomRight = ToVirtual(new Vector2(IRR.ScreenWidth, IRR.ScreenHeight), false);
float halfWidth = (screenBottomRight.X - screenTopLeft.X) / 2;
float halfHeight = (screenBottomRight.Y - screenTopLeft.Y) / 2;
if (_position.X - halfWidth < 0)
{
finalPosition.X -= _position.X - halfWidth;
changed = true;
}
else if (screenBottomRight.X > levelWidth)
{
finalPosition.X -= screenBottomRight.X - levelWidth;
changed = true;
}
if (_position.Y - halfHeight < 0)
{
finalPosition.Y -= _position.Y - halfHeight;
changed = true;
}
else if (screenBottomRight.Y > levelHeight)
{
finalPosition.Y -= screenBottomRight.Y - levelHeight;
changed = true;
}
if (changed)
{
if (IsTransitionActive)
UpdateTransitionTarget(finalPosition);
else
Position = finalPosition;
}
}
if(IsTransitionActive)
updateTransition(gt);
}
public void StartFollow(Object target)
{
followTarget = target;
}
public void StopFollow()
{
followTarget = null;
}
}
}