-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHud.cs
352 lines (321 loc) · 10.1 KB
/
Hud.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RPG
{
class Hud
{
private Texture2D textbox;
private string[] messages;
private Point[][] locations;
private int width;
private int height;
private int offsetX;
private int offsetY;
private int posX;
private int posY;
private double timer;
private int charCount;
private int curMessage;
public bool visible;
private bool canClose;
private Dictionary<char, int> charLengths;
private int[] lengthRef;
private int[][] indeces;
private int lineLength;
private int maxLines;
private int linesToShift;
private int linesShifted;
private int textWidth;
private bool wait;
private byte cursorBob;
private bool cursorDown, centered;
private double cursorTimer;
private byte textOffset;
private byte spacing;
private byte deathOffX;
private Color textColor;
public Hud(string[] message, ContentManager content, int width = 26, int height = 3, int posX = -1, int posY = -1, bool canClose = true, bool centered = false)
{
lengthRef = new int[] { 2, 2, 3, 2, 5, 9, 7, 2, 3, 3, 3, 5, 2, 2, 2, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 3, 3, 5, 3, 4, 5, 6, 5, 5, 5, 4, 4, 5, 5, 1, 4, 5, 4, 7, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 5, 5, 4, 5, 4, 6, 4, 5, 1, 4, 4, 4, 4, 4, 3, 4, 4, 1, 2, 4, 1, 7, 4, 4, 4, 4, 3, 4, 3, 4, 5, 7, 4, 4, 4, 2, 5, 2, 6, 7, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };
indeces = new int[message.Length][];
for(int i = 0; i < indeces.Length; i++)
indeces[i] = new int[message[i].Length];
lineLength = 0;
this.maxLines = height - 2;
this.centered = centered;
linesShifted = 0;
linesToShift = 0;
wait = false;
cursorBob = 1;
cursorDown = true;
this.canClose = canClose;
this.posX = posX;
this.posY = posY;
visible = true;
curMessage = 0;
charCount = 0;
timer = 0;
this.width = width;
//this.maxLines = height;
height += 2;
this.height = height;
textColor = Color.White;
if(height % 2 == 0)
{
textOffset = 9;
spacing = 15;
}
else
{
textOffset = 7;
spacing = 14;
}
if (posX == -1)
offsetX = (Game1.width - (width + 2) * 8) / 2;
else
offsetX = posX;
if (posY == -1)
offsetY = Game1.height - (height + 2) * 8 - 4;
else
offsetY = posY;
this.textbox = content.Load<Texture2D>("Textbox/Text");
this.messages = message;
//message = message.ToUpper();
locations = new Point[messages.Length][];//[message.Length];
for (int i = 0; i < locations.Length; i++)
locations[i] = new Point[messages[i].Length];
for(int i = 0; i < messages.Length; i++)
for(int j = 0; j < messages[i].Length; j++)
{
char letter = message[i][j];
int line, off;
if (letter != '\n')
{
//Determinesthe sprite's position based on its distance from the first character in the spritesheet, the space
line = (letter - ' ') / 16;
off = (letter - ' ') % 16;
locations[i][j] = new Point(16 * off, 16 * line);
if (j < messages[i].Length - 1)
{
indeces[i][j+1] = lengthRef[(int)(letter - ' ')] + 1;// + indeces[i][j-1] + 1;
//Keep the offset cumulative
if (j > 0)
indeces[i][j+1] += indeces[i][j];
}
}
}
//DeathMode(true);
if(messages[0].Length > 0)
textWidth = indeces[0][messages[0].Length - 1];
}
public void DeathMode(bool enabled)
{
if (enabled)
{
deathOffX = 32;
textColor = new Color(245, 139, 148);
}
else
{
deathOffX = 0;
textColor = Color.White;
}
}
public void finishMessage()
{
//curMessage = messages.Length - 1;
charCount = messages[curMessage].Length;
//visible = !canClose;
}
public void finishText()
{
curMessage = messages.Length - 1;
charCount = messages[curMessage].Length;
visible = !canClose;
}
public void Update(GameTime gameTime, KeyboardState prevStateKb, MouseState prevStateM)
{
if ((Keyboard.GetState().IsKeyDown(Keys.Space) && prevStateKb.IsKeyUp(Keys.Space)) ||
(prevStateM.LeftButton == ButtonState.Pressed && Mouse.GetState().LeftButton == ButtonState.Released))
{
if (wait)
{
wait = false;
if (linesShifted > maxLines)
linesToShift++;
linesShifted++;
cursorBob = 0;
cursorDown = true;
}
if (true)
{
if (isFinished())// && curMessage < messages.Length - 1)
{
if (curMessage < messages.Length - 1)
{
//advance message
curMessage++;
charCount = 0;
lineLength = 0;
linesToShift = 0;
linesShifted = 0;
}
else
{
//close textbox
visible = !canClose;
}
}
else
{
if (charCount < messages[curMessage].Length)
{
//skip text
//charCount = messages[curMessage].Length;
//CHANGE TO SKIP LINE
}
}
}
//spacePressedLastFrame = true;
}
else
{
//spacePressedLastFrame = false;
timer += gameTime.ElapsedGameTime.TotalSeconds;
cursorTimer += gameTime.ElapsedGameTime.TotalSeconds;
if (timer >= 0.016)
{
timer = 0;
int len = messages[curMessage].Length;
if (!wait && charCount < len)
{
if (messages[curMessage].ElementAt(charCount) == '\n')
{
if (charCount + 1 != len && messages[curMessage].ElementAt(charCount + 1) == '@')
{
wait = true;
}
else
{
if (linesShifted > maxLines)
linesToShift++;
linesShifted++;
}
}
charCount++;
}
}
if(cursorTimer > 0.25)
{
cursorTimer -= 0.25;
if (wait || isFinished())
{
if (cursorDown)
{
cursorBob++;
if (cursorBob > 3)
{
cursorBob--;
cursorDown = false;
}
}
else
{
cursorBob--;
if (cursorBob < 1)
{
cursorBob++;
cursorDown = true;
}
}
}
}
}
}
private void DrawBlank(SpriteBatch sb)
{
//UL corner
sb.Draw(textbox, new Rectangle(offsetX, offsetY, 8, 8), new Rectangle(0 + deathOffX, 112, 8, 8), Color.White);
//BL corner
sb.Draw(textbox, new Rectangle(offsetX, offsetY + (height + 1) * 8, 8, 8), new Rectangle(0 + deathOffX, 120, 8, 8), Color.White);
//UR corner
sb.Draw(textbox, new Rectangle(offsetX + (width+1)*8, offsetY, 8, 8), new Rectangle(8 + deathOffX, 112, 8, 8), Color.White);
//BR corner
sb.Draw(textbox, new Rectangle(offsetX + (width+1)*8, offsetY + (height + 1) * 8, 8, 8), new Rectangle(8 + deathOffX, 120, 8, 8), Color.White);
//top&bottom
//sb.Draw(textbox, new Rectangle(offsetX + (1) * 8, offsetY, width*8, 8), new Rectangle(16, 112, 1, 8), Color.White);
//sb.Draw(textbox, new Rectangle(offsetX + (1) * 8, offsetY + (height + 1) * 8, width*8, 8), new Rectangle(17, 112, 1, 8), Color.White);
for (int i = 0; i < width; i++)
{
sb.Draw(textbox, new Rectangle(offsetX + (i + 1) * 8, offsetY, 8, 8), new Rectangle(16 + deathOffX, 112, 8, 8), Color.White);//top
sb.Draw(textbox, new Rectangle(offsetX + (i + 1) * 8, offsetY + (height + 1) * 8, 8, 8), new Rectangle(16 + deathOffX, 120, 8, 8), Color.White);//bottom
}
//left&right
//sb.Draw(textbox, new Rectangle(offsetX, offsetY + (1) * 8, 8, 8*height), new Rectangle(20, 112, 8, 1), Color.White);//left
//sb.Draw(textbox, new Rectangle(offsetX + (width+1)*8, offsetY + (1) * 8, 8, 8*height), new Rectangle(26, 112, 8, 1), Color.White);//right
for (int i = 0; i < height; i++)
{
sb.Draw(textbox, new Rectangle(offsetX, offsetY + (i + 1) * 8, 8, 8), new Rectangle(24 + deathOffX, 112, 8, 8), Color.White);
sb.Draw(textbox, new Rectangle(offsetX + (width + 1) * 8, offsetY + (i + 1) * 8, 8, 8), new Rectangle(24 + deathOffX, 120, 8, 8), Color.White);
}
//fill inside with black
sb.Draw(textbox, new Rectangle(offsetX + 8, offsetY + 8, 8 * width, 8 * height), new Rectangle(7 + deathOffX, 119, 1, 1), Color.White);
//215, 40
//26 * 5
}
public bool isFinished()
{
return charCount == messages[curMessage].Length;
}
public void Draw(SpriteBatch sb)//16x96: 7x4
{
if (visible)
{
DrawBlank(sb);
char letter;
int lineCount = 0;
for(int i = 0; i < charCount; i++)
{
letter = messages[curMessage].ElementAt<char>(i);
if (letter == '\n')
{
lineCount++;
}
else if(lineCount >= linesToShift)
{
//Draw character
if(centered)
sb.Draw(textbox, new Rectangle(offsetX + 1 + ((width + 1)*4) - textWidth/2 + indeces[curMessage][i], offsetY + (lineCount - linesToShift) * spacing + textOffset, 16, 16), new Rectangle(locations[curMessage][i].X, locations[curMessage][i].Y, 16, 16), textColor);
else
sb.Draw(textbox, new Rectangle(offsetX + 8 + indeces[curMessage][i], offsetY + (lineCount - linesToShift) * spacing + textOffset, 16, 16), new Rectangle(locations[curMessage][i].X, locations[curMessage][i].Y, 16, 16), textColor);
}
}
//Draws cusor bobbing up and down
if(canClose && (wait || isFinished()))
{
sb.Draw(textbox, new Rectangle(offsetX + width*8, cursorBob + offsetY + height*8 + 1, 7, 4), new Rectangle(16, 96, 7, 4), textColor);
//Console.WriteLine("Cursor bob: " + cursorBob);
}
}
}
public bool messageComplete()
{
return !visible;
}
public int getHeight()
{
return height * 8;
}
public bool IsWaiting()
{
return wait;
}
}
}