-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cs
495 lines (442 loc) · 12.8 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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Kafe
{
public class Kafe : Game
{
public string[] Args { get; set; }
private RenderTarget2D rawScreen;
GraphicsDeviceManager graphics;
public static SpriteBatch SpriteBatch;
public static Effect ClutEffect;
public static Effect TransitionEffect;
public static Effect RetrowaveEffect;
private static Texture2D transitionImage;
private static TildeConsole tildeConsole;
public static GraphicsDevice GfxDev { get; private set; }
public static Kafe Me { get; private set; }
public static Input Input { get; private set; }
public static Character[] Characters { get; private set; }
public const int ScreenWidth = 480, ScreenHeight = 270;
public const int Scale = 2;
public const int CrtWidth = ScreenWidth * Scale, CrtHeight = ScreenHeight * Scale;
public const int Speed = 10;
public static int Ground = 240, LeftStart = 100, RightStart = 100;
public static Vector2 Camera;
public Kafe() : base()
{
Kafe.Me = this;
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = CrtWidth;
graphics.PreferredBackBufferHeight = CrtHeight;
if (CrtWidth == GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width && CrtHeight == GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height)
{
Window.IsBorderless = true;
Window.Position = new Point(0, 0);
}
Console.Prepare();
Mix.Initialize();
IsMouseVisible = true;
}
public static Effect GetEffect(string assetName)
{
if (!assetName.Contains("."))
assetName += ".fxb";
Console.WriteLine("GetEffect: fetching \"{0}\"...", assetName);
return new Effect(GfxDev, Mix.GetBytes(assetName));
}
protected override void Initialize()
{
Kafe.GfxDev = graphics.GraphicsDevice;
SoundEngine.Initialize();
tildeConsole = new TildeConsole(this);
tildeConsole.Visible = false;
tildeConsole.OnCommand += new TildeConsole.OnCommandEventHandler(TildeConsole_OnCommand);
Components.Add(tildeConsole);
Input = new global::Kafe.Input(this);
Components.Add(Input);
base.Initialize();
Console.WriteLine("Game initialized.");
}
protected override void LoadContent()
{
Console.WriteLine("Loading game content...");
SpriteBatch = new SpriteBatch(GraphicsDevice);
rawScreen = new RenderTarget2D(GraphicsDevice, ScreenWidth, ScreenHeight);
ClutEffect = GetEffect("clut");
TransitionEffect = GetEffect("transition");
transitionImage = Mix.GetTexture("transition");
RetrowaveEffect = GetEffect("shaderwave");
Console.WriteLine("Enumerating characters and locations...");
var fighterFiles = Mix.GetFilesWithPattern("fighters\\*.char.json");
var fighters = new List<string>();
foreach (var f in fighterFiles)
{
fighters.Add(f.Substring(f.IndexOf('\\') + 1));
}
var arenaFiles = Mix.GetFilesWithPattern("locales\\*.bg.json");
var arenas = new List<string>();
foreach (var a in arenaFiles)
{
arenas.Add(a.Substring(a.IndexOf('\\') + 1));
}
if (Args.Length >= 3 && Args[0] == "/edit")
{
Console.WriteLine("|c3|Edit mode requested.");
LoadingScreen.Start(() =>
{
Components.Add(new Editor("locales\\" + Args[1] + ".bg.json", Args[2] + ".char.json"));
});
}
else if (Args.Length > 0 && Args[0] == "/quick")
{
Console.WriteLine("|c3|Quick battle requested.");
var names = new string[2];
var colors = new int[2];
var rand = new Random();
for (var i = 0; i < 2; i++)
names[i] = fighters[rand.Next(fighters.Count)];
var numNames = (Args.Length > 2) ? 2 : 1;
for (var i = 0; i < numNames; i++)
{
names[i] = Args[i + 1].ToLowerInvariant();
colors[i] = 1;
if (names[i].Contains(","))
{
colors[i] = int.Parse(names[i].Substring(names[i].IndexOf(',') + 1));
names[i] = names[i].Remove(names[i].IndexOf(','));
}
if (!names[i].EndsWith(".char.json"))
names[i] += ".char.json";
}
//Ensure mirror matches have distinct colors
if (names[0] == names[1] && colors[0] == colors[1])
colors[1]++;
var arena = "locales\\" + ((Args.Length > 3) ? Args[3] + ".bg.json" : arenas[rand.Next(arenas.Count)]);
LoadingScreen.Start(() =>
{
var left = new Character(names[0], colors[0]);
var right = new Character(names[1], colors[1]);
Components.Add(new Arena(arena, left, right));
});
}
else
{
Console.WriteLine("|c3|Regular gameplay requested.");
LoadingScreen.Start(() =>
{
var fighterList = new List<Character>();
foreach (var f in fighters)
fighterList.Add(new Character(f, 0));
Kafe.Characters = fighterList.ToArray();
Components.Add(new TitleBackground());
Components.Add(new TitleScreen(false));
});
}
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (tildeConsole.Visible)
{
tildeConsole.Update(gameTime);
Input.Update(gameTime);
return;
}
if (TransitionDelta > -1)
{
TransitionDelta += 0.05f;
if (TransitionDelta >= 1.0f)
{
TransitionDelta = -1;
if (OnTransitionFinish != null)
OnTransitionFinish();
}
base.Update(gameTime);
return;
}
base.Update(gameTime);
if (Input.WasJustReleased(Keys.OemTilde))
{
tildeConsole.Visible = true;
Input.Flush();
}
if (Input.WasJustPressed(Keys.F12))
TakeScreenshot();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.SetRenderTarget(rawScreen);
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
if (TransitionDelta > -1)
{
TransitionEffect.Parameters["TransitionDelta"].SetValue(transitionIn ? TransitionDelta : 1 - TransitionDelta);
SpriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp, null, null, TransitionEffect);
SpriteBatch.Draw(transitionImage, new Rectangle(0, 0, ScreenWidth, ScreenHeight), Color.White);
SpriteBatch.End();
}
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
SpriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp, null, null, null);
SpriteBatch.Draw(rawScreen, new Rectangle(0, 0, CrtWidth, CrtHeight), new Rectangle(0, 0, ScreenWidth, ScreenHeight), Color.White);
SpriteBatch.End();
if (tildeConsole.Visible)
tildeConsole.Draw(gameTime);
}
public static float TransitionDelta = -1;
private static bool transitionIn;
public static Action OnTransitionFinish;
public static void DoTransition(bool goIn, Action onFinish)
{
TransitionDelta = 0;
transitionIn = goIn;
OnTransitionFinish = onFinish;
}
public static void PauseAll(bool enabled, GameComponent but)
{
foreach (var component in Kafe.Me.Components)
{
if (but != null && component == but)
continue;
if (component == Kafe.Input)
continue;
if (component is TitleBackground)
continue;
((GameComponent)component).Enabled = enabled;
}
}
public static void AskToQuit()
{
ConfirmScreen.Ask("Are you sure you want to exit?", () => { Kafe.Me.Exit(); }, () => { Input.Flush(); });
}
public void TakeScreenshot()
{
var newThread = new System.Threading.Thread(ScreenshotThread);
newThread.Start();
}
private void ScreenshotThread()
{
using (var stream = System.IO.File.OpenWrite(DateTime.Now.Ticks.ToString() + ".png"))
rawScreen.SaveAsPng(stream, Kafe.ScreenWidth, Kafe.ScreenHeight);
}
private void TildeConsole_OnCommand(object sender, ConsoleEventArgs e)
{
var command = e.Command.Trim();
if (string.IsNullOrWhiteSpace(command))
return;
var bits = command.ToLowerInvariant().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var toBool = new Func<int, bool>(i =>
{
return (bits[i] == "on" || bits[i] == "true" || bits[i] == "1" || bits[i] == "y");
});
var toInt = new Func<int, int>(i =>
{
return int.Parse(bits[i]);
});
var findCharacters = new Func<int, Character[]>(i =>
{
foreach (var component in Components)
{
if (component is Background)
{
return ((Background)component).Characters;
}
}
return null;
});
try
{
switch (bits[0])
{
case "quit":
this.Exit();
return;
case "screenshot":
Console.WriteLine("Screenshot taken.");
TakeScreenshot();
return;
case "showboxes":
var characters = findCharacters(0);
if (characters == null)
Console.WriteLine("No characters here right now.");
else if (bits.Length == 1)
{
var shown = new List<bool>();
foreach(var c in characters)
shown.Add(c.ShowBoxes);
Console.WriteLine(string.Join(", ", shown));
}
else if (bits.Length > 1)
{
var charIndex = toInt(1);
if (charIndex >= characters.Length)
Console.WriteLine("Character index out of range.");
else
{
if (bits.Length == 2)
Console.WriteLine(characters[charIndex].ShowBoxes.ToString());
else
characters[charIndex].ShowBoxes = toBool(2);
}
}
return;
}
}
catch (Exception ex)
{
Console.WriteLine("|c4|{0}|c0|: {1}", ex.ToString(), ex.Message);
}
}
}
public class LoadingScreen : DrawableGameComponent
{
private static LoadingScreen me;
private Action task;
private bool beenDrawn;
public LoadingScreen(Game game) : base(game) { }
public static void Start(Action task)
{
if (me == null)
me = new LoadingScreen(Kafe.Me);
me.task = task;
me.beenDrawn = false;
Kafe.Me.Components.Add(me);
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
if (task == null)
{
Kafe.Me.Components.Remove(this);
return;
}
if (beenDrawn)
{
task();
task = null;
Kafe.Me.Components.Remove(this);
}
}
public override void Draw(GameTime gameTime)
{
Kafe.GfxDev.Clear(Color.Black);
Kafe.SpriteBatch.Begin();
Text.Draw(Kafe.SpriteBatch, 1, "Loading...", 8, Kafe.ScreenHeight - 24);
Kafe.SpriteBatch.End();
beenDrawn = true;
}
}
public class ConfirmScreen : DrawableGameComponent
{
private static ConfirmScreen me;
private string prompt;
private Action onYes, onNo;
private Texture2D bits;
private int animTime;
private bool onRight;
public ConfirmScreen(Game game) : base(game) { }
protected override void LoadContent()
{
bits = Mix.GetTexture("confirm.png");
base.LoadContent();
}
public static void Ask(string prompt, Action onYes, Action onNo)
{
if (me == null)
me = new ConfirmScreen(Kafe.Me);
me.prompt = prompt;
me.onYes = onYes;
me.onNo = onNo;
me.animTime = 0;
me.onRight = false;
Kafe.PauseAll(false, me);
Kafe.Me.Components.Add(me);
}
public static void Ask(string prompt, Action onYes)
{
Ask(prompt, onYes, () => { return; });
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
if (animTime < 8)
{
animTime++;
return;
}
var control = Input.Controls[0];
if (Input.WasJustReleased(Keys.Y))
{
Kafe.PauseAll(true, null);
Kafe.Me.Components.Remove(this);
onYes();
}
else if (Input.WasJustReleased(Keys.N) || Input.WasJustReleased(Keys.Escape))
{
Kafe.PauseAll(true, null);
Kafe.Me.Components.Remove(this);
onNo();
}
else if (Input.WasJustReleased(Keys.Enter))
{
Kafe.PauseAll(true, null);
Kafe.Me.Components.Remove(this);
if (!onRight)
onYes();
else
onNo();
}
else if (control.TrgLeft || control.TrgRight)
onRight = !onRight;
}
public override void Draw(GameTime gameTime)
{
var batch = Kafe.SpriteBatch;
batch.Begin();
var height = animTime * 4;
var top = (Kafe.ScreenHeight / 2) - (height / 2);
var bottom = top + height;
var width = Kafe.ScreenWidth;
var buttonDelta = 32;
var yesPos = width - 16 - (buttonDelta * 2);
var src = new Rectangle(1, 1, 15, 13);
var dst = new Rectangle(0, top - 6, width, 13);
batch.Draw(bits, dst, src, Color.White);
dst.Offset(0, height);
batch.Draw(bits, dst, src, Color.White);
src = new Rectangle(17, 17, 14, 14);
dst = new Rectangle(0, top, width, height);
batch.Draw(bits, dst, src, Color.White);
if (animTime >= 8)
{
Text.Draw(batch, 0, prompt, 32, top + 8);
src = new Rectangle(17, 1, 14, 14);
dst = new Rectangle(yesPos + (onRight ? buttonDelta : 0), top + 12, 32, 16);
batch.Draw(bits, dst, src, Color.White);
Text.Draw(batch, 0, "YES", yesPos + 4, top + 16);
Text.Draw(batch, 0, " NO", yesPos + 4 + buttonDelta, top + 16);
}
batch.End();
}
}
#if WINDOWS || LINUX
public static class Program
{
[STAThread]
static void Main(string[] args)
{
using (var game = new Kafe())
{
game.Args = args;
game.Run();
}
}
}
#endif
}