PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/Ultima/World/Entities/Effects/MovingEffect.cs

https://gitlab.com/swak/UltimaXNA
C# | 190 lines | 156 code | 19 blank | 15 comment | 25 complexity | f8cdbd79045968c4681b486d1120c5d8 MD5 | raw file
  1. /***************************************************************************
  2. * MovingEffect.cs
  3. * Copyright (c) 2015 UltimaXNA Development Team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. ***************************************************************************/
  11. #region usings
  12. using System;
  13. using Microsoft.Xna.Framework;
  14. using UltimaXNA.Ultima.World.Entities.Items;
  15. using UltimaXNA.Ultima.World.Entities.Mobiles;
  16. using UltimaXNA.Ultima.World.EntityViews;
  17. using UltimaXNA.Ultima.World.Maps;
  18. #endregion
  19. namespace UltimaXNA.Ultima.World.Entities.Effects
  20. {
  21. public class MovingEffect : AEffect
  22. {
  23. public float AngleToTarget = 0f;
  24. int m_ItemID;
  25. public int ItemID
  26. {
  27. get { return m_ItemID; }
  28. }
  29. public MovingEffect(Map map,int itemID, int hue)
  30. : base(map)
  31. {
  32. Hue = hue;
  33. itemID &= 0x3fff;
  34. m_ItemID = itemID | 0x4000;
  35. }
  36. #region Constructors
  37. public MovingEffect(Map map,AEntity Source, AEntity Target, int itemID, int hue)
  38. : this(map, itemID, hue)
  39. {
  40. base.SetSource(Source);
  41. base.SetTarget(Target);
  42. }
  43. public MovingEffect(Map map,AEntity Source, int xTarget, int yTarget, int zTarget, int itemID, int hue)
  44. : this(map, itemID, hue)
  45. {
  46. base.SetSource(Source);
  47. base.SetTarget(xTarget, yTarget, zTarget);
  48. }
  49. public MovingEffect(Map map,int xSource, int ySource, int zSource, AEntity Target, int itemID, int hue)
  50. : this(map, itemID, hue)
  51. {
  52. base.SetSource(xSource, ySource, zSource);
  53. base.SetTarget(Target);
  54. }
  55. public MovingEffect(Map map,int xSource, int ySource, int zSource, int xTarget, int yTarget, int zTarget, int itemID, int hue)
  56. : this(map, itemID, hue)
  57. {
  58. base.SetSource(xSource, ySource, zSource);
  59. base.SetTarget(xTarget, yTarget, zTarget);
  60. }
  61. public MovingEffect(Map map,int sourceSerial, int targetSerial, int xSource, int ySource, int zSource, int xTarget, int yTarget, int zTarget, int itemID, int hue)
  62. : this(map, itemID, hue)
  63. {
  64. sbyte zSrcByte = (sbyte)zSource;
  65. sbyte zTarByte = (sbyte)zTarget;
  66. AEntity source = WorldModel.Entities.GetObject<AEntity>(sourceSerial, false);
  67. if (source != null)
  68. {
  69. if (source is Mobile)
  70. {
  71. base.SetSource(source.X, source.Y, source.Z);
  72. Mobile mobile = source as Mobile;
  73. if ((!mobile.IsClientEntity && !mobile.IsMoving) && ((xSource | ySource | zSource) != 0))
  74. {
  75. source.Position.Set(xSource, ySource, zSrcByte);
  76. }
  77. }
  78. else if (source is Item)
  79. {
  80. base.SetSource(source.X, source.Y, source.Z);
  81. Item item = source as Item;
  82. if ((xSource | ySource | zSource) != 0)
  83. {
  84. item.Position.Set(xSource, ySource, zSrcByte);
  85. }
  86. }
  87. else
  88. {
  89. base.SetSource(xSource, ySource, zSrcByte);
  90. }
  91. }
  92. else
  93. {
  94. base.SetSource(xSource, ySource, zSource);
  95. }
  96. AEntity target = WorldModel.Entities.GetObject<AEntity>(targetSerial, false);
  97. if (target != null)
  98. {
  99. if (target is Mobile)
  100. {
  101. base.SetTarget(target);
  102. Mobile mobile = target as Mobile;
  103. if ((!mobile.IsClientEntity && !mobile.IsMoving) && ((xTarget | yTarget | zTarget) != 0))
  104. {
  105. mobile.Position.Set(xTarget, yTarget, zTarByte);
  106. }
  107. }
  108. else if (target is Item)
  109. {
  110. base.SetTarget(target);
  111. Item item = target as Item;
  112. if ((xTarget | yTarget | zTarget) != 0)
  113. {
  114. item.Position.Set(xTarget, yTarget, zTarByte);
  115. }
  116. }
  117. else
  118. {
  119. base.SetSource(xTarget, yTarget, zTarByte);
  120. }
  121. }
  122. }
  123. #endregion
  124. public override void Update(double frameMS)
  125. {
  126. base.Update(frameMS);
  127. int sx, sy, sz, tx, ty, tz;
  128. GetSource(out sx, out sy, out sz);
  129. GetTarget(out tx, out ty, out tz);
  130. if (m_TimeUntilHit == 0f)
  131. {
  132. m_TimeActive = 0f;
  133. m_TimeUntilHit = (float)Math.Sqrt(Math.Pow((tx - sx), 2) + Math.Pow((ty - sy), 2) + Math.Pow((tz - sz), 2)) * 75f;
  134. }
  135. else
  136. {
  137. m_TimeActive += (float)frameMS;
  138. }
  139. if (m_TimeActive >= m_TimeUntilHit)
  140. {
  141. Dispose();
  142. return;
  143. }
  144. else
  145. {
  146. float x, y, z;
  147. x = (sx + (m_TimeActive / m_TimeUntilHit) * (float)(tx - sx));
  148. y = (sy + (m_TimeActive / m_TimeUntilHit) * (float)(ty - sy));
  149. z = (sz + (m_TimeActive / m_TimeUntilHit) * (float)(tz - sz));
  150. Position.Set((int)x, (int)y, (int)z);
  151. Position.Offset = new Vector3(x % 1, y % 1, z % 1);
  152. AngleToTarget = -((float)Math.Atan2((ty - sy), (tx - sx)) + (float)(Math.PI) * (1f / 4f)); // In radians
  153. }
  154. // m_RenderMode:
  155. // 2: Alpha = 1.0, Additive.
  156. // 3: Alpha = 1.5, Additive.
  157. // 4: Alpha = 0.5, AlphaBlend.
  158. // draw rotated.
  159. }
  160. private float m_TimeActive = 0f;
  161. private float m_TimeUntilHit = 0f;
  162. protected override AEntityView CreateView()
  163. {
  164. return new MovingEffectView(this);
  165. }
  166. public override string ToString()
  167. {
  168. return string.Format("MovingEffect");
  169. }
  170. }
  171. }