Effect.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. using Client.MirGraphics;
  2. using Client.MirScenes;
  3. using Client.MirSounds;
  4. namespace Client.MirObjects
  5. {
  6. public class Effect
  7. {
  8. public MLibrary Library;
  9. public int BaseIndex, Count, Duration;
  10. public long Start;
  11. public int CurrentFrame;
  12. public long NextFrame;
  13. public Point Source;
  14. public MapObject Owner;
  15. public int Light = 6;
  16. public Color LightColour = Color.White;
  17. public bool Blend = true;
  18. public float Rate = 1F;
  19. public Point DrawLocation;
  20. public Point DrawOffset = Point.Empty;
  21. public bool Repeat;
  22. public long RepeatUntil;
  23. public bool DrawBehind = false;
  24. public long CurrentDelay;
  25. public long Delay;
  26. public event EventHandler Complete;
  27. public event EventHandler Played;
  28. public Effect(MLibrary library, int baseIndex, int count, int duration, MapObject owner, long starttime = 0, bool drawBehind = false)
  29. {
  30. Library = library;
  31. BaseIndex = baseIndex;
  32. Count = count == 0 ? 1 : count;
  33. Duration = duration;
  34. Start = starttime == 0 ? CMain.Time : starttime;
  35. NextFrame = Start + (Duration / Count) * (CurrentFrame + 1);
  36. Owner = owner;
  37. Source = Owner.CurrentLocation;
  38. DrawBehind = drawBehind;
  39. }
  40. public Effect(MLibrary library, int baseIndex, int count, int duration, Point source, long starttime = 0, bool drawBehind = false)
  41. {
  42. Library = library;
  43. BaseIndex = baseIndex;
  44. Count = count == 0 ? 1 : count;
  45. Duration = duration;
  46. Start = starttime == 0 ? CMain.Time : starttime;
  47. NextFrame = Start + (Duration / Count) * (CurrentFrame + 1);
  48. Source = source;
  49. DrawBehind = drawBehind;
  50. }
  51. public void SetStart(long start)
  52. {
  53. Start = start;
  54. NextFrame = Start + (Duration / Count) * (CurrentFrame + 1);
  55. }
  56. public virtual void Process()
  57. {
  58. if (CurrentFrame == 1)
  59. {
  60. if (Played != null)
  61. Played(this, EventArgs.Empty);
  62. }
  63. if (CMain.Time <= NextFrame) return;
  64. if (Owner != null && Owner.SkipFrames) CurrentFrame++;
  65. if (++CurrentFrame >= Count)
  66. {
  67. if (Repeat && (RepeatUntil == 0 || CMain.Time < RepeatUntil))
  68. {
  69. CurrentFrame = 0;
  70. Start = CMain.Time + Delay;
  71. NextFrame = Start + (Duration / Count) * (CurrentFrame + 1);
  72. }
  73. else
  74. Remove();
  75. }
  76. else NextFrame = Start + (Duration / Count) * (CurrentFrame + 1);
  77. GameScene.Scene.MapControl.TextureValid = false;
  78. }
  79. public virtual void Remove()
  80. {
  81. if (Owner != null)
  82. Owner.Effects.Remove(this);
  83. else
  84. MapControl.Effects.Remove(this);
  85. if (Complete != null)
  86. Complete(this, EventArgs.Empty);
  87. }
  88. public virtual void Draw()
  89. {
  90. if (CMain.Time < Start) return;
  91. if (Owner != null)
  92. {
  93. DrawLocation = Owner.DrawLocation;
  94. }
  95. else
  96. {
  97. DrawLocation = new Point((Source.X - MapObject.User.Movement.X + MapControl.OffSetX) * MapControl.CellWidth,
  98. (Source.Y - MapObject.User.Movement.Y + MapControl.OffSetY) * MapControl.CellHeight);
  99. DrawLocation.Offset(DrawOffset);
  100. DrawLocation.Offset(MapObject.User.OffSetMove);
  101. }
  102. if (Blend)
  103. Library.DrawBlend(BaseIndex + CurrentFrame, DrawLocation, Color.White, true, Rate);
  104. else
  105. Library.Draw(BaseIndex + CurrentFrame, DrawLocation, Color.White, true);
  106. }
  107. public void Clear()
  108. {
  109. Complete = null;
  110. Played = null;
  111. }
  112. }
  113. public class Missile : Effect
  114. {
  115. public static List<Missile> Missiles = new List<Missile>();
  116. public MapObject Target;
  117. public Point Destination;
  118. public int Interval, FrameCount, Skip;
  119. public int Direction;
  120. public bool Explode;
  121. public Missile(MLibrary library, int baseIndex, int count, int duration, MapObject owner, Point target, bool direction16 = true)
  122. : base(library, baseIndex, count, duration, owner)
  123. {
  124. Missiles.Add(this);
  125. Source = Owner.CurrentLocation;
  126. Destination = target;
  127. Direction = direction16 ? MapControl.Direction16(Source, Destination) : (int)Functions.DirectionFromPoint(Source, Destination);
  128. }
  129. public Missile(MLibrary library, int baseIndex, int count, int duration, Point source, Point target)
  130. : base(library, baseIndex, count, duration, source)
  131. {
  132. Missiles.Add(this);
  133. Destination = target;
  134. Direction = MapControl.Direction16(Source, Destination);
  135. }
  136. public override void Process()
  137. {
  138. if (CMain.Time < Start) return;
  139. if (Target != null) Destination = Target.CurrentLocation;
  140. else if (!Explode)
  141. {
  142. int dist = Functions.MaxDistance(Owner.CurrentLocation, Destination);
  143. if (dist < Globals.DataRange)
  144. Destination.Offset(Destination.X - Source.X, Destination.Y - Source.Y);
  145. }
  146. Duration = Functions.MaxDistance(Source, Destination) * 50;
  147. Count = Duration / Interval;
  148. if (Count == 0) Count = 1;
  149. base.Process();
  150. }
  151. public override void Remove()
  152. {
  153. base.Remove();
  154. Missiles.Remove(this);
  155. }
  156. public override void Draw()
  157. {
  158. if (CMain.Time < Start) return;
  159. int index = BaseIndex + (CurrentFrame % FrameCount) + Direction * (Skip + FrameCount);
  160. DrawLocation = new Point((Source.X - MapObject.User.Movement.X + MapControl.OffSetX) * MapControl.CellWidth,
  161. (Source.Y - MapObject.User.Movement.Y + MapControl.OffSetY) * MapControl.CellHeight);
  162. DrawLocation.Offset(MapObject.User.OffSetMove);
  163. int x = (Destination.X - Source.X) * MapControl.CellWidth;
  164. int y = (Destination.Y - Source.Y) * MapControl.CellHeight;
  165. DrawLocation.Offset(x * CurrentFrame / Count, y * CurrentFrame / Count);
  166. if (!Blend)
  167. Library.Draw(index, DrawLocation, Color.White, true);
  168. else
  169. Library.DrawBlend(index, DrawLocation, Color.White, true, Rate);
  170. }
  171. }
  172. public class InterruptionEffect : Effect
  173. {
  174. public static List<InterruptionEffect> effectlist = new List<InterruptionEffect>();
  175. bool noProcess = false;
  176. public InterruptionEffect(MLibrary library, int baseIndex, int count, int duration, MapObject owner, bool blend, long starttime = 0)
  177. : base(library, baseIndex, count, duration, owner)
  178. {
  179. Repeat = true;
  180. Blend = blend;
  181. effectlist.Add(this);
  182. }
  183. public override void Process()
  184. {
  185. if (!noProcess)
  186. base.Process();
  187. }
  188. public override void Remove()
  189. {
  190. base.Remove();
  191. effectlist.Remove(this);
  192. }
  193. public override void Draw()
  194. {
  195. if (!((PlayerObject)Owner).Concentrating)
  196. Remove();
  197. else if (((PlayerObject)Owner).ConcentrateInterrupted)
  198. noProcess = true;
  199. else
  200. noProcess = false;
  201. if (!noProcess)
  202. base.Draw();
  203. }
  204. public static int GetOwnerEffectID(uint objectID)
  205. {
  206. for (int i = 0; i < effectlist.Count; i++)
  207. {
  208. if (effectlist[i].Owner.ObjectID != objectID) continue;
  209. return i;
  210. }
  211. return -1;
  212. }
  213. }
  214. public class ElementsEffect : Effect
  215. {
  216. int myType;//1 = green orb, 2 = blue orb, 3 = red orb, 4 = mixed orbs
  217. long killAt;//holds the exp value for 4 orbs : kills all orbs when myType 4 is reached
  218. bool loopit = false;//soundloop
  219. public ElementsEffect(MLibrary library, int baseIndex, int count, int duration, MapObject owner, bool blend, int elementType, int killtime, bool loopon = false)
  220. : base(library, baseIndex, count, duration, owner)
  221. {
  222. Repeat = true;
  223. Blend = blend;
  224. myType = elementType;
  225. killAt = killtime;
  226. //
  227. loopit = loopon;
  228. StopSounds();
  229. StartSound();
  230. }
  231. public override void Process()
  232. {
  233. base.Process();
  234. }
  235. private void StartSound()
  236. {
  237. SoundManager.PlaySound(20000 + 126 * 10 + 4 + myType, loopit);
  238. }
  239. private void StopSounds()
  240. {
  241. for (int i = 0; i <= 3; i++)
  242. SoundManager.StopSound(20000 + 126 * 10 + 5 + i);
  243. }
  244. public override void Remove()
  245. {
  246. SoundManager.StopSound(20000 + 126 * 10 + 4 + myType);
  247. base.Remove();
  248. }
  249. public override void Draw()
  250. {
  251. if (!((PlayerObject)Owner).HasElements)
  252. Remove();
  253. if (((PlayerObject)Owner).ElementsLevel >= killAt && myType < 4)
  254. Remove();
  255. base.Draw();
  256. }
  257. }
  258. public class DelayedExplosionEffect : Effect
  259. {
  260. public static List<DelayedExplosionEffect> effectlist = new List<DelayedExplosionEffect>();
  261. public int stage;
  262. public DelayedExplosionEffect(MLibrary library, int baseIndex, int count, int duration, MapObject owner, bool blend, int Stage, long until)
  263. : base(library, baseIndex, count, duration, owner)
  264. {
  265. Repeat = (Stage == 2) ? false : true;
  266. Blend = blend;
  267. stage = Stage;
  268. effectlist.Add(this);
  269. if (stage == 1)
  270. SoundManager.PlaySound(20000 + 125 * 10);
  271. if (stage == 2)
  272. SoundManager.PlaySound(20000 + 125 * 10 + 5);
  273. }
  274. public override void Process()
  275. {
  276. base.Process();
  277. }
  278. public override void Remove()
  279. {
  280. base.Remove();
  281. effectlist.Remove(this);
  282. }
  283. public override void Draw()
  284. {
  285. if (Owner.Dead)
  286. {
  287. Remove();
  288. return;
  289. }
  290. base.Draw();
  291. }
  292. public static int GetOwnerEffectID(uint objectID)
  293. {
  294. for (int i = 0; i < effectlist.Count; i++)
  295. {
  296. if (effectlist[i].Owner.ObjectID != objectID) continue;
  297. return i;
  298. }
  299. return -1;
  300. }
  301. }
  302. public class SpecialEffect : Effect
  303. {
  304. public uint EffectType = 0;
  305. public SpecialEffect(MLibrary library, int baseIndex, int count, int duration, MapObject owner, bool blend, bool drawBehind, uint type)
  306. : base(library, baseIndex, count, duration, owner, 0, drawBehind)
  307. {
  308. Blend = blend;
  309. DrawBehind = drawBehind;
  310. EffectType = type;
  311. Light = -1;
  312. }
  313. public override void Process()
  314. {
  315. base.Process();
  316. }
  317. }
  318. public class BuffEffect : Effect
  319. {
  320. public BuffType BuffType;
  321. public BuffEffect(MLibrary library, int baseIndex, int count, int duration, MapObject owner, bool blend, BuffType buffType)
  322. : base(library, baseIndex, count, duration, owner, 0)
  323. {
  324. Repeat = true;
  325. Blend = blend;
  326. BuffType = buffType;
  327. Light = -1;
  328. }
  329. public override void Process()
  330. {
  331. base.Process();
  332. }
  333. }
  334. public class TrackableEffect : Effect
  335. {
  336. public static List<TrackableEffect> effectlist = new List<TrackableEffect>();
  337. public string EffectName = "default";
  338. public TrackableEffect(Effect baseEffect, string effName = "null")
  339. : base(baseEffect.Library, baseEffect.BaseIndex, baseEffect.Count, baseEffect.Duration, baseEffect.Owner, baseEffect.Start)
  340. {
  341. Repeat = baseEffect.Repeat;
  342. RepeatUntil = baseEffect.RepeatUntil;
  343. Blend = baseEffect.Blend;
  344. EffectName = effName;
  345. effectlist.Add(this);
  346. }
  347. public static int GetOwnerEffectID(uint objectID, string effectName = "null")
  348. {
  349. for (int i = 0; i < effectlist.Count; i++)
  350. {
  351. if (effectlist[i].Owner.ObjectID != objectID) continue;
  352. if (effectName != "null" && effectlist[i].EffectName != effectName) continue;
  353. return i;
  354. }
  355. return -1;
  356. }
  357. public override void Process()
  358. {
  359. base.Process();
  360. if (Owner == null) Remove();
  361. else if (Owner.Dead) Remove();
  362. }
  363. public override void Remove()
  364. {
  365. base.Remove();
  366. effectlist.Remove(this);
  367. }
  368. public void RemoveNoComplete()
  369. {
  370. if (Owner != null)
  371. Owner.Effects.Remove(this);
  372. else
  373. MapControl.Effects.Remove(this);
  374. }
  375. }
  376. public class LightEffect : Effect
  377. {
  378. public LightEffect(int duration, MapObject owner, long starttime = 0, int lightDistance = 6, Color? lightColour = null)
  379. : base(null, 0, 0, duration, owner, starttime)
  380. {
  381. Light = lightDistance;
  382. LightColour = lightColour == null ? Color.White : (Color)lightColour;
  383. }
  384. public LightEffect(int duration, Point source, long starttime = 0, int lightDistance = 6, Color? lightColour = null)
  385. : base(null, 0, 0, duration, source, starttime)
  386. {
  387. Light = lightDistance;
  388. LightColour = lightColour == null ? Color.White : (Color)lightColour;
  389. }
  390. public override void Process()
  391. {
  392. if (CMain.Time >= Start + Duration)
  393. Remove();
  394. GameScene.Scene.MapControl.TextureValid = false;
  395. }
  396. public override void Draw()
  397. {
  398. if (CMain.Time < Start) return;
  399. if (Owner != null)
  400. {
  401. DrawLocation = Owner.DrawLocation;
  402. }
  403. else
  404. {
  405. DrawLocation = new Point((Source.X - MapObject.User.Movement.X + MapControl.OffSetX) * MapControl.CellWidth,
  406. (Source.Y - MapObject.User.Movement.Y + MapControl.OffSetY) * MapControl.CellHeight);
  407. DrawLocation.Offset(MapObject.User.OffSetMove);
  408. }
  409. }
  410. }
  411. }