home *** CD-ROM | disk | FTP | other *** search
/ Nejlepší České Hry / Nej české hry.iso / hry / plane arcade / planearcade.exe / tank3.bmp / bullets.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  7.1 KB  |  310 lines

  1.  
  2. #include "main.h"
  3.  
  4. //Inicializacia zasobnika
  5. //------------------------------------------------------------------
  6. void BULLETSYSTEM::Initialize()
  7. {
  8.  
  9.     //log
  10.     LogPrint("Vytvaram zasobnik striel");
  11.  
  12.     //vynulovanie
  13.     VertexBuffer = NULL;
  14.     ActBullet = 0;
  15.     Friendly = false;
  16.     UnFriendly = false;
  17.  
  18.     //Vertex Buffer
  19.     if( FAILED( g_pd3dDevice->CreateVertexBuffer( Bullet_MaxBullets*3*sizeof(CUSTOMVERTEXBULLET),
  20.                                                   0, D3DFVF_CUSTOMVERTEXBULLET,
  21.                                                   D3DPOOL_DEFAULT, &VertexBuffer, NULL ) ) )
  22.     {
  23.         LogPrint(" chyba pri vytvarani Vertex Bufferu");
  24.     }
  25.     else
  26.     {
  27.         LogPrint(" Vertex Buffer vytvoreny");
  28.     }
  29.  
  30. }
  31.  
  32. //destruktor
  33. //------------------------------------------------------------------
  34. BULLETSYSTEM::~BULLETSYSTEM()
  35. {
  36.  
  37.     if (VertexBuffer)
  38.         VertexBuffer->Release();
  39.     VertexBuffer = NULL;
  40.  
  41. }
  42.  
  43. //resetuje hodnoty
  44. //-----------------------------------------------------------------
  45. void BULLETSYSTEM::Reset()
  46. {
  47.  
  48.     ActBullet = 0;
  49.  
  50.     for (int i=0;i<Bullet_MaxBullets;i++)
  51.     {
  52.         Bullet[i].Active = false;
  53.     }
  54. }
  55.  
  56. //refresh - vyrenderovanie a posunutie
  57. //-----------------------------------------------------------------
  58. void BULLETSYSTEM::Refresh()
  59. {
  60.  
  61.     int i;
  62.  
  63.     ///////////
  64.     //Process//
  65.     ///////////
  66.     //posunie jednotlive gulky
  67.     
  68.     VECTOR3D PomPos;
  69.  
  70.     for (i = 0;i<Bullet_MaxBullets;i++)
  71.     {
  72.  
  73.         if (Bullet[i].Active == false)
  74.             continue;
  75.  
  76.         //ak je mimo hracej plochy tak deaktivuj
  77.         if ((Bullet[i].Pos.X > F_MapSize || Bullet[i].Pos.X < -F_MapSize) ||
  78.             (Bullet[i].Pos.Z > F_MapSize || Bullet[i].Pos.Z < -F_MapSize) ||
  79.             (Bullet[i].Pos.Y > F_MapSize || Bullet[i].Pos.Y < -F_MapSize))
  80.             Bullet[i].Active = false;
  81.             
  82.  
  83.         PomPos.X = Bullet[i].Pos.X + Power(Bullet_Speed)*(Bullet[i].Sme.X);
  84.         PomPos.Y = Bullet[i].Pos.Y + Power(Bullet_Speed)*(Bullet[i].Sme.Y);
  85.         PomPos.Z = Bullet[i].Pos.Z + Power(Bullet_Speed)*(Bullet[i].Sme.Z);
  86.  
  87.         //kolizia
  88.         if (Collision(Bullet[i].Pos, PomPos) == true)
  89.         {
  90.             Bullet[i].Active = false;
  91.         }
  92.         
  93.         //inak posun
  94.         Bullet[i].Pos = PomPos;
  95.     }
  96.  
  97.     //////////
  98.     //Render//
  99.     //////////
  100.     int NumVertices = 0;
  101.     CUSTOMVERTEXBULLET *Vertices;
  102.  
  103.     //Otvor VB
  104.     VertexBuffer->Lock(0, 0, (void**)&Vertices, 0 ) ;
  105.  
  106.     //zobrazi model do pola vertexov
  107.     for (i = 0;i<Bullet_MaxBullets;i++)
  108.     {
  109.  
  110.         //ak nieje aktivovany preskoc
  111.         if (Bullet[i].Active == false)
  112.             continue;
  113.  
  114.         //pridaj vertex
  115.         Vertices[NumVertices].pos.x = Bullet[i].Pos.X;
  116.         Vertices[NumVertices].pos.y = Bullet[i].Pos.Y;
  117.         Vertices[NumVertices].pos.z = Bullet[i].Pos.Z;
  118.         Vertices[NumVertices].color = 0xffff0000;
  119.         NumVertices++;
  120.  
  121.         //pridaj vertex
  122.         Vertices[NumVertices].pos.x = (Bullet[i].Pos.X + Bullet_Size*Bullet[i].Sme.X) + (1.0f*(-Bullet[i].Sme.Z));
  123.         Vertices[NumVertices].pos.y = (Bullet[i].Pos.Y + Bullet_Size*Bullet[i].Sme.Y) +  1.0f ;
  124.         Vertices[NumVertices].pos.z = (Bullet[i].Pos.Z + Bullet_Size*Bullet[i].Sme.Z) + (1.0f*(Bullet[i].Sme.X)) ;
  125.         Vertices[NumVertices].color = 0xffffff00;
  126.         NumVertices++;
  127.  
  128.         //pridaj vertex
  129.         Vertices[NumVertices].pos.x = (Bullet[i].Pos.X + Bullet_Size*Bullet[i].Sme.X) + (1.0f*(Bullet[i].Sme.Z));
  130.         Vertices[NumVertices].pos.y = (Bullet[i].Pos.Y + Bullet_Size*Bullet[i].Sme.Y) -  1.0f;
  131.         Vertices[NumVertices].pos.z = (Bullet[i].Pos.Z + Bullet_Size*Bullet[i].Sme.Z) + (1.0f*(-Bullet[i].Sme.X));
  132.         Vertices[NumVertices].color = 0xffffff00;
  133.         NumVertices++;
  134.             
  135.     }
  136.  
  137.     //uzavri VB
  138.     VertexBuffer->Unlock() ;
  139.  
  140.     //vynuluj maticu
  141.     D3DXMATRIXA16 NullMatrix;        
  142.     D3DXMatrixIdentity(&NullMatrix);
  143.     g_pd3dDevice->SetTransform( D3DTS_WORLD, &NullMatrix);
  144.  
  145.     //vypni texturu
  146.     g_pd3dDevice->SetTexture(0,NULL);
  147.     
  148.     //vyrenderuj
  149.     g_pd3dDevice->SetStreamSource( 0, VertexBuffer, 0, sizeof(CUSTOMVERTEXBULLET));
  150.     g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEXBULLET);
  151.     g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0 , NumVertices/3 );
  152.  
  153.     //reset
  154.     Engine.ResetToDefault();
  155. }
  156.  
  157. //Vypusti bullet
  158. //--------------------------------------------------------------
  159. void BULLETSYSTEM::SpawnBullet(VECTOR3D Pos,VECTOR3D Rot)
  160. {
  161.  
  162.     Bullet[ActBullet].Pos = Pos;
  163.  
  164.     //vypocita smerovy vektor
  165.     Bullet[ActBullet].Sme.X =  cosf(Rot.X) * sinf(Rot.Y);
  166.     Bullet[ActBullet].Sme.Z =  cosf(Rot.X) * cosf(Rot.Y);
  167.     Bullet[ActBullet].Sme.Y = -sinf(Rot.X);
  168.  
  169.     //nepresnost
  170.     Bullet[ActBullet].Sme.X += RandomMinMax(-0.01f,0.01f);
  171.     Bullet[ActBullet].Sme.Y += RandomMinMax(-0.01f,0.01f);
  172.     Bullet[ActBullet].Sme.Z += RandomMinMax(-0.01f,0.01f);
  173.  
  174.     Bullet[ActBullet].Active = true;
  175.  
  176.     ActBullet++;
  177.  
  178.     if (ActBullet == Bullet_MaxBullets)
  179.         ActBullet = 0;
  180. }
  181.  
  182. //kolizia zo vsetkymi objektami
  183. //----------------------------------------------------------------
  184. bool BULLETSYSTEM::Collision(VECTOR3D P1, VECTOR3D P2)
  185. {
  186.     int i;
  187.  
  188.     //ZEM
  189.     //--------------
  190.     if (Level.Krajina.Collise(P1,P2) == true)
  191.     {
  192.         Explo.SpawnHit(Level.Krajina.IntPos);
  193.         return true;
  194.     }
  195.  
  196.  
  197.     ///////////////////////
  198.     //NEPRIATELSKE STRELY//
  199.     ///////////////////////
  200.     if (UnFriendly == true)
  201.     {
  202.  
  203.         //kolizia so spitfirom
  204.         if (SpitFire.CollisionDetail(P1,P2) == true)
  205.         {
  206.             SpitFire.Life -= Bullet_Power;
  207.  
  208.             Explo.SpawnHit(SpitFire.Pos);
  209.             return true;
  210.         }
  211.  
  212.     }
  213.  
  214.     //////////////////
  215.     //PLAYERS STRELY//
  216.     //////////////////
  217.     if (Friendly == true)
  218.     {
  219.  
  220.         //
  221.         //MesserSchmitt
  222.         for (i=0;i<Max_MesserSchmitt;i++)
  223.         {
  224.             if (Level.MesserSchmitt[i].Active == false)
  225.                 continue;
  226.  
  227.             if (Level.MesserSchmitt[i].CollisionBox(P1,P2) == true)
  228.             {
  229.                 Level.MesserSchmitt[i].Life -= Bullet_Power;
  230.                 Explo.SpawnHit(Level.MesserSchmitt[i].Pos);
  231.  
  232.                 //prepni do uhybacieho modu
  233.                 Level.MesserSchmitt[i].Uhybanie = true;
  234.                 Level.MesserSchmitt[i].Sledovanie = false;
  235.                 Level.MesserSchmitt[i].Vyhybanie = false;
  236.  
  237.                 return true;
  238.             }
  239.         }
  240.  
  241.         //
  242.         //Volker
  243.         for (i=0;i<Max_Volkers;i++)
  244.         {
  245.             if (Level.Volker[i].Active == false)
  246.                 continue;
  247.  
  248.             if (Level.Volker[i].CollisionBox(P1,P2) == true)
  249.             {
  250.                 Level.Volker[i].Life -= Bullet_Power;
  251.                 Explo.SpawnHit(Level.Volker[i].Pos);
  252.  
  253.                 //prepni do uhybacieho modu
  254.                 Level.Volker[i].Uhybanie = true;
  255.                 Level.Volker[i].Sledovanie = false;
  256.                 Level.Volker[i].Vyhybanie = false;
  257.  
  258.                 return true;
  259.             }
  260.         }
  261.  
  262.         //
  263.         //Bombarder
  264.         for (i=0;i<Max_Bombarders;i++)
  265.         {
  266.             if (Level.Bombarder[i].Active == false)
  267.                 continue;
  268.  
  269.             if (Level.Bombarder[i].CollisionDetail(P1,P2) == true)
  270.             {
  271.                 Level.Bombarder[i].Life -= Bullet_Power;
  272.                 Explo.SpawnHit(ModelLib.Bombarder_ModelNormal.ColPosition);
  273.  
  274.                 return true;
  275.             }
  276.         }
  277.  
  278.         //
  279.         //Strucure
  280.         for (i=0;i<Max_Structures;i++)
  281.         {
  282.             if (Level.Structure[i].CollisionDetail(P1,P2) == true)
  283.             {
  284.                 Level.Structure[i].Life -= Bullet_Power;
  285.                 Explo.SpawnHit(ModelLib.GetStructure(Level.Structure[i].ModelIndex)->ColPosition);
  286.  
  287.                 return true;
  288.             }
  289.         }
  290.  
  291.         //
  292.         //Truck
  293.         for (i=0;i<Max_Trucks;i++)
  294.         {
  295.             if (Level.Truck[i].CollisionDetail(P1,P2) == true)
  296.             {
  297.                 Level.Truck[i].Life -= Bullet_Power;
  298.                 Explo.SpawnHit(ModelLib.Truck_ModelDestroyed.ColPosition);
  299.  
  300.                 return true;
  301.             }
  302.         }
  303.  
  304.     }
  305.  
  306.  
  307.     return false;
  308.  
  309. }
  310.