Advertisement
KSSBrawl_

WaveCircle.dnh

Oct 16th, 2018
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.22 KB | None | 0 0
  1. // Constants
  2. //------------------------------------------------------------
  3.  
  4. // Maximum effect width
  5. let MAX_WIDTH = 160;
  6.  
  7. // Effect color RGB values, adjust to your liking
  8. let EFF_COLOR_R = 192;
  9. let EFF_COLOR_G = 255;
  10. let EFF_COLOR_B = 255;
  11.  
  12. // Variables
  13. //------------------------------------------------------------
  14.  
  15. // Boss object ID, pass to this script using SetScriptArgument().
  16. let nParentID = GetScriptArgument( 0 );
  17.  
  18. let nParentX = ObjMove_GetX( nParentID );
  19. let nParentY = ObjMove_GetY( nParentID );
  20.  
  21. let nOldX = ObjMove_GetX( nParentID );
  22. let nOldY = ObjMove_GetY( nParentID );
  23.  
  24. // Main
  25. //------------------------------------------------------------
  26.  
  27. @Event
  28. {
  29. }
  30.  
  31. @Initialize
  32. {
  33.     // Color correction
  34.     EFF_COLOR_R = 255 - EFF_COLOR_R;
  35.     EFF_COLOR_G = 255 - EFF_COLOR_G;
  36.     EFF_COLOR_B = 255 - EFF_COLOR_B;
  37.  
  38.     TWaveCircle();
  39. }
  40.  
  41. @MainLoop
  42. {
  43.     nParentX = ObjMove_GetX( nParentID );
  44.     nParentY = ObjMove_GetY( nParentID );
  45.     yield;
  46.     nOldX = nParentX;
  47.     nOldY = nParentY;
  48. }
  49.  
  50. @Finalize
  51. {
  52. }
  53.  
  54. // Functions
  55. //------------------------------------------------------------
  56.  
  57. task TWaveCircle()
  58. {
  59.     // Variables
  60.     //------------------------------------------------------------
  61.  
  62.     let renderTexture = GetReservedRenderTargetName( 0 );
  63.  
  64.     // Stores the primitive objects making up the effect
  65.     let a_nObj = [];
  66.  
  67.     // Current effect width
  68.     let nWidth = 16;
  69.  
  70.     // Effect width plus 20
  71.     let nWidth2;
  72.  
  73.     // X oscillator, controls wobble along the X axis
  74.     let nOscX = 0;
  75.     // Y oscillator, controls wobble along the Y axis
  76.     let nOscY = 0;
  77.     // X oscillator phase offset, used to add ripples
  78.     let nPhaseX = 0;
  79.     // Y oscillator phase offset, used to add ripples
  80.     let nPhaseY = 0;
  81.  
  82.     // Whether the effect is still expanding
  83.     let bExpand = true;
  84.  
  85.     // Begin
  86.     //------------------------------------------------------------
  87.  
  88.     loop ( 16 )
  89.     {
  90.         let obj = ObjPrim_Create( OBJ_PRIMITIVE_2D );
  91.         Obj_SetRenderPriority( obj, 0.29 );
  92.         ObjPrim_SetTexture( obj, renderTexture );
  93.         ObjPrim_SetPrimitiveType( obj, PRIMITIVE_TRIANGLESTRIP );
  94.         ObjPrim_SetVertexCount( obj, 34 );
  95.  
  96.         a_nObj = a_nObj ~ [obj];
  97.     }
  98.  
  99.     // Hack to prevent index going out of bounds
  100.     a_nObj = a_nObj ~ [ID_INVALID];
  101.  
  102.     while ( !Obj_IsDeleted( nParentID ) )
  103.     {
  104.         // Update texture
  105.         RenderToTextureA1( renderTexture, 20, 28, true );
  106.  
  107.         // Increase effect width
  108.         if ( nWidth < 160 )
  109.         {
  110.             nWidth += 2;
  111.         } else if ( bExpand )
  112.         {
  113.             bExpand = false;
  114.         }
  115.  
  116.         // Update object positions
  117.         ascent ( i in 0..16 )
  118.         {
  119.             ObjRender_SetPosition( a_nObj[i], nParentX, nParentY, 0 );
  120.         }
  121.  
  122.         // Update vertices
  123.         //------------------------------------------------------------
  124.  
  125.         // Size of effect
  126.         let nEffSize = nWidth * nWidth;
  127.  
  128.         nPhaseX = nOscX;
  129.         nPhaseY = nOscY;
  130.  
  131.         // Texture UV offsets
  132.         let nBiasX = nParentX + 32 - 180;
  133.         let nBiasY = nParentY + 16 - 180;
  134.  
  135.         nWidth2 = nWidth + 20;
  136.  
  137.         // I'm willing to wager 80% of this code is optimization
  138.  
  139.         let i = 0;
  140.  
  141.         loop ( 17 )
  142.         {
  143.             // Previous primitive
  144.             let obj1;
  145.             // Current primitive
  146.             let obj2 = a_nObj[i];
  147.  
  148.             if ( i > 0 )
  149.             {
  150.                 obj1 = a_nObj[min( i - 1, 15 )];
  151.             } else
  152.             {
  153.                 obj1 = a_nObj[0];
  154.             }
  155.  
  156.             let j = 0;
  157.  
  158.             // Vertex X coordinate
  159.             let nX = ( 0.125*i - 1.0 ) * nWidth2;
  160.             // Vertex texture U
  161.             let nU = 22.5*i + nBiasX;
  162.  
  163.             loop ( 17 )
  164.             {
  165.                 // Vertex Y coordinate
  166.                 let nY = ( 0.125*j - 1.0 ) * nWidth2;
  167.                 // Vertex texture V
  168.                 let nV = 22.5*j + nBiasY;
  169.  
  170.                 // Vertex's distance (squared) from the effect's center
  171.                 let nDistance = nX*nX + nY*nY;
  172.  
  173.                 // Vertex indices
  174.                 let nIndex1 = j*2;
  175.                 let nIndex2 = j*2 + 1;
  176.  
  177.                 if ( bExpand )
  178.                 {
  179.                     ObjPrim_SetVertexUVT( obj1, nIndex2, nU, nV );
  180.                     ObjPrim_SetVertexUVT( obj2, nIndex1, nU, nV );
  181.                 }  else if ( nOldX != nParentX || nOldY != nParentY )
  182.                 {
  183.                     ObjPrim_SetVertexUVT( obj1, nIndex2, nU, nV );
  184.                     ObjPrim_SetVertexUVT( obj2, nIndex1, nU, nV );
  185.                 }
  186.  
  187.                 if ( nDistance < nEffSize )
  188.                 {
  189.                     // Multiplier to scale calculations to fit the bulge
  190.                     let nMult = ( nEffSize - nDistance ) / nEffSize;
  191.  
  192.                     // X and Y components of a vector from the effect's center to the vertex's position
  193.                     let nVectorX;
  194.                     let nVectorY;
  195.  
  196.                     // Calculate vector components
  197.                     if ( nDistance != 0 )
  198.                     {
  199.                         nDistance ^= -0.5;
  200.                         nVectorX = nX * nDistance;
  201.                         nVectorY = nY * nDistance;
  202.                     } else
  203.                     {
  204.                         nVectorX = 0;
  205.                         nVectorY = 0;
  206.                     }
  207.  
  208.                     let nMult2 = 32 * nMult;
  209.                     let nMult3 =  8 * nMult;
  210.  
  211.                     // Offset vertex position
  212.                     let nX2 = nX + nMult2*nVectorX + nMult3*sin( nPhaseX );
  213.                     nY +=          nMult2*nVectorY + nMult3*sin( nPhaseY );
  214.  
  215.                     ObjPrim_SetVertexPosition( obj1, nIndex2, nX2, nY, 0 );
  216.    
  217.                     if ( i < 16 )
  218.                     {
  219.                         ObjPrim_SetVertexPosition( obj2, nIndex1, nX2, nY, 0 );
  220.                     }
  221.  
  222.                     // Calculate and apply color gradient
  223.                     if ( bExpand )
  224.                     {
  225.                         let nRed   = 255 - nMult*EFF_COLOR_R;
  226.                         let nGreen = 255 - nMult*EFF_COLOR_G;
  227.                         let nBlue  = 255 - nMult*EFF_COLOR_B;
  228.    
  229.                         ObjPrim_SetVertexColor( obj1, nIndex2, nRed, nGreen, nBlue );
  230.                         ObjPrim_SetVertexAlpha( obj1, nIndex2, 255 );
  231.                
  232.                         if ( i < 16 )
  233.                         {
  234.                             ObjPrim_SetVertexColor( obj2, nIndex1, nRed, nGreen, nBlue );
  235.                             ObjPrim_SetVertexAlpha( obj2, nIndex1, 255 );
  236.                         }
  237.                     }
  238.                 } else
  239.                 {
  240.                     if ( bExpand )
  241.                     {
  242.                         ObjPrim_SetVertexPosition( obj1, nIndex2, nX, nY, 0 );
  243.                         ObjPrim_SetVertexColor( obj1, nIndex2, 255, 255, 255 );
  244.                         ObjPrim_SetVertexAlpha( obj1, nIndex2, 0 );
  245.    
  246.                         if ( i < 16 )
  247.                         {
  248.                             ObjPrim_SetVertexPosition( obj2, nIndex1, nX, nY, 0 );
  249.                             ObjPrim_SetVertexColor( obj2, nIndex1, 255, 255, 255 );
  250.                             ObjPrim_SetVertexAlpha( obj2, nIndex1, 0 );
  251.                         }
  252.                     } else
  253.                     {
  254.                         if ( nOldX != nParentX || nOldY != nParentY )
  255.                         {
  256.                             ObjPrim_SetVertexPosition( obj1, nIndex2, nX, nY, 0 );
  257.    
  258.                             if ( i < 16 )
  259.                             {
  260.                                 ObjPrim_SetVertexPosition( obj2, nIndex1, nX, nY, 0 );
  261.                             }
  262.                         }
  263.                     }
  264.                 }
  265.                 nPhaseX += 5.625;
  266.                 nPhaseY -= 2.8125;
  267.                 j++;
  268.             }
  269.             i++;
  270.         }
  271.  
  272.         nOscX += 11.25;
  273.         nOscY += 5.625;
  274.  
  275.         yield;
  276.     }
  277.  
  278.     ascent ( i in 0..16 )
  279.     {
  280.         Obj_Delete( a_nObj[i] );
  281.     }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement