Advertisement
SilverTES

Test Factory Entity STL

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