SpecialEffectsRenderer.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace OpenVIII.World
  9. {
  10. class SpecialEffectsRenderer
  11. {
  12. //FX SYSTEM EXPLAINED
  13. /*
  14. * So in vanilla ff8 when you move through the forest for example it spawns the texture
  15. * and that texture shrinks to 0% then is destroyed.
  16. * The fxWalkDuration should be incremented every bHasMoved until X then spawn texture at player position
  17. * with 100% scale and spriteId until it gets to 0% and is destroyed
  18. *
  19. */
  20. private static float fxWalkDuration = 0;
  21. private static int lastFxBushSpriteId = 0;
  22. struct worldFx
  23. {
  24. public Vector3 fxLocation;
  25. public float scale;
  26. public int atlasId;
  27. }
  28. private static List<worldFx> worldEffects = new List<worldFx>();
  29. /// <summary>
  30. /// Takes care of drawing shadows and additional FX when needed (like in forest). [WIP]
  31. /// </summary>
  32. public static void DrawCharacterShadowSpecialEffects()
  33. {
  34. Module_world_debug.worldCharacterInstances[Module_world_debug.currentControllableEntity].bDraw = true; //always draw and later test the cases
  35. if (Module_world_debug.activeCollidePolygon == null)
  36. return;
  37. #region casual shadow
  38. if ((Module_world_debug.activeCollidePolygon.Value.vertFlags & Module_world_debug.TRIFLAGS_FORESTTEST) > 0 && Module_world_debug.activeCollidePolygon.Value.groundtype > 5) //shadow
  39. {
  40. var shadowGeom = Extended.GetShadowPlane(Module_world_debug.playerPosition + new Vector3(-2.2f, .1f, -2.2f), 4f);
  41. Module_world_debug.ate.Texture = (Texture2D)Module_world_debug.wmset.GetWorldMapTexture(Wmset.Section38_textures.shadowBig, 0);
  42. Module_world_debug.ate.Alpha = .25f;
  43. foreach (var pass in Module_world_debug.ate.CurrentTechnique.Passes)
  44. {
  45. pass.Apply();
  46. Module_world_debug.ate.GraphicsDevice.DepthStencilState = DepthStencilState.None;
  47. Memory.Graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, shadowGeom, 0, shadowGeom.Length / 3);
  48. }
  49. Module_world_debug.ate.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  50. Module_world_debug.ate.Alpha = 1f;
  51. }
  52. #endregion
  53. #region forest leaves fx
  54. else if ((Module_world_debug.activeCollidePolygon.Value.vertFlags & Module_world_debug.TRIFLAGS_FORESTTEST) == 0 && Module_world_debug.activeCollidePolygon.Value.texFlags == 0
  55. && Module_world_debug.activeCollidePolygon.Value.groundtype <= 5) //forest
  56. {
  57. Module_world_debug.ate.Alpha = 1f;
  58. Module_world_debug.worldCharacterInstances[Module_world_debug.currentControllableEntity].bDraw = false;
  59. if (Module_world_debug.bHasMoved && fxWalkDuration > 0.25f)
  60. {
  61. fxWalkDuration = 0f;
  62. lastFxBushSpriteId %= 4;
  63. worldEffects.Add(new worldFx()
  64. {
  65. fxLocation = Module_world_debug.playerPosition,
  66. scale = 1.00f, atlasId = lastFxBushSpriteId++
  67. });
  68. }
  69. else fxWalkDuration += 0.05f;
  70. }
  71. #endregion
  72. //All effects renderer below except shadows which are rendered in their own region
  73. for (int i = worldEffects.Count - 1; i > 0; i--)
  74. {
  75. //we get basic square geometry here
  76. VertexPositionTexture[] shadowGeom = Extended.GetShadowPlane(worldEffects[i].fxLocation +
  77. new Vector3(-2.2f, .1f, -2.2f), 12f * worldEffects[i].scale);
  78. Extended.ConvertToSprite(ref shadowGeom, 4, worldEffects[i].atlasId);
  79. Module_world_debug.ate.Texture = (Texture2D)Module_world_debug.wmset.GetWorldMapTexture(Wmset.Section38_textures.wmfx_bush,
  80. MathHelper.Clamp(GetLeavesFxClut(Module_world_debug.activeCollidePolygon), 0, 5));
  81. Module_world_debug.ate.Alpha = 0.75f;
  82. foreach (EffectPass pass in Module_world_debug.ate.CurrentTechnique.Passes)
  83. {
  84. pass.Apply();
  85. Module_world_debug.ate.GraphicsDevice.DepthStencilState = DepthStencilState.None;
  86. Memory.Graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, shadowGeom, 0, shadowGeom.Length / 3);
  87. }
  88. //ate.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  89. worldFx currentFx = worldEffects[i];
  90. currentFx.scale -= 0.005f;
  91. if (currentFx.scale < 0.8)
  92. worldEffects.RemoveAt(i);
  93. else
  94. worldEffects[i] = currentFx;
  95. }
  96. }
  97. /// <summary>
  98. /// Gets leaves fx clut ID based on actively colliding polygon. The clut ID is tied to ground type.
  99. /// </summary>
  100. /// <param name="activeCollidePolygon">polygon that player is on</param>
  101. /// <returns></returns>
  102. private static int GetLeavesFxClut(Module_world_debug.Polygon? activeCollidePolygon)
  103. {
  104. switch (activeCollidePolygon.Value.groundtype)
  105. {
  106. case 0:
  107. return 5;
  108. case 1:
  109. return 3;
  110. case 2:
  111. return 1;
  112. case 3:
  113. return 4;
  114. case 4:
  115. return 0;
  116. case 5:
  117. return 2;
  118. default:
  119. return 0;
  120. }
  121. }
  122. }
  123. }