-
Notifications
You must be signed in to change notification settings - Fork 48
/
CameraBounds.cs
48 lines (34 loc) · 1.01 KB
/
CameraBounds.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
using Microsoft.Xna.Framework;
namespace Nez.Samples
{
public class CameraBounds : Component, IUpdatable
{
public Vector2 Min, Max;
public CameraBounds()
{
// make sure we run last so the camera is already moved before we evaluate its position
SetUpdateOrder(int.MaxValue);
}
public CameraBounds(Vector2 min, Vector2 max) : this()
{
Min = min;
Max = max;
}
public override void OnAddedToEntity()
{
Entity.UpdateOrder = int.MaxValue;
}
void IUpdatable.Update()
{
var cameraBounds = Entity.Scene.Camera.Bounds;
if (cameraBounds.Top < Min.Y)
Entity.Scene.Camera.Position += new Vector2(0, Min.Y - cameraBounds.Top);
if (cameraBounds.Left < Min.X)
Entity.Scene.Camera.Position += new Vector2(Min.X - cameraBounds.Left, 0);
if (cameraBounds.Bottom > Max.Y)
Entity.Scene.Camera.Position += new Vector2(0, Max.Y - cameraBounds.Bottom);
if (cameraBounds.Right > Max.X)
Entity.Scene.Camera.Position += new Vector2(Max.X - cameraBounds.Right, 0);
}
}
}