Particle.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using Client.MirObjects;
  2. using Client.MirScenes;
  3. using SlimDX;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace Client.MirGraphics.Particles
  11. {
  12. public class ParticleImageInfo
  13. {
  14. public MLibrary Library;
  15. public Size Size = Size.Empty;
  16. public int Count = 0;
  17. public TimeSpan DrawFrameMS = TimeSpan.FromMilliseconds(50);
  18. public int BaseIndex, Duration;
  19. public long Start;
  20. public int CurrentFrame;
  21. public long NextFrame;
  22. public long CurrentDelay;
  23. public long Delay;
  24. public ParticleImageInfo(MLibrary file, int index, int count = 1, int drawMS = 50)
  25. {
  26. BaseIndex = index;
  27. Library = file;
  28. Size = Library.GetSize(index);
  29. Count = count;
  30. DrawFrameMS = TimeSpan.FromMilliseconds(50);
  31. // Delay = drawMS;
  32. Start = CMain.Time;
  33. NextFrame = Start + (Duration / Count) * (CurrentFrame + 1);
  34. Duration = drawMS * count;
  35. }
  36. }
  37. public class Particle
  38. {
  39. public ParticleImageInfo ImageInfo { get; set; }
  40. public ParticleEngine Engine { get; set; }
  41. public BlendMode BlendMode = BlendMode.NORMAL;
  42. public Vector2 OldPosition = Vector2.Zero;
  43. public Vector2 Position
  44. {
  45. get
  46. { return _position; }
  47. set
  48. {
  49. if (_position == value) return;
  50. OldPosition = _position;
  51. _position = value;
  52. OnPositionChanged();
  53. }
  54. }
  55. private Vector2 _position { get; set; }
  56. public int DrawFrame;
  57. public Vector2 OldVelocity = Vector2.Zero;
  58. public Vector2 _velocity { get; set; }
  59. public Vector2 Velocity
  60. {
  61. get { return _velocity; }
  62. set
  63. {
  64. if (OldVelocity == Vector2.Zero)
  65. {
  66. OldVelocity = value;
  67. }
  68. _velocity = value;
  69. }
  70. }
  71. public Color Color { get; set; }
  72. public float Size { get; set; }
  73. public DateTime AliveTime { get; set; }
  74. public bool Blend { get; set; }
  75. public float BlendRate { get; set; }
  76. public TimeSpan UpdateDelay = TimeSpan.FromMilliseconds(50);
  77. protected DateTime NextUpdateTime { get; set; }
  78. protected DateTime NextDraw { get; set; }
  79. public virtual void Update()
  80. {
  81. if (CMain.Now < NextUpdateTime) return;
  82. NextUpdateTime = CMain.Now + UpdateDelay;
  83. Position += Velocity;
  84. }
  85. public void Draw()
  86. {
  87. if (ImageInfo == null)
  88. {
  89. return;
  90. }
  91. int drawx = (int)Position.X;
  92. int drawy = (int)Position.Y;
  93. if (Blend)
  94. ImageInfo.Library.DrawBlend(ImageInfo.BaseIndex + ImageInfo.CurrentFrame, new Point(drawx, drawy), Color, true, BlendRate);
  95. else
  96. ImageInfo.Library.Draw(ImageInfo.BaseIndex + ImageInfo.CurrentFrame, new Point(drawx, drawy), Color, true, BlendRate);
  97. }
  98. public void ProcessImage()
  99. {
  100. if (CMain.Time <= ImageInfo.NextFrame) return;
  101. if (++ImageInfo.CurrentFrame >= ImageInfo.Count)
  102. {
  103. ImageInfo.CurrentFrame = 0;
  104. ImageInfo.Start = CMain.Time + ImageInfo.Delay;
  105. ImageInfo.NextFrame = ImageInfo.Start + (ImageInfo.Duration / ImageInfo.Count) * (ImageInfo.CurrentFrame + 1);
  106. }
  107. else ImageInfo.NextFrame = ImageInfo.Start + (ImageInfo.Duration / ImageInfo.Count) * (ImageInfo.CurrentFrame + 1);
  108. GameScene.Scene.MapControl.TextureValid = false;
  109. }
  110. protected virtual void OnPositionChanged()
  111. {
  112. try
  113. {
  114. if (ImageInfo.Size.Height == 0 || ImageInfo.Size.Width == 0)
  115. return;
  116. int xwidth = (int)(ImageInfo.Size.Width * (Math.Ceiling(Settings.ScreenWidth / (decimal)ImageInfo.Size.Width) + 2));
  117. int ywidth = (int)(ImageInfo.Size.Height * (Math.Ceiling(Settings.ScreenHeight / (decimal)ImageInfo.Size.Height) + 2));
  118. Vector2 xreset = new Vector2(xwidth, 0);
  119. Vector2 yreset = new Vector2(0, ywidth);
  120. if (Position.Y < -ImageInfo.Size.Height * 2)
  121. Position += yreset;
  122. else if (Position.Y > Settings.ScreenHeight + ImageInfo.Size.Height)
  123. Position -= yreset;
  124. else if (Position.X < -ImageInfo.Size.Width * 2)
  125. Position += xreset;
  126. else if (Position.X > Settings.ScreenWidth + ImageInfo.Size.Width)
  127. Position -= xreset;
  128. }
  129. catch (Exception e)
  130. {
  131. throw e;
  132. }
  133. }
  134. public virtual void OnParticleEnd()
  135. {
  136. }
  137. }
  138. }