-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathOldDrawable.cs
697 lines (607 loc) · 15.1 KB
/
OldDrawable.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace monopulse.GameEngine.Entities
{
public class Animation : ICloneable
{
#region fields
// first frame of animation
private Rectangle initialFrame;
// number of frames in the animation
private int frameCount = 1;
// current frame
private int currentFrame = 0;
// amount of time, in seconds, to display each frame
private float frameLength = 0.5f;
// amount of time that has passed since the last frame change
private float frameTimer = 0.0f;
// how many times this animation has been played
private int playedCount = 0;
// texture string for the animation to be played next
private string nextAnimation = null;
#endregion
#region getters and setters
/// <summary>
/// Number of frames in the animation
/// </summary>
public int FrameCount
{
get
{
return frameCount;
}
set
{
frameCount = value;
}
}
/// <summary>
/// The time, in seconds, each frame lasts
/// </summary>
public float FrameLength
{
get
{
return frameLength;
}
set
{
frameLength = value;
}
}
/// <summary>
/// Width of the frame
/// </summary>
public int FrameWidth
{
get
{
return initialFrame.Width;
}
}
/// <summary>
/// Height of the frame
/// </summary>
public int FrameHeight
{
get
{
return initialFrame.Height;
}
}
/// <summary>
/// Current frame being drawn
/// </summary>
public int CurrentFrame
{
get
{
return currentFrame;
}
set
{
// restricts the current frame to be between 0 and framecount -1
// don't want negative frames, now do we?
currentFrame = (int)MathHelper.Clamp(value, 0, frameCount - 1);
}
}
/// <summary>
/// Rectangle of the animation frame
/// </summary>
public Rectangle Frame
{
get
{
return new Rectangle
(
initialFrame.X + (initialFrame.Width * currentFrame),
initialFrame.Y,
initialFrame.Width,
initialFrame.Height
);
}
}
/// <summary>
/// How many times this animation has been played
/// </summary>
public int PlayedCount
{
get
{
return playedCount;
}
set
{
playedCount = value;
}
}
/// <summary>
/// Next texture string in sequence (what frame will be played next)
/// </summary>
public string NextAnimation
{
get
{
return nextAnimation;
}
set
{
nextAnimation = value;
}
}
#endregion
#region constructors
/// <summary>
/// when you know the initial rect and the number of frames
/// </summary>
/// <param name="first"></param>
/// <param name="numFrames"></param>
public Animation(Rectangle first, int numFrames)
{
initialFrame = first;
frameCount = numFrames;
}
/// <summary>
/// when you want to create the initial rect, and you know the number of frames
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="numFrames"></param>
public Animation(int x, int y, int width, int height, int numFrames)
{
initialFrame = new Rectangle(x, y, width, height);
frameCount = numFrames;
}
/// <summary>
/// when you want to create the initial rect, you know the number of frames, and the length of the frames
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="numFrames"></param>
/// <param name="length"></param>
public Animation(int x, int y, int width, int height, int numFrames, float length)
{
initialFrame = new Rectangle(x, y, width, height);
frameCount = numFrames;
frameLength = length;
}
/// <summary>
/// when you want to create the initial rect, you know the number of frames, the length of the frames, and the next texture to use
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="numFrames"></param>
/// <param name="length"></param>
/// <param name="next"></param>
public Animation(int x, int y, int width, int height, int numFrames, float length, string next)
{
initialFrame = new Rectangle(x, y, width, height);
frameCount = numFrames;
frameLength = length;
nextAnimation = next;
}
#endregion
#region methods
/// <summary>
/// update
/// </summary>
/// <param name="gameTime"></param>
public void Update(GameTime gameTime)
{
// set the frame timer to elapsed time
frameTimer = frameTimer + (float)gameTime.ElapsedGameTime.TotalSeconds;
// if the frametime is greater than the frame time length, update that sucker!
if (frameTimer > frameLength)
{
// reset frametimer
frameTimer = 0.0f;
// set current frame to the next frame; if we're looping then mod it with frame count
currentFrame = (currentFrame + 1) % frameCount;
// if current frame equals 0, we looped; so, increment played count
if (currentFrame == 0)
{
playedCount = playedCount + 1;
}
}
}
/// <summary>
/// clones the animation
/// </summary>
/// <returns></returns>
object ICloneable.Clone()
{
return new Animation
(
this.initialFrame.X,
this.initialFrame.Y,
this.initialFrame.Width,
this.initialFrame.Height,
this.frameCount,
this.frameLength,
nextAnimation
);
}
#endregion
}
public class Drawable : IDisposable
{
#region fields
Texture2D currentTexture;
bool animating = true;
Color tint = Color.White;
Vector2 position = new Vector2(0, 0);
Vector2 lastPosition = new Vector2(0, 0);
Dictionary<string, Animation> animations = new Dictionary<string, Animation>();
string currentAnimation = null;
Vector2 center;
int width;
int height;
Vector2 drawOffset;
float drawDepth;
bool active = true; // MAY REMOVE THIS
bool visible = true;
#endregion
#region getters/setters
/// <summary>
/// offset, pair x,y from the entity's position from where the drawable will be drawn
/// </summary>
public Vector2 DrawOffset
{
get
{
return drawOffset;
}
set
{
drawOffset = value;
}
}
/// <summary>
/// depth at which the drawable is drawn
/// 0:
/// 1:
/// </summary>
public float DrawDepth
{
get
{
return drawDepth;
}
set
{
drawDepth = value;
}
}
/// <summary>
/// position of the upper left corner (in pixels)
/// </summary>
public Vector2 Position
{
get
{
return position;
}
set
{
lastPosition = position;
position = value;
}
}
/// <summary>
/// last position of the drawable
/// </summary>
public Vector2 LastPosition
{
get
{
return lastPosition;
}
set
{
lastPosition = value;
}
}
/// <summary>
/// width of the sprite frame (in pixels)
/// </summary>
public int Width
{
get
{
return width;
}
}
/// <summary>
/// height of the sprite frame (in pixels)
/// </summary>
public int Height
{
get
{
return height;
}
}
/// <summary>
/// coordinates, on the screen, of the bounding box of the sprite
/// </summary>
public Rectangle boundBox
{
get
{
return new Rectangle((int)position.X, (int)position.Y, width, height);
}
}
/// <summary>
/// the texture of the sprite
/// </summary>
public Texture2D texture
{
get
{
return currentTexture;
}
}
/// <summary>
/// color to tint the sprite with when drawing
/// </summary>
public Color Tint
{
get
{
return tint;
}
set
{
tint = value;
}
}
/// <summary>
/// returns true of the sprite is, or should be, animating
/// if false, this sprite will not be drawn
/// </summary>
public bool isAnimating
{
get
{
return animating;
}
set
{
animating = value;
}
}
/// <summary>
/// current frame of the animation
/// </summary>
public Animation CurrentFrame
{
get
{
if (string.IsNullOrEmpty(currentAnimation) == false)
{
return animations[currentAnimation];
}
else
{
return null;
}
}
}
/// <summary>
/// name of the currently playing animation.
/// setting the animation (giving it a new string) resets currentFrame and playedCount
/// NOTE: when this class is edited to account for jumping animations, or idle animations,
/// or sword swings or whatever, we will NEED to change this, and be able to start/stop animations
/// wherever we wish, or implement multiple spritesheets for one sprite (have a different set of animations
/// on each sheet)
/// </summary>
public string CurrentAnimation
{
get
{
return currentAnimation;
}
set
{
if (animations.ContainsKey(value))
{
currentAnimation = value;
animations[currentAnimation].CurrentFrame = 0;
animations[currentAnimation].PlayedCount = 0;
}
}
}
public bool IsVisible
{
get
{
return visible;
}
set
{
visible = value;
}
}
public bool IsActive
{
get
{
return active;
}
set
{
active = value;
}
}
#endregion
#region constructors / destructors
/// <summary>
/// construct the animation, giving it a texture
/// </summary>
/// <param name="texture"></param>
public Drawable(Texture2D texture)
{
currentTexture = texture;
drawOffset = Vector2.Zero;
drawDepth = 0.0f;
}
#endregion
#region methods
/// <summary>
/// may need to force collection on a drawable, depending on the texture / animation
/// </summary>
public void Dispose()
{
GC.SuppressFinalize(this);
}
/// <summary>
/// usually use this to add an animation
/// </summary>
/// <param name="name"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="w"></param>
/// <param name="h"></param>
/// <param name="frames"></param>
/// <param name="frameLength"></param>
public void AddAnimation(string name, int x, int y, int w, int h, int frames, float frameLength)
{
animations.Add(name, new Animation(x, y, w, h, frames, frameLength));
width = w;
height = h;
center = new Vector2((int)(width / 2), (int)(height / 2));
}
/// <summary>
/// use this if you know the next frame in animation
/// </summary>
/// <param name="name"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="w"></param>
/// <param name="h"></param>
/// <param name="frames"></param>
/// <param name="frameLength"></param>
/// <param name="nextAnimation"></param>
public void AddAnimation(string name, int x, int y, int w, int h,
int frames, float frameLength, string nextAnimation)
{
animations.Add(name, new Animation(x, y, w, h, frames, frameLength, nextAnimation));
width = w;
height = h;
center = new Vector2((int)(width / 2), (int)(height / 2));
}
/// <summary>
/// gets the name of the current animation
/// if there is none, it doesnt exist in the dictionary of animations, and returns null string
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public Animation GetAnimation(string name)
{
if (animations.ContainsKey(name))
{
return animations[name];
}
else
{
return null;
}
}
/// <summary>
/// move this bro, passing in a delta x and delta y
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public void MoveBy(int x, int y)
{
lastPosition = position;
position.X = position.X + x;
position.Y = position.Y + y;
}
/// <summary>
/// move this bro to a new position x and y
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public void MoveTo(int x, int y)
{
lastPosition = position;
position.X = x;
position.Y = y;
}
/// <summary>
/// update this
/// </summary>
/// <param name="gameTime"></param>
public void Update(GameTime gameTime)
{
// dont do anything if its not animating
if (animating == true)
{
// if there is not an active animation
if (CurrentFrame == null)
{
// make sure there's an animation
if (animations.Count > 0)
{
// set the active animation to the first animation
string[] keys = new string[animations.Count];
animations.Keys.CopyTo(keys, 0);
currentAnimation = keys[0];
}
else
{
return;
}
}
// run the animation's update
CurrentFrame.Update(gameTime);
// check to see if there is an animation following this one
if (String.IsNullOrEmpty(CurrentFrame.NextAnimation) == false)
{
// if there is a next animation
// check if the animation has completed a full cycle
if (CurrentFrame.PlayedCount > 0)
{
currentAnimation = CurrentFrame.NextAnimation;
}
}
}
}
/// <summary>
/// draw this
/// </summary>
/// <param name="spriteBatch"></param>
/// <param name="xOffset"></param>
/// <param name="yOffset"></param>
public void Draw(SpriteBatch spriteBatch, int xOffset, int yOffset)
{
if (visible == true) // animating == true
{
spriteBatch.Draw
(
currentTexture,
position + new Vector2(xOffset, yOffset) + center + DrawOffset,
CurrentFrame.Frame,
tint,
0.0f,
center,
1f,
SpriteEffects.None,
DrawDepth
);
}
}
#endregion
}
}