-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cs
231 lines (205 loc) · 8.24 KB
/
Game.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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace TheraWii
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game : Microsoft.Xna.Framework.Game
{
DialogTask goodbye;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont font;
Therapy therapy;
Profile profile;
Session session;
bool justWentFullScreen = false;
Matrix worldMatrix;
Matrix viewMatrix;
Matrix projectionMatrix;
BasicEffect basicEffect;
VertexDeclaration vertexDeclaration;
Line xAxis, yAxis;
private const int PREFERRED_WIDTH = 1024;
private const int PREFERRED_HEIGHT = 768;
private const long defaultTargetElapsedTime = 333333; // 1/30th of a second in 100s of nanoseconds (ticks).;
public Game(Therapy t, Profile p, bool fullscreen)
{
therapy = t;
profile = p;
session = new Session();
session.Initialize(profile, therapy);
profile.Sessions.Add(session);
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = PREFERRED_WIDTH; // GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
graphics.PreferredBackBufferHeight = PREFERRED_HEIGHT; // GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
graphics.IsFullScreen = fullscreen;
InputDevice.GAME_WIDTH = graphics.PreferredBackBufferWidth;
InputDevice.GAME_HEIGHT = graphics.PreferredBackBufferHeight;
this.IsFixedTimeStep = true;
this.TargetElapsedTime = new TimeSpan(defaultTargetElapsedTime);
graphics.PreferMultiSampling = true;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
InitializeTransform();
InitializeEffect();
xAxis = new Line(GraphicsDevice, new Vector3(-1, 0, 0), new Vector3(1, 0, 0), Color.Gray);
yAxis = new Line(GraphicsDevice, new Vector3(0, -1, 0), new Vector3(0, 1, 0), Color.Gray);
Mouse.SetPosition(PREFERRED_WIDTH / 2, PREFERRED_HEIGHT / 2);
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
therapy.tasks.Initialize(graphics, Content, spriteBatch, PREFERRED_WIDTH, PREFERRED_HEIGHT, session, Vector3.Zero);
font = Content.Load<SpriteFont>("Fonts\\GameFont");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
Content.Unload();
}
/// <summary>
/// Initializes the transforms used by the game.
/// </summary>
private void InitializeTransform()
{
viewMatrix = Matrix.CreateLookAt(
new Vector3(0.0f, 0.0f, 10.0f),
Vector3.Zero,
Vector3.Up
);
projectionMatrix = Matrix.CreatePerspective(
0.2f,
0.2f,
1.0f, 1000.0f);
}
/// <summary>
/// Initializes the effect (loading, parameter setting, and technique selection)
/// used by the game.
/// </summary>
private void InitializeEffect()
{
vertexDeclaration = new VertexDeclaration(
GraphicsDevice,
VertexPositionColor.VertexElements
);
basicEffect = new BasicEffect(GraphicsDevice, null);
basicEffect.VertexColorEnabled = true;
worldMatrix = Matrix.Identity;
basicEffect.World = worldMatrix;
basicEffect.View = viewMatrix;
basicEffect.Projection = projectionMatrix;
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
WiiUse.Poll();
// Allows the game to exit
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
{
this.Exit();
}
// Toggle Full-screen with Alt-Enter
KeyboardState keyState = Keyboard.GetState();
if ((keyState.IsKeyDown(Keys.RightAlt) ||
keyState.IsKeyDown(Keys.LeftAlt)) &&
keyState.IsKeyDown(Keys.Enter))
{
if (!justWentFullScreen)
{
graphics.ToggleFullScreen();
justWentFullScreen = true;
}
}
else
{
justWentFullScreen = false;
}
// Do we exit
if (therapy.tasks.isComplete() && goodbye == null)
{
goodbye = new DialogTask();
goodbye.endCondition = new TimeLimitEndCondition();
((TimeLimitEndCondition)goodbye.endCondition).TimeLimit = (double)2.0;
((TimeLimitEndCondition)goodbye.endCondition).Type = TimeLimitType.TotalTime;
goodbye.DisplayText = "Therapy Complete!";
goodbye.Name = "Complete";
goodbye.Initialize(this.graphics, this.Content, this.spriteBatch, PREFERRED_WIDTH, PREFERRED_HEIGHT, this.session, Vector3.Zero);
goodbye.textBox.Initialize(goodbye.DisplayText, this.Content, this.spriteBatch, PREFERRED_WIDTH, PREFERRED_HEIGHT);
}
if (goodbye != null)
{
if (goodbye.isComplete())
this.Exit();
else
goodbye.Update(gameTime);
}
else
{
// Update Current task
therapy.tasks.Update(gameTime);
}
base.Update(gameTime);
}
protected override void OnExiting(object sender, EventArgs args)
{
base.OnExiting(sender, args);
session.Finish();
UnloadContent();
graphics.GraphicsDevice.Dispose();
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.White);
GraphicsDevice.VertexDeclaration = vertexDeclaration;
basicEffect.Begin();
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Begin();
xAxis.Draw();
yAxis.Draw();
if (!therapy.tasks.isComplete())
therapy.tasks.Draw(gameTime);
else if (goodbye != null)
goodbye.Draw(gameTime);
pass.End();
}
basicEffect.End();
base.Draw(gameTime);
}
}
}