ZoriaRPG

ZScript: Moosh 3D

May 19th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. ffc script Test{
  2.     void run(){
  3.         int yaw;
  4.         int pitch;
  5.         int roll;
  6.         while(true){
  7.             if(Link->InputUp)
  8.                 pitch = WrapDegrees(pitch-1.5);
  9.             else if(Link->InputDown)
  10.                 pitch = WrapDegrees(pitch+1.5);
  11.             if(Link->InputLeft)
  12.                 yaw = WrapDegrees(yaw-1.5);
  13.             else if(Link->InputRight)
  14.                 yaw = WrapDegrees(yaw+1.5);
  15.             if(Link->InputL)
  16.                 roll = WrapDegrees(roll-1.5);
  17.             else if(Link->InputR)
  18.                 roll = WrapDegrees(roll+1.5);
  19.             int tex[6] = {1540, 1541, 1542, 1543, 1544, 1545};
  20.             DrawCube(2, 32, 32, 16, yaw, pitch, roll, tex, 1, 7);
  21.             WaitNoAction();
  22.         }
  23.     }
  24. }
  25.  
  26. void DrawCube(int layer, int x, int y, int scale, int yaw, int pitch, int roll, int tex, int texwidth, int cs){
  27.     int cx[8];
  28.     int cy[8];
  29.     int cz[8];
  30.     //Initialize all 8 points in the default position
  31.     for(int i=0; i<4; i++){
  32.         cx[i] = VectorX3D(scale, -45+90*i, -45);
  33.         cy[i] = VectorY3D(scale, -45+90*i, -45);
  34.         cz[i] = scale/2;
  35.        
  36.         cx[i+4] = VectorX3D(scale, -45+90*i, 90+45);
  37.         cy[i+4] = VectorY3D(scale, -45+90*i, 90+45);
  38.         cz[i+4] = -scale/2;
  39.     }
  40.    
  41.     //Rotate from the default position to the desired rotation
  42.     RotateX3D(yaw, cx, cy, cz);
  43.     RotateY3D(pitch, cx, cy, cz);
  44.     RotateZ3D(roll, cx, cy, cz);
  45.    
  46.     //Draw all six faces of the cube
  47.     DrawCubeFace(layer, x, y, 0, 1, 2, 3, tex[0], texwidth, cs, cx, cy, cz);
  48.     DrawCubeFace(layer, x, y, 4, 5, 6, 7, tex[1], texwidth, cs, cx, cy, cz);
  49.    
  50.     DrawCubeFace(layer, x, y, 1, 2, 4, 7, tex[2], texwidth, cs, cx, cy, cz);
  51.     DrawCubeFace(layer, x, y, 3, 0, 6, 5, tex[3], texwidth, cs, cx, cy, cz);
  52.    
  53.     DrawCubeFace(layer, x, y, 0, 1, 7, 6, tex[4], texwidth, cs, cx, cy, cz);
  54.     DrawCubeFace(layer, x, y, 2, 3, 5, 4, tex[5], texwidth, cs, cx, cy, cz);
  55. }
  56.  
  57. void DrawCubeFace(int layer, int x, int y, int p1, int p2, int p3, int p4, int tex, int texwidth, int cs, int cx, int cy, int cz){
  58.     //Mask out back sides
  59.     if(CubeCenterZ(p1, p2, p3, p4, cz)>=0){
  60.         //Assign points from cube arrays to quad array
  61.         int pos[12];
  62.        
  63.         pos[0] = x+cx[p1];
  64.         pos[1] = y+cy[p1];
  65.         pos[2] = cy[p1];
  66.        
  67.         pos[3] = x+cx[p2];
  68.         pos[4] = y+cy[p2];
  69.         pos[5] = cy[p2];
  70.        
  71.         pos[6] = x+cx[p3];
  72.         pos[7] = y+cy[p3];
  73.         pos[8] = cy[p3];
  74.        
  75.         pos[9] = x+cx[p4];
  76.         pos[10] = y+cy[p4];
  77.         pos[11] = cy[p4];
  78.        
  79.         //Some more garbage
  80.         int w = texwidth*16-1;
  81.         int uv[8] = {0,0,  0,w,  w,w,  w,0};
  82.         int csets[4] = {cs, cs, cs, cs};
  83.         int size[2] = {texwidth, texwidth};
  84.        
  85.         Screen->Quad3D(6, pos, uv, csets, size, 0, tex, PT_MASKTEXTURE);
  86.     }      
  87. }
  88.  
  89. int CubeCenterZ(int p1, int p2, int p3, int p4, int cz){
  90.     return (cz[p1]+cz[p2]+cz[p3]+cz[p4])/4;
  91. }
  92.  
  93. void RotateX3D(int angle, int cx, int cy, int cz){
  94.     for(int i=0; i<SizeOfArray(cx); i++){
  95.         int y = cy[i];
  96.         int z = cz[i];
  97.        
  98.         cy[i] = y * Cos(angle) - z * Sin(angle);
  99.         cz[i] = z * Cos(angle) + y * Sin(angle);
  100.     }
  101. }
  102.  
  103. void RotateY3D(int angle, int cx, int cy, int cz){
  104.     for(int i=0; i<SizeOfArray(cx); i++){
  105.         int x = cx[i];
  106.         int z = cz[i];
  107.        
  108.         cx[i] = x * Cos(angle) - z * Sin(angle);
  109.         cz[i] = z * Cos(angle) + x * Sin(angle);
  110.     }
  111. }
  112.  
  113. void RotateZ3D(int angle, int cx, int cy, int cz){
  114.     for(int i=0; i<SizeOfArray(cx); i++){
  115.         int x = cx[i];
  116.         int y = cy[i];
  117.        
  118.         cx[i] = x * Cos(angle) - y * Sin(angle);
  119.         cy[i] = y * Cos(angle) + x * Sin(angle);
  120.     }
  121. }
  122.  
  123. int VectorX3D(int distance, int yaw, int pitch){
  124.     return distance*Sin(pitch)*Cos(yaw);
  125. }
  126.  
  127. int VectorY3D(int distance, int yaw, int pitch){
  128.     return distance*Sin(pitch)*Sin(yaw);
  129. }
  130.  
  131. int VectorZ3D(int distance, int yaw, int pitch){
  132.     return distance*Cos(pitch);
  133. }
Add Comment
Please, Sign In to add comment