Advertisement
eliasYFGM

Juego de plataformas en C++/Allegro (Nivel.cpp)

Jul 21st, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. // Nivel.cpp
  2. // Base para juego de plataformas en C++ y Allegro
  3.  
  4. #include <vector>
  5.  
  6. #include <allegro5/allegro_primitives.h>
  7.  
  8. #include "Nivel.h"
  9. #include "Juego.h"
  10.  
  11.  
  12.  
  13. namespace {
  14.  
  15. // Las plataformas se almacenarán en un vector después de crear el nivel.
  16. std::vector<Plataforma*> plataformas;
  17.  
  18. } // namespace
  19.  
  20.  
  21.  
  22. void Crear_Nivel(const char *cadena, float& inicio_x, float& inicio_y)
  23. {
  24.    Destruir_Nivel();
  25.  
  26.    int x = 0, y = 0;
  27.  
  28.    // Buscar el jugador y las plataformas en la cadena
  29.    for (const char *pos = cadena; *pos != '\0'; ++pos)
  30.    {  if (*pos == 'J')
  31.       {  inicio_x = x * 32;
  32.          inicio_y = y * 32;
  33.       }
  34.       else if (*pos == 'X')
  35.          plataformas.push_back(new Plataforma(x * 32, y * 32));
  36.  
  37.       ++x;
  38.  
  39.       if (x == TAMANO_NIVEL_X)
  40.       {  ++y;
  41.          x = 0;
  42.       }
  43.    }
  44. }
  45.  
  46.  
  47.  
  48. void Dibujar_Nivel()
  49. {
  50.    for (auto i : plataformas)
  51.       al_draw_filled_rectangle(i->x, i->y, i->x + 32, i->y + 32, C_BLANCO);
  52. }
  53.  
  54.  
  55.  
  56. void Destruir_Nivel()
  57. {
  58.    for (auto i : plataformas)
  59.       delete i;
  60.  
  61.    plataformas.clear();
  62. }
  63.  
  64.  
  65.  
  66. Plataforma *Checar_Plataforma(float _x, float _y)
  67. {
  68.    for (auto i : plataformas)
  69.    {  if (bbox_collision(_x, _y, TAMANO_JUGADOR_X, TAMANO_JUGADOR_Y, i->x, i->y
  70.                          , 32, 32))
  71.          return i;
  72.    }
  73.  
  74.    return nullptr;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement