/
Camera.cs
195 lines (152 loc) · 4.96 KB
/
Camera.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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
// TODO: Documentation for Camera code.
namespace Mammoth.Engine
{
/// <summary>
/// This interface encapsulates the idea of a camera service. This can be used with Game.GetService()
/// to get the current camera.
/// </summary>
public interface ICameraService
{
Matrix View
{
get;
}
Matrix Projection
{
get;
}
Camera.CameraType Type
{
get;
}
}
public abstract class Camera : GameComponent, ICameraService
{
#region Variables
public enum CameraType
{
FIRST_PERSON,
THIRD_PERSON,
MENU
}
#endregion
public Camera(Game game) : base(game)
{
}
public abstract override void Update(GameTime gameTime);
public override void Initialize()
{
base.Initialize();
UpdateProjection();
}
/// <summary>
/// This updates the projection matrix based on the game's aspect ratio. This should be called whenever
/// the resolution is changed to ensure that the projection matrix is correct.
/// </summary>
public void UpdateProjection()
{
this.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
Engine.Instance.GraphicsDevice.Viewport.AspectRatio,
0.1f, 10000.0f);
}
/*public void UpdateView()
{
LocalPlayer lp = Engine.Instance.LocalPlayer;
Vector3 forward = Vector3.Transform(Vector3.Forward, lp.HeadOrient);
Vector3 position, look;
switch(this.Type)
{
case CameraType.FIRST_PERSON:
position = lp.Position + (Vector3.Up * lp.Height / 4.0f);
look = position + forward;
break;
// TODO: Make sure that the third-person "works".
// I don't think it'll look any good, but once we have something drawing for the player, we
// should fix it up so that it's somewhat useful for debugging.
case CameraType.THIRD_PERSON:
position = lp.Position - forward * 15 + Vector3.Up * 5;
look = lp.Position + Vector3.Up * lp.Height * 3 / 4;
break;
default:
position = Vector3.Zero;
look = Vector3.Zero;
break;
}
this.View = Matrix.CreateLookAt(position, look, Vector3.Up);
}*/
#region Properties
public Matrix View
{
get;
protected set;
}
public Matrix Projection
{
get;
protected set;
}
public abstract CameraType Type
{
get;
protected set;
}
#endregion
}
public class FirstPersonCamera : Camera
{
#region Variables
private CameraType _type;
#endregion
public FirstPersonCamera(Game game)
: base(game)
{
this.Type = CameraType.FIRST_PERSON;
}
public override void Update(GameTime gameTime)
{
LocalPlayer lp = Engine.Instance.LocalPlayer;
Vector3 forward = Vector3.Transform(Vector3.Forward, lp.HeadOrient);
Vector3 position = lp.Position + (Vector3.Up * lp.Height / 4.0f);
Vector3 look = position + forward;
this.View = Matrix.CreateLookAt(position, look, Vector3.Up);
}
#region Properties
public override CameraType Type
{
get { return _type; }
protected set { _type = value; }
}
#endregion
}
public class ThirdPersonCamera : Camera
{
#region Variables
private CameraType _type;
#endregion
public ThirdPersonCamera(Game game)
: base(game)
{
this.Type = CameraType.THIRD_PERSON;
}
public override void Update(GameTime gameTime)
{
LocalPlayer lp = Engine.Instance.LocalPlayer;
Vector3 forward = Vector3.Transform(Vector3.Forward, lp.HeadOrient);
Vector3 position = lp.Position - forward * 15 + Vector3.Up * 5;
Vector3 look = lp.Position + Vector3.Up * lp.Height * 3 / 4;
this.View = Matrix.CreateLookAt(position, look, Vector3.Up);
}
#region Properties
public override CameraType Type
{
get { return _type; }
protected set { _type = value; }
}
#endregion
}
}