/
Client.cs
471 lines (397 loc) · 16.6 KB
/
Client.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using Yuusha.gui;
using Yuusha.Utility.Settings;
namespace Yuusha
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Client : Game
{
#region Private Data
private static bool m_roundDelay = false;
private static TimeSpan m_roundDelayStart;
private static bool m_hasFocus;
private static SpriteBatch m_spriteBatch;
private static Enums.EGameState m_gameState;
private static Enums.EGameDisplayMode m_gameDisplayMode;
private static int m_width;
private static int m_height;
private static string m_title;
private static UserSettings m_userSettings = new UserSettings();
private static ServerSettings m_serverSettings = new ServerSettings();
private static ClientSettings m_clientSettings = new ClientSettings();
private static int m_preferredWindowWidth = 1280;
private static int m_preferredWindowHeight = 720;
private static readonly SurfaceFormat m_preferredSurfaceFormat = SurfaceFormat.Color;
private static readonly DepthFormat m_preferredDepthFormat = DepthFormat.Depth24;
private static Color m_deviceClearColor = Color.Black;
private static bool m_isFullScreen = false;
private static Rectangle m_prevClientBounds;
private static Rectangle m_nowClientBounds;
private static Point m_prevClientPosition;
private static TimeSpan m_lastPing;
GraphicsDeviceManager m_graphics;
readonly ContentManager m_contentManager;
GuiManager m_guiManager;
readonly GameHUD m_gameHUD;
readonly Audio.AudioManager m_audioManager;
bool m_firstFullScreen;
bool m_noDraw;
#endregion
#region Public Properties
public static UserSettings UserSettings
{
get { return m_userSettings; }
}
public static ServerSettings ServerSettings
{
get { return m_serverSettings; }
}
public static ClientSettings ClientSettings
{
get { return m_clientSettings; }
}
public static SpriteBatch SpriteBatch
{
get { return m_spriteBatch; }
}
public static Enums.EGameState GameState
{
get { return m_gameState; }
set { m_gameState = value; }
}
public static Enums.EGameDisplayMode GameDisplayMode
{
get { return m_gameDisplayMode; }
set { m_gameDisplayMode = value; }
}
public static int Width
{
get { return m_width; }
}
public static int Height
{
get { return m_height; }
}
public static string Title
{
get { return m_title; }
set { m_title = value; }
}
public static int PreferredWindowWidth
{
get { return m_preferredWindowWidth; }
set { m_preferredWindowWidth = value; }
}
public static int PreferredWindowHeight
{
get { return m_preferredWindowHeight; }
set { m_preferredWindowHeight = value; }
}
public static Color DeviceClearColor
{
get { return m_deviceClearColor; }
set { m_deviceClearColor = value; }
}
public static bool IsFullScreen
{
get { return m_isFullScreen; }
set { m_isFullScreen = value; }
}
public static Rectangle PrevClientBounds
{
get { return m_prevClientBounds; }
}
public static Rectangle NowClientBounds
{
get { return m_nowClientBounds; }
}
public static bool HasFocus
{
get { return m_hasFocus; }
}
public static bool RoundDelay
{
get { return m_roundDelay; }
set { m_roundDelay = value; }
}
public static TimeSpan LastPing
{
get { return m_lastPing; }
set { m_lastPing = value; }
}
//public static TimeSpan TotalGameTime
//{
// get { return m_totalGameTime; }
//}
public GuiManager GUIManager
{
get { return m_guiManager; }
}
//public SplashScreen SplashScreen
//{
// get { return m_splashScreen; }
//}
public GameTime ClientGameTime
{ get; set; }
public static bool InGame
{ get { return Client.GameState.ToString().EndsWith("Game"); } }
#endregion
public Client()
{
m_graphics = new GraphicsDeviceManager(this);
m_contentManager = new ContentManager(Services);
m_guiManager = new GuiManager(this);
Components.Add(m_guiManager);
m_gameHUD = new GameHUD(this);
Components.Add(m_gameHUD);
m_audioManager = new Audio.AudioManager(this);
Components.Add(m_audioManager);
Deactivated += new EventHandler<EventArgs>(Client_Deactivated);
Activated += new EventHandler<EventArgs>(Client_Activated);
Disposed += Client_Disposed;
Content.RootDirectory = "Content";
m_firstFullScreen = true;
m_noDraw = false;
Window.AllowUserResizing = true;
Window.ClientSizeChanged += Window_ClientSizeChanged;
}
private void Client_Disposed(object sender, EventArgs e)
{
OnExiting(sender, e);
}
private void Window_ClientSizeChanged(object sender, EventArgs e)
{
//m_prevClientBounds = new Rectangle(Window.Position.X, Window.Position.Y, PreferredWindowWidth, PreferredWindowHeight);
//m_preferredWindowHeight = Window.ClientBounds.Height;
//m_preferredWindowWidth = Window.ClientBounds.Width;
//m_height = Window.ClientBounds.Height;
//m_width = Window.ClientBounds.Width;
//m_graphics.PreferredBackBufferHeight = m_preferredWindowHeight;
//m_graphics.PreferredBackBufferWidth = m_preferredWindowWidth;
//m_graphics.ApplyChanges();
//OnClientResize();
if (Window.ClientBounds.Height < PreferredWindowHeight || Window.ClientBounds.Width < PreferredWindowWidth)
m_hasFocus = false;
else m_hasFocus = true;
}
private void Client_Activated(object sender, EventArgs e)
{
m_hasFocus = true;
switch (GameState)
{
case Enums.EGameState.IOKGame:
case Enums.EGameState.SpinelGame:
case Enums.EGameState.YuushaGame:
if (GuiManager.CurrentSheet[Globals.GAMEINPUTTEXTBOX] is TextBox inputTextBox)
{
inputTextBox.HasFocus = true;
GuiManager.ActiveTextBox = inputTextBox.Name;
}
break;
}
}
private void Client_Deactivated(object sender, EventArgs e)
{
m_hasFocus = false;
}
/// <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()
{
base.Initialize();
TargetElapsedTime = TimeSpan.FromSeconds(1.0f / 100.0f);
IsFixedTimeStep = false;
//In some cases you want to call the Draw() method at maximum intensity.
//m_graphics.SynchronizeWithVerticalRetrace = false;
// set up the back buffer
m_graphics.PreferredBackBufferWidth = m_preferredWindowWidth;
m_graphics.PreferredBackBufferHeight = m_preferredWindowHeight;
m_graphics.PreferredBackBufferFormat = m_preferredSurfaceFormat;
m_graphics.PreferredDepthStencilFormat = m_preferredDepthFormat;
m_graphics.ApplyChanges();
// create necessary directories
Utils.CreateDirectories();
// load settings
m_userSettings = UserSettings.Load();
m_clientSettings = ClientSettings.Load();
Character.LoadSettings();
m_gameState = Enums.EGameState.Login;
m_preferredWindowWidth = 1024;
m_preferredWindowHeight = 768;
//m_isFullScreen = UserSettings.FullScreen;
Events.RegisterEvent(Events.EventName.Set_Login_State, Enums.ELoginState.Disconnected);
Events.RegisterEvent(Events.EventName.Set_Game_State, Enums.EGameState.Login);
Title = string.Format("{0} (v{1})", StaticSettings.ClientName, StaticSettings.ClientVersion);
//m_graphics.SynchronizeWithVerticalRetrace = true;
//m_graphics.GraphicsProfile = GraphicsProfile.HiDef;
//if (ClientSettings.StartupInFullScreen)
//{
// ToggleFullScreen();
//}
}
/// <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);
// create sprite batch
m_spriteBatch = new SpriteBatch(m_graphics.GraphicsDevice);
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
Content.Unload();
}
/// <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)
{
ClientGameTime = gameTime;
if ((m_preferredWindowWidth != m_graphics.PreferredBackBufferWidth ||
m_preferredWindowHeight != m_graphics.PreferredBackBufferHeight) && !m_graphics.IsFullScreen)
{
m_graphics.PreferredBackBufferWidth = m_preferredWindowWidth;
m_graphics.PreferredBackBufferHeight = m_preferredWindowHeight;
m_graphics.ApplyChanges();
}
//if (GameState != Enums.eGameState.Splash && m_isFullScreen != UserSettings.FullScreen)
// m_isFullScreen = UserSettings.FullScreen;
//if (m_isFullScreen != m_graphics.IsFullScreen)
// ToggleFullScreen();
// allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
Exit();
Window.Title = m_title;
m_height = Window.ClientBounds.Height;
m_width = Window.ClientBounds.Width;
base.Update(gameTime);
if (m_roundDelayStart != null && m_roundDelayStart.TotalMilliseconds - gameTime.TotalGameTime.TotalMilliseconds
>= StaticSettings.RoundDelayLength)
RoundDelay = false;
if (RoundDelay)
m_roundDelayStart = gameTime.TotalGameTime;
}
/// <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)
{
m_graphics.GraphicsDevice.Clear(m_deviceClearColor);
if (m_noDraw)
return;
//m_graphics.GraphicsProfile = GraphicsProfile.Reach;
m_guiManager.Draw(gameTime);
//m_graphics.GraphicsProfile = GraphicsProfile.HiDef;
// draw effects like bloom
base.Draw(gameTime);
}
protected override void OnExiting(object sender, EventArgs args)
{
base.OnExiting(sender, args);
// remove this code and players who close the client without properly quitting the game are moved to karmaRes location
if (IO.IsAlive && InGame)
{
IO.Send(Protocol.GAME_EXIT);
}
Microsoft.Xna.Framework.Media.MediaPlayer.Stop();
//Microsoft.Xna.Framework.Media.MediaPlayer.Queue.ActiveSong.Dispose();
// save client settings
ClientSettings.Save();
// save user settings
if (Account.Name.Length > 0)
UserSettings.Save();
// save character settings
if (Character.CurrentCharacter != null &&
!string.IsNullOrEmpty(Character.CurrentCharacter.Name))
{
Character.Settings.Save();
Character.FogOfWarSettings.Save();
if (Character.GUIPositionSettings != null)
Character.GUIPositionSettings.Save();
}
// register disconnect event
Events.RegisterEvent(Events.EventName.Disconnect);
UnloadContent();
Exit();
}
public void ToggleFullScreen()
{
if(IsFullScreen && GameState == Enums.EGameState.HotButtonEditMode)
{
TextCue.AddClientInfoTextCue("Please exit Hot Button Edit Mode before switching from full screen.",
TextCue.TextCueTag.None, Color.Red, Color.Black, 255, 2000, false, false, true);
return;
}
PresentationParameters presentation = m_graphics.GraphicsDevice.PresentationParameters;
presentation.HardwareModeSwitch = true;
m_prevClientBounds = Window.ClientBounds;
if (presentation.IsFullScreen)
{
m_graphics.PreferredBackBufferWidth = m_preferredWindowWidth;
m_graphics.PreferredBackBufferHeight = m_preferredWindowHeight;
m_isFullScreen = false;
//try { Window.IsBorderless = true; }
//catch (NotImplementedException) { }
}
else
{
// going fullscreen, use desktop resolution to minimize display mode changes
// this also has the nice effect of working around some displays that lie about
// supporting 1280x720
GraphicsAdapter adapter = m_graphics.GraphicsDevice.Adapter;//CreationParameters.Adapter;
m_graphics.PreferredBackBufferWidth = adapter.CurrentDisplayMode.Width;
m_graphics.PreferredBackBufferHeight = adapter.CurrentDisplayMode.Height;
m_prevClientPosition = Window.Position;
m_isFullScreen = true;
//try { Window.IsBorderless = false; }
//catch (NotImplementedException) { }
}
m_graphics.ToggleFullScreen();
if(!m_isFullScreen && m_prevClientPosition != null) // not working properly
Window.Position = m_prevClientPosition;
UserSettings.FullScreen = m_graphics.IsFullScreen;
m_nowClientBounds = Window.ClientBounds;
OnClientResize();
// to correct issue with top 20 pixels inaccessible after first full screen toggle
if (m_graphics.IsFullScreen && m_firstFullScreen)
{
m_noDraw = true;
m_firstFullScreen = false;
ToggleFullScreen();
ToggleFullScreen();
m_noDraw = false;
}
}
public static void OnClientResize()
{
foreach (Sheet sheet in GuiManager.Sheets.Values)
sheet.OnClientResize(m_prevClientBounds, m_nowClientBounds);
// resize generic sheet
GuiManager.GenericSheet.OnClientResize(m_prevClientBounds, m_nowClientBounds);
foreach (TextCue tc in GuiManager.TextCues)
tc.OnClientResize(m_prevClientBounds, m_nowClientBounds);
}
}
}