Advertisement
SilverTES

Tower Defense TEST

Apr 4th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.36 KB | None | 0 0
  1. #define ALLEGRO_STATICLINK
  2.  
  3. #include <iostream>
  4. #include <allegro.h>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. const int SCRX = 800;
  10. const int SCRY = 600;
  11.  
  12. SAMPLE * sample;
  13. SAMPLE * smpExplose;
  14. BITMAP * sprite0;
  15.  
  16. volatile int ticks=0;
  17. void ticker()
  18. {
  19.   ticks++;
  20. }
  21. END_OF_FUNCTION(ticker)
  22.  
  23. enum {TOWER, MONSTER0, BULLET0, BALL, EXPLOSION };
  24.  
  25. class Entity
  26. {
  27. protected:
  28.     bool visible = false;
  29.     bool active = true;
  30.     bool collidable = true;
  31.     bool dead = false;
  32.     int x = 0;
  33.     int y = 0;
  34.     int z = 0;
  35.     int type = 0;
  36.  
  37. public:
  38.  
  39.     Entity()
  40.     {
  41.         //cout << "+ Entity Created" << endl;
  42.     }
  43.     virtual ~Entity()
  44.     {
  45.         //cout << "- Entity Killed" << endl;
  46.     }
  47.     bool isDead ()
  48.     {
  49.         if (dead) return true;
  50.         return false;
  51.     }
  52.     int getX()
  53.     {
  54.         return x;
  55.     }
  56.     int getY()
  57.     {
  58.         return y;
  59.     }
  60.     void setX(int v)
  61.     {
  62.         x=v;
  63.     }
  64.     void setY(int v)
  65.     {
  66.         y=v;
  67.     }
  68.     void setPos(int vx, int vy)
  69.     {
  70.         x=vx;
  71.         y=vy;
  72.     }
  73.     virtual void Update() {}
  74.     virtual void Render(BITMAP * bmp);
  75.     static Entity * EntityFactory(int typeEntity);
  76.     static void addEntity(int type, int x = 0, int y = 0);
  77.     virtual void displayMember();
  78. };
  79.  
  80. vector<Entity*> vectorEntity;
  81.  
  82. class Tower : public Entity
  83. {
  84.     int hp = 0;
  85.     int mana = 0;
  86.     int reload = 80;
  87.     int tempo = 0;
  88.     int life = 50000;
  89.     bool shoot = false;
  90.  
  91. public:
  92.     Tower()
  93.     {
  94.         cout << "+ Tower Created" << endl;
  95.     }
  96.     virtual ~Tower()
  97.     {
  98.         cout << "- Tower Killed" << endl;
  99.     }
  100.     Tower(int hp, int mana)
  101.     {
  102.         this->hp = hp;
  103.         this->mana = mana;
  104.     }
  105.     void Update()
  106.     {
  107.         tempo++;
  108.         if (tempo<16)
  109.         {
  110.             x++; // fait avancer tour !
  111.             if (x>SCRX) dead = true;
  112.             shoot = false;
  113.         }
  114.         if (tempo>reload)
  115.         {
  116.             shoot=true;
  117.             Entity::addEntity(BULLET0, x, y);
  118.             //Entity::addEntity(EXPLOSION,x,y);
  119.             //cout << "...Bullet0 added successfully" << endl;
  120.             tempo = 0;
  121.         }
  122.         life--;
  123.         if (life<0) dead = true;
  124.     }
  125.     virtual void Render(BITMAP * bmp)
  126.     {
  127.         if (shoot) rectfill (bmp,x-16, y-16, x+16, y+16, makecol(230,2,20));
  128.         rectfill (bmp,x-8, y-8, x+8, y+8, makecol(130,30,0));
  129.         rect (bmp,x-8, y-8, x+8, y+8, makecol(230,230,200));
  130.  
  131.         //draw_sprite (bmp,sprite0,x-sprite0->w*0.5,y-sprite0->h*0.5);
  132.  
  133.         rectfill (bmp,x-8, y+12, x-8+(tempo*16/reload), y+14, makecol(0,230,0));
  134.         rect (bmp,x-8, y+12, x+8, y+14, makecol(30,130,10));
  135.     }
  136.  
  137. };
  138.  
  139. class Ball : public Entity
  140. {
  141.     int speedX = 1;
  142.     int speedY = 1;
  143.     int accelX = 0;
  144.     int accelY = 0;
  145.     int vecX = 1;
  146.     int vecY = 1;
  147.     int life = 400;
  148.     int vMax = 6;
  149.  
  150. public:
  151.     Ball()
  152.     {
  153.         cout << "+ Ball Created" << endl;
  154.     }
  155.     virtual ~Ball()
  156.     {
  157.         cout << "- Ball Killed" << endl;
  158.     }
  159.     void Update()
  160.     {
  161.         accelX += vecX;
  162.         accelY += vecY;
  163.         speedX = accelX;
  164.         speedY = accelY;
  165.         if (accelX>=vMax) accelX = vMax;
  166.         if (accelX<=-vMax) accelX = -vMax;
  167.  
  168.         if (accelY>vMax) accelY = vMax;
  169.         if (accelY<-vMax) accelY = -vMax;
  170.  
  171.         if (x >= SCRX) {speedX = -1;accelX=0;vecX=-1;}
  172.         if (x <= 0) {speedX = 1;accelX=0;vecX=1;}
  173.  
  174.         if (y >= SCRY) {speedY = -1;accelY=0;vecY=-1;}
  175.         if (y <= 0) {speedY = 1;accelY=0;vecY=1;}
  176.  
  177.         x+=speedX;
  178.         y+=speedY;
  179.  
  180.         life--;
  181.         if (life<0) dead = true;
  182.  
  183.     }
  184.     virtual void Render(BITMAP * bmp)
  185.     {
  186.         circlefill (bmp,x, y, 4, makecol(230,236,0));
  187.     }
  188. };
  189.  
  190. class Monster0 : public Entity
  191. {
  192.  
  193. public:
  194.     Monster0()
  195.     {
  196.         cout << "+ Monster0 Created" << endl;
  197.     }
  198.     virtual ~Monster0()
  199.     {
  200.         cout << "- Monster0 Killed" << endl;
  201.     }
  202. };
  203.  
  204. class Bullet0 : public Entity
  205. {
  206.     int speedX = 0;
  207.     int speedY = 0;
  208.     int tempo = 0;
  209.  
  210. public:
  211.     Bullet0()
  212.     {
  213.         cout << "+ Bullet0 Created" << endl;
  214.         play_sample(sample, 16, 128, 1000, FALSE);
  215.     }
  216.     virtual ~Bullet0()
  217.     {
  218.         cout << "- Bullet0 Killed" << endl;
  219.     }
  220.     void Update()
  221.     {
  222.         tempo++;
  223.         if (tempo>2)
  224.         {
  225.             speedX += 1;
  226.             tempo = 0;
  227.  
  228.         }
  229.         if (speedX> 16) speedX = 16;
  230.         x +=speedX;
  231.         if (x > SCRX-10)
  232.         {
  233.             Entity::addEntity(EXPLOSION,x,y);
  234.             dead = true;
  235.         }
  236.     }
  237.     virtual void Render(BITMAP * bmp)
  238.     {
  239.         rectfill (bmp, x-(speedX*3), y-1, x, y+1, makecol(250,210,5));
  240.         rect (bmp, x-(speedX*3), y-1, x, y+1, makecol(120,60,5));
  241.     }
  242. };
  243.  
  244. class Explosion : public Entity
  245. {
  246.     int speedX = 0;
  247.     int speedY = 0;
  248.     int life = 12;
  249.  
  250. public:
  251.     Explosion()
  252.     {
  253.         cout << "+ Explosion Created" << endl;
  254.         play_sample(smpExplose, 64, 128, 1000, FALSE);
  255.     }
  256.     virtual ~Explosion()
  257.     {
  258.         cout << "- Explosion Killed" << endl;
  259.     }
  260.     void Update()
  261.     {
  262.         life--;
  263.         if (life <0) dead = true;
  264.     }
  265.     virtual void Render(BITMAP * bmp)
  266.     {
  267.         circlefill (bmp, x, y, life, makecol(250,250,5));
  268.         circle (bmp, x, y, 2+life, makecol(250,240,255));
  269.     }
  270. };
  271.  
  272. void Entity::displayMember()
  273. {
  274.     cout << "----------------" << endl;
  275.     cout << " active  = " << active << endl ;
  276.     cout << " x       = " << x << endl ;
  277.     cout << " y       = " << y << endl ;
  278.     cout << " z       = " << z << endl ;
  279.     cout << " visible = " << visible << endl ;
  280.     cout << "----------------" << endl;
  281. }
  282.  
  283. void Entity::Render(BITMAP * bmp)
  284. {
  285.     circlefill (bmp,x, y, 8, makecol(230,56,67));
  286. }
  287.  
  288. Entity * Entity::EntityFactory(int typeEntity)
  289. {
  290.     Entity * ptr;
  291.     switch (typeEntity)
  292.     {
  293.     case TOWER:
  294.         ptr = new Tower();
  295.         //cout << "...Tower created successfully" << endl;
  296.         break;
  297.     case MONSTER0:
  298.         ptr = new Monster0();
  299.         //cout << "...Monster0 created successfully" << endl;
  300.         break;
  301.     case BULLET0:
  302.         ptr = new Bullet0();
  303.         //cout << "...Bullet0 created successfully" << endl;
  304.         break;
  305.     case BALL:
  306.         ptr = new Ball();
  307.         //cout << "...Ball created successfully" << endl;
  308.         break;
  309.     case EXPLOSION:
  310.         ptr = new Explosion();
  311.         //cout << "...Explosion created successfully" << endl;
  312.         break;
  313.     }
  314.  
  315.     return ptr;
  316. }
  317.  
  318. void Entity::addEntity(int type, int x, int y)
  319. {
  320.     Entity * pEntity = Entity::EntityFactory(type);
  321.     pEntity->setPos(x,y);
  322.     vectorEntity.push_back(pEntity);
  323.     //cout << "...Entity added successfully" << endl;
  324. }
  325.  
  326. int main(void)
  327. {
  328.     allegro_init();
  329.     set_gfx_mode(GFX_AUTODETECT_WINDOWED, SCRX, SCRY, 0, 0);
  330.     set_display_switch_mode(SWITCH_BACKGROUND);
  331.     set_color_depth(8);
  332.     install_keyboard();
  333.  
  334.     LOCK_VARIABLE(ticks);
  335.     LOCK_FUNCTION(ticker);
  336.  
  337.     //Finally we can install the timer, using either method.
  338.     //install_int(ticker,10);
  339.     install_int_ex(ticker, BPS_TO_TIMER(60));
  340.  
  341.     BITMAP * buffer = create_bitmap(SCRX,SCRY);
  342.  
  343.     //install a digital sound driver
  344.     if (install_sound(DIGI_AUTODETECT, MIDI_NONE, "") != 0)
  345.     {
  346.         allegro_message("Error initializing sound system");
  347.         return 1;
  348.     }
  349.  
  350.  
  351.     //load the wave file
  352.     sample = load_sample("LaserBlast.wav");
  353.     if (!sample)
  354.     {
  355.         allegro_message("Error reading wave file");
  356.         return 1;
  357.     }
  358.  
  359.     smpExplose = load_sample("Explosion.wav");
  360.     if (!smpExplose)
  361.     {
  362.         allegro_message("Error reading wave file");
  363.         return 1;
  364.     }
  365.  
  366.     PALLETE pal;
  367.     sprite0 = load_tga("jet.tga",pal);
  368.  
  369.     //set_palette(pal);
  370.  
  371.     for (int i(0); i<4; i++)
  372.     {
  373.         Entity::addEntity(BALL, i*32, i*16);
  374.     }
  375.  
  376.     bool keyB = false;
  377.     bool keyZ = false;
  378.     bool keyT = false;
  379.  
  380.     int px = 128;
  381.     int py = 128;
  382.     int speed = 8;
  383.  
  384.     int tileSize = 16;
  385.     int mapW = SCRX/tileSize;
  386.     int mapH = SCRY/tileSize;
  387.  
  388.     while(!key[KEY_ESC])
  389.     {
  390.  
  391.  
  392.         if (!key[KEY_B]) keyB = false;
  393.         if (!key[KEY_Z]) keyZ = false;
  394.         if (!key[KEY_T]) keyT = false;
  395.  
  396.         if (key[KEY_B] && !keyB) {Entity::addEntity(BALL,px,py);keyB=true;}
  397.         if (key[KEY_Z] && !keyZ) {Entity::addEntity(BULLET0,px,py);keyZ=true; play_sample(sample, 16, 128, 1000, FALSE);}
  398.         if (key[KEY_T] && !keyT) {Entity::addEntity(TOWER,px,py);keyT=true;}
  399.  
  400.         if (key[KEY_UP] && py>0)       py -= speed;
  401.         if (key[KEY_DOWN] && py<SCRY)  py += speed;
  402.         if (key[KEY_LEFT] && px>0)     px -= speed;
  403.         if (key[KEY_RIGHT] && px<SCRX) px += speed;
  404.  
  405.         if (key[KEY_BACKSPACE])
  406.         {
  407.             for (unsigned int i = 0; i < vectorEntity.size(); i++)
  408.             {
  409.                 delete vectorEntity[i];
  410.                 vectorEntity.erase(vectorEntity.begin()+i);
  411.             }
  412.         }
  413.  
  414.         clear_bitmap(buffer);
  415.  
  416.         ticks = 0;
  417.  
  418.         for (int j(0);j<mapH;j++)
  419.         {
  420.             for (int i(0);i<mapW;i++)
  421.             {
  422.                 rect (buffer,i*tileSize,j*tileSize,i*tileSize+tileSize,j*tileSize+tileSize,makecol(0,16,64));
  423.             }
  424.         }
  425.  
  426.         vline (buffer,px,0,SCRY,makecol(50,50,50));
  427.         hline (buffer,0,py,SCRX,makecol(50,50,50));
  428.  
  429.         for (unsigned int i = 0; i < vectorEntity.size(); i++)
  430.         {
  431.             vectorEntity[i]->Update();
  432.             vectorEntity[i]->Render(buffer);
  433.  
  434.             if (vectorEntity[i]->isDead())
  435.             {
  436.                 delete vectorEntity[i];
  437.                 vectorEntity.erase(vectorEntity.begin()+i);
  438.             }
  439.  
  440.         }
  441.  
  442. //        for (vector<Entity*>::iterator it = vectorEntity.begin() ; it != vectorEntity.end();)
  443. //        {
  444. ////            cout << "## Begin UPDATE ##" << endl;
  445. //
  446. //            (*it)->Update();
  447. //            (*it)->Render(buffer);
  448. //
  449. //            if ((*it)->isDead())
  450. //            {
  451. //                delete *it;
  452. //                it = vectorEntity.erase(it);
  453. //            }
  454. //            else
  455. //            {
  456. //                ++it;
  457. //            }
  458. //        }
  459.  
  460.  
  461.         rect (buffer,px-8, py-8, px+8, py+8, makecol(130,130,0));
  462.  
  463.         rectfill (buffer,16,SCRY-16,16+vectorEntity.size(),SCRY-24,makecol(250,120,0));
  464.  
  465.         textprintf_ex(buffer, font, 1, 1, 9, -1,"--- Alien total destruction ---");
  466.         textprintf_ex(buffer, font, 1, 12, 10, -1,"<T> TOWER <Z> LASER <B> BALL");
  467.  
  468.         textprintf_ex(buffer, font, 1, SCRY - 10, 11, -1,"Num Entity = %i", vectorEntity.size());
  469.  
  470.  
  471.  
  472.         blit (buffer, screen, 0, 0, 0, 0, SCRX, SCRY);
  473.  
  474.  
  475.         while(ticks < 1)
  476.         {
  477.             //rest(1);//rest until a full tick has passed
  478.         }
  479.  
  480.  
  481.  
  482.     }
  483.     for (unsigned int i = 0; i < vectorEntity.size(); i++)
  484.     {
  485.         delete vectorEntity[i];
  486.         vectorEntity.erase(vectorEntity.begin()+i);
  487.     }
  488. //    for (vector<Entity*>::iterator it = vectorEntity.begin() ; it != vectorEntity.end();++it)
  489. //    {
  490. //        delete *it;
  491. //        it = vectorEntity.erase(it);
  492. //    }
  493.  
  494.     vectorEntity.clear();
  495.     cout << vectorEntity.size();
  496.  
  497.     destroy_sample(sample);
  498.     destroy_sample(smpExplose);
  499.     destroy_bitmap(sprite0);
  500.     destroy_bitmap(buffer);
  501.     allegro_exit();
  502.     return 0;
  503. }
  504. END_OF_MAIN()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement