Yawin

Asteroids (problema colisiones)

Jul 17th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.60 KB | None | 0 0
  1. #include "stdlib.h"
  2. #include "miniwin.h"
  3. #include <cmath>#include <cstdlib>
  4. #include <ctime>
  5. #include <list>
  6. using namespace miniwin;
  7. using namespace std;
  8.  
  9. const int ANCHO=1280;
  10. const int ALTO=720;
  11.  
  12. struct Coord
  13. {
  14.     int x,y;
  15.     Coord(int x0=0,int y0=0){x=x0; y=y0;}
  16.     Coord(const Coord& c){x=c.x; y=c.y;}
  17. };
  18. struct Polar
  19. {
  20.     int ro;
  21.     float angle;
  22.     Polar(int ro0=0,float ang=0.00){ro=ro0; angle=ang;}
  23.     Polar(const Polar& p){ro=p.ro; angle=p.angle;}
  24. };
  25.  
  26. class Pieza
  27. {
  28.     private:
  29.         Coord orig,veloc;   //Coordenadas de origen de la pieza, Velocidad en 'X' y en 'Y';
  30.         float angulo;       //Ángulo de orientación de la pieza;
  31.         Polar *perif;       //Puntero a tabla (alojada en el heap) con los puntos
  32.                             //periféricos de la pieza en coordenadas polares;
  33.         int tam,esc;        //Tamaño de la array de periféricos y escala de la pieza (en %);
  34.         bool colision;      //Indica si está en colisión
  35.  
  36.     public:
  37.         //Constructores
  38.             Pieza(int x0=0,int y0=0,int ang=0,int p=0,int e=100,Coord v={0,0})
  39.             {
  40.                 orig.x=x0;
  41.                 orig.y=y0;
  42.                 angulo=float(ang);
  43.                 tam=p;
  44.                 perif=new Polar[tam];
  45.                 veloc=v;
  46.                 esc=e;
  47.             }
  48.  
  49.         //Setters y getters
  50.             Coord coords(){return orig;}
  51.             void coords(Coord& c){ orig=c; }
  52.             //int coords(char c){return c=='x'? orig.x : (c=='y'? orig.y : -1);}
  53.  
  54.             void x (int c) { orig.x=c;      }
  55.              int x (     ) { return orig.x; }
  56.  
  57.             void y (int c) { orig.y=c;      }
  58.              int y (     ) { return orig.y; }
  59.  
  60.              int angle ( ) { return int(angulo);}
  61.             void angle (int ang) {angulo=float(ang);}
  62.  
  63.             int periferico(){return tam;}
  64.             Polar periferico(int id){return id<tam ? perif[id]:-1;}
  65.             void periferico(int id,Polar valor){ id<tam ? perif[id]=valor:-1;}
  66.  
  67.             Coord velocidad (       ) { return veloc;}
  68.              void velocidad (Coord v) {veloc=v;}
  69.  
  70.              int escala (       ) { return esc;}
  71.             void escala (int e) {esc=e;}
  72.  
  73.             bool ha_chocado (      ) { return colision;}
  74.             void ha_chocado (bool c) {colision=c;}
  75.  
  76.         void rota(int r)
  77.         {
  78.             angulo+=float(r);
  79.         }
  80.         void acelera()
  81.         {
  82.             Polar aux=Polar(2,angulo/180*M_PI);
  83.             veloc.x+=aux.ro*cos(aux.angle);
  84.             veloc.y+=aux.ro*sin(aux.angle);
  85.         }
  86.         void avanza()
  87.         {
  88.             orig.x+=veloc.x;
  89.             orig.y+=veloc.y;
  90.  
  91.             if(orig.x<0) {orig.x=ANCHO;}
  92.             else if(orig.x>ANCHO) {orig.x=0;}
  93.  
  94.             if(orig.y<0) {orig.y=ALTO;}
  95.             else if(orig.y>ALTO) {orig.y=0;}
  96.         }
  97.  
  98.         void pinta() const
  99.         {
  100.             Polar aux[tam];
  101.             for(int i=0;i<tam;i++)
  102.             {
  103.                 aux[i].ro=perif[i].ro*(esc/100);
  104.                 aux[i].angle=(perif[i].angle+angulo)/180*M_PI;
  105.             }
  106.  
  107.             float xy[tam][2];
  108.             for(int i=0;i<tam;i++)
  109.             {
  110.                 xy[i][0]=float(orig.x+aux[i].ro*cos(aux[i].angle));
  111.                 xy[i][1]=float(orig.y+aux[i].ro*sin(aux[i].angle));
  112.             }
  113.  
  114.             if(tam>1)
  115.             {
  116.                 for(int i=0;i<tam-1;i++)
  117.                 {
  118.                     linea(xy[i][0],xy[i][1],xy[i+1][0],xy[i+1][1]);
  119.                 }
  120.                 linea(xy[tam-1][0],xy[tam-1][1],xy[0][0],xy[0][1]);
  121.             }
  122.         }
  123.  
  124.         virtual void actualiza(){};
  125. };
  126.  
  127. class Nave: public Pieza
  128. {
  129.     public:
  130.         //Constructores
  131.             Nave(int x0=0,int y0=0,int ang=0):Pieza(x0,y0,ang,4,100,Coord(0,0))
  132.             {
  133.                 periferico(0,Polar(20,225.00));
  134.                 periferico(1,Polar(32,0.00));
  135.                 periferico(2,Polar(20,135.00));
  136.                 periferico(3,Polar(5,180.00));
  137.             }
  138.  
  139.         virtual void actualiza()
  140.         {
  141.             if(ha_chocado())
  142.             {
  143.                 //Se muere
  144.             }
  145.  
  146.             avanza();
  147.             pinta();
  148.         }
  149. };
  150.  
  151. class Asteroide: public Pieza
  152. {
  153.     private:
  154.         int r;
  155.  
  156.     public:
  157.         //Constructores
  158.             Asteroide():Pieza(-100,-100,0,9,2000,Coord(0,0))
  159.             {
  160.                 periferico(0,Polar(4,45.00));
  161.                 periferico(1,Polar(4,90.00));
  162.                 periferico(2,Polar(4,135.00));
  163.                 periferico(3,Polar(4,180.00));
  164.                 periferico(4,Polar(4,225.00));
  165.                 periferico(5,Polar(4,270.00));
  166.                 periferico(6,Polar(4,315.00));
  167.                 periferico(7,Polar(1,337));
  168.                 periferico(8,Polar(4,360.00));
  169.  
  170.                 Coord c,p;
  171.                 c=Coord(rand()%ANCHO,rand()%ALTO);
  172.                 p=Coord((rand()%4)-2,(rand()%4)-2);
  173.                 coords(c);
  174.                 velocidad(p);
  175.  
  176.                 r=rand()%2;
  177.             }
  178.  
  179.         virtual void actualiza(Pieza& nave)
  180.         {
  181.             if(ha_chocado())
  182.             {
  183.                 //Se muere
  184.             }
  185.  
  186.             rota(r>0?1:-1);
  187.             avanza();
  188.             pinta();
  189.  
  190.             if(colisiona(nave))
  191.             {
  192.                 ha_chocado(true);
  193.                 nave.ha_chocado(true);
  194.             }
  195.         }
  196.  
  197.         bool colisiona(Pieza& nave)
  198.         {
  199.             //Se calcula si el asteroide choca contra la nave
  200.             return true;
  201.         }
  202. };
  203.  
  204. //Utilidades y globales
  205.     list<Asteroide*> A;
  206.     list<Asteroide*>::iterator Ait;
  207.  
  208.     void frame(Nave& nave)
  209.     {
  210.         borra();
  211.         nave.actualiza();
  212.  
  213.         for(Ait=A.begin();Ait!=A.end();Ait++)
  214.         {
  215.             (*Ait)->actualiza(nave);
  216.         }
  217.  
  218.         refresca();
  219.     }
  220. //End utilidades y globales
  221.  
  222. int main()
  223. {
  224.     srand(time(0)); //Ponemos la semilla
  225.  
  226.     vredimensiona(ANCHO, ALTO);
  227.     Nave nave(ANCHO/2,ALTO/2,270);
  228.  
  229.     for(int i=0;i<5;i++)
  230.     {
  231.         A.push_back(new Asteroide());
  232.     }
  233.  
  234.     frame(nave);
  235.  
  236.     int t;
  237.     do
  238.     {
  239.         switch(t)
  240.         {
  241.             case IZQUIERDA:
  242.                 nave.rota(-3);
  243.                 break;
  244.             case DERECHA:
  245.                 nave.rota(3);
  246.                 break;
  247.             case ARRIBA:
  248.                 nave.acelera();
  249.                 break;
  250.             case ABAJO:
  251.                 break;
  252.         }
  253.  
  254.         frame(nave);
  255.  
  256.         espera(30);
  257.         t=tecla();
  258.     }while(t!=ESCAPE);
  259.  
  260.     refresca();
  261.     vcierra();
  262.     return 0;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment