Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ALLEGRO_STATICLINK
- #include <iostream>
- #include <allegro.h>
- #include <vector>
- using namespace std;
- const int SCRX = 800;
- const int SCRY = 600;
- SAMPLE * sample;
- SAMPLE * smpExplose;
- BITMAP * sprite0;
- volatile int ticks=0;
- void ticker()
- {
- ticks++;
- }
- END_OF_FUNCTION(ticker)
- enum {TOWER, MONSTER0, BULLET0, BALL, EXPLOSION };
- class Entity
- {
- protected:
- bool visible = false;
- bool active = true;
- bool collidable = true;
- bool dead = false;
- int x = 0;
- int y = 0;
- int z = 0;
- int type = 0;
- public:
- Entity()
- {
- //cout << "+ Entity Created" << endl;
- }
- virtual ~Entity()
- {
- //cout << "- Entity Killed" << endl;
- }
- bool isDead ()
- {
- if (dead) return true;
- return false;
- }
- int getX()
- {
- return x;
- }
- int getY()
- {
- return y;
- }
- void setX(int v)
- {
- x=v;
- }
- void setY(int v)
- {
- y=v;
- }
- void setPos(int vx, int vy)
- {
- x=vx;
- y=vy;
- }
- virtual void Update() {}
- virtual void Render(BITMAP * bmp);
- static Entity * EntityFactory(int typeEntity);
- static void addEntity(int type, int x = 0, int y = 0);
- virtual void displayMember();
- };
- vector<Entity*> vectorEntity;
- class Tower : public Entity
- {
- int hp = 0;
- int mana = 0;
- int reload = 80;
- int tempo = 0;
- int life = 50000;
- bool shoot = false;
- public:
- Tower()
- {
- cout << "+ Tower Created" << endl;
- }
- virtual ~Tower()
- {
- cout << "- Tower Killed" << endl;
- }
- Tower(int hp, int mana)
- {
- this->hp = hp;
- this->mana = mana;
- }
- void Update()
- {
- tempo++;
- if (tempo<16)
- {
- x++; // fait avancer tour !
- if (x>SCRX) dead = true;
- shoot = false;
- }
- if (tempo>reload)
- {
- shoot=true;
- Entity::addEntity(BULLET0, x, y);
- //Entity::addEntity(EXPLOSION,x,y);
- //cout << "...Bullet0 added successfully" << endl;
- tempo = 0;
- }
- life--;
- if (life<0) dead = true;
- }
- virtual void Render(BITMAP * bmp)
- {
- if (shoot) rectfill (bmp,x-16, y-16, x+16, y+16, makecol(230,2,20));
- rectfill (bmp,x-8, y-8, x+8, y+8, makecol(130,30,0));
- rect (bmp,x-8, y-8, x+8, y+8, makecol(230,230,200));
- //draw_sprite (bmp,sprite0,x-sprite0->w*0.5,y-sprite0->h*0.5);
- rectfill (bmp,x-8, y+12, x-8+(tempo*16/reload), y+14, makecol(0,230,0));
- rect (bmp,x-8, y+12, x+8, y+14, makecol(30,130,10));
- }
- };
- class Ball : public Entity
- {
- int speedX = 1;
- int speedY = 1;
- int accelX = 0;
- int accelY = 0;
- int vecX = 1;
- int vecY = 1;
- int life = 400;
- int vMax = 6;
- public:
- Ball()
- {
- cout << "+ Ball Created" << endl;
- }
- virtual ~Ball()
- {
- cout << "- Ball Killed" << endl;
- }
- void Update()
- {
- accelX += vecX;
- accelY += vecY;
- speedX = accelX;
- speedY = accelY;
- if (accelX>=vMax) accelX = vMax;
- if (accelX<=-vMax) accelX = -vMax;
- if (accelY>vMax) accelY = vMax;
- if (accelY<-vMax) accelY = -vMax;
- if (x >= SCRX) {speedX = -1;accelX=0;vecX=-1;}
- if (x <= 0) {speedX = 1;accelX=0;vecX=1;}
- if (y >= SCRY) {speedY = -1;accelY=0;vecY=-1;}
- if (y <= 0) {speedY = 1;accelY=0;vecY=1;}
- x+=speedX;
- y+=speedY;
- life--;
- if (life<0) dead = true;
- }
- virtual void Render(BITMAP * bmp)
- {
- circlefill (bmp,x, y, 4, makecol(230,236,0));
- }
- };
- class Monster0 : public Entity
- {
- public:
- Monster0()
- {
- cout << "+ Monster0 Created" << endl;
- }
- virtual ~Monster0()
- {
- cout << "- Monster0 Killed" << endl;
- }
- };
- class Bullet0 : public Entity
- {
- int speedX = 0;
- int speedY = 0;
- int tempo = 0;
- public:
- Bullet0()
- {
- cout << "+ Bullet0 Created" << endl;
- play_sample(sample, 16, 128, 1000, FALSE);
- }
- virtual ~Bullet0()
- {
- cout << "- Bullet0 Killed" << endl;
- }
- void Update()
- {
- tempo++;
- if (tempo>2)
- {
- speedX += 1;
- tempo = 0;
- }
- if (speedX> 16) speedX = 16;
- x +=speedX;
- if (x > SCRX-10)
- {
- Entity::addEntity(EXPLOSION,x,y);
- dead = true;
- }
- }
- virtual void Render(BITMAP * bmp)
- {
- rectfill (bmp, x-(speedX*3), y-1, x, y+1, makecol(250,210,5));
- rect (bmp, x-(speedX*3), y-1, x, y+1, makecol(120,60,5));
- }
- };
- class Explosion : public Entity
- {
- int speedX = 0;
- int speedY = 0;
- int life = 12;
- public:
- Explosion()
- {
- cout << "+ Explosion Created" << endl;
- play_sample(smpExplose, 64, 128, 1000, FALSE);
- }
- virtual ~Explosion()
- {
- cout << "- Explosion Killed" << endl;
- }
- void Update()
- {
- life--;
- if (life <0) dead = true;
- }
- virtual void Render(BITMAP * bmp)
- {
- circlefill (bmp, x, y, life, makecol(250,250,5));
- circle (bmp, x, y, 2+life, makecol(250,240,255));
- }
- };
- void Entity::displayMember()
- {
- cout << "----------------" << endl;
- cout << " active = " << active << endl ;
- cout << " x = " << x << endl ;
- cout << " y = " << y << endl ;
- cout << " z = " << z << endl ;
- cout << " visible = " << visible << endl ;
- cout << "----------------" << endl;
- }
- void Entity::Render(BITMAP * bmp)
- {
- circlefill (bmp,x, y, 8, makecol(230,56,67));
- }
- Entity * Entity::EntityFactory(int typeEntity)
- {
- Entity * ptr;
- switch (typeEntity)
- {
- case TOWER:
- ptr = new Tower();
- //cout << "...Tower created successfully" << endl;
- break;
- case MONSTER0:
- ptr = new Monster0();
- //cout << "...Monster0 created successfully" << endl;
- break;
- case BULLET0:
- ptr = new Bullet0();
- //cout << "...Bullet0 created successfully" << endl;
- break;
- case BALL:
- ptr = new Ball();
- //cout << "...Ball created successfully" << endl;
- break;
- case EXPLOSION:
- ptr = new Explosion();
- //cout << "...Explosion created successfully" << endl;
- break;
- }
- return ptr;
- }
- void Entity::addEntity(int type, int x, int y)
- {
- Entity * pEntity = Entity::EntityFactory(type);
- pEntity->setPos(x,y);
- vectorEntity.push_back(pEntity);
- //cout << "...Entity added successfully" << endl;
- }
- int main(void)
- {
- allegro_init();
- set_gfx_mode(GFX_AUTODETECT_WINDOWED, SCRX, SCRY, 0, 0);
- set_display_switch_mode(SWITCH_BACKGROUND);
- set_color_depth(8);
- install_keyboard();
- LOCK_VARIABLE(ticks);
- LOCK_FUNCTION(ticker);
- //Finally we can install the timer, using either method.
- //install_int(ticker,10);
- install_int_ex(ticker, BPS_TO_TIMER(60));
- BITMAP * buffer = create_bitmap(SCRX,SCRY);
- //install a digital sound driver
- if (install_sound(DIGI_AUTODETECT, MIDI_NONE, "") != 0)
- {
- allegro_message("Error initializing sound system");
- return 1;
- }
- //load the wave file
- sample = load_sample("LaserBlast.wav");
- if (!sample)
- {
- allegro_message("Error reading wave file");
- return 1;
- }
- smpExplose = load_sample("Explosion.wav");
- if (!smpExplose)
- {
- allegro_message("Error reading wave file");
- return 1;
- }
- PALLETE pal;
- sprite0 = load_tga("jet.tga",pal);
- //set_palette(pal);
- for (int i(0); i<4; i++)
- {
- Entity::addEntity(BALL, i*32, i*16);
- }
- bool keyB = false;
- bool keyZ = false;
- bool keyT = false;
- int px = 128;
- int py = 128;
- int speed = 8;
- int tileSize = 16;
- int mapW = SCRX/tileSize;
- int mapH = SCRY/tileSize;
- while(!key[KEY_ESC])
- {
- if (!key[KEY_B]) keyB = false;
- if (!key[KEY_Z]) keyZ = false;
- if (!key[KEY_T]) keyT = false;
- if (key[KEY_B] && !keyB) {Entity::addEntity(BALL,px,py);keyB=true;}
- if (key[KEY_Z] && !keyZ) {Entity::addEntity(BULLET0,px,py);keyZ=true; play_sample(sample, 16, 128, 1000, FALSE);}
- if (key[KEY_T] && !keyT) {Entity::addEntity(TOWER,px,py);keyT=true;}
- if (key[KEY_UP] && py>0) py -= speed;
- if (key[KEY_DOWN] && py<SCRY) py += speed;
- if (key[KEY_LEFT] && px>0) px -= speed;
- if (key[KEY_RIGHT] && px<SCRX) px += speed;
- if (key[KEY_BACKSPACE])
- {
- for (unsigned int i = 0; i < vectorEntity.size(); i++)
- {
- delete vectorEntity[i];
- vectorEntity.erase(vectorEntity.begin()+i);
- }
- }
- clear_bitmap(buffer);
- ticks = 0;
- for (int j(0);j<mapH;j++)
- {
- for (int i(0);i<mapW;i++)
- {
- rect (buffer,i*tileSize,j*tileSize,i*tileSize+tileSize,j*tileSize+tileSize,makecol(0,16,64));
- }
- }
- vline (buffer,px,0,SCRY,makecol(50,50,50));
- hline (buffer,0,py,SCRX,makecol(50,50,50));
- for (unsigned int i = 0; i < vectorEntity.size(); i++)
- {
- vectorEntity[i]->Update();
- vectorEntity[i]->Render(buffer);
- if (vectorEntity[i]->isDead())
- {
- delete vectorEntity[i];
- vectorEntity.erase(vectorEntity.begin()+i);
- }
- }
- // for (vector<Entity*>::iterator it = vectorEntity.begin() ; it != vectorEntity.end();)
- // {
- //// cout << "## Begin UPDATE ##" << endl;
- //
- // (*it)->Update();
- // (*it)->Render(buffer);
- //
- // if ((*it)->isDead())
- // {
- // delete *it;
- // it = vectorEntity.erase(it);
- // }
- // else
- // {
- // ++it;
- // }
- // }
- rect (buffer,px-8, py-8, px+8, py+8, makecol(130,130,0));
- rectfill (buffer,16,SCRY-16,16+vectorEntity.size(),SCRY-24,makecol(250,120,0));
- textprintf_ex(buffer, font, 1, 1, 9, -1,"--- Alien total destruction ---");
- textprintf_ex(buffer, font, 1, 12, 10, -1,"<T> TOWER <Z> LASER <B> BALL");
- textprintf_ex(buffer, font, 1, SCRY - 10, 11, -1,"Num Entity = %i", vectorEntity.size());
- blit (buffer, screen, 0, 0, 0, 0, SCRX, SCRY);
- while(ticks < 1)
- {
- //rest(1);//rest until a full tick has passed
- }
- }
- for (unsigned int i = 0; i < vectorEntity.size(); i++)
- {
- delete vectorEntity[i];
- vectorEntity.erase(vectorEntity.begin()+i);
- }
- // for (vector<Entity*>::iterator it = vectorEntity.begin() ; it != vectorEntity.end();++it)
- // {
- // delete *it;
- // it = vectorEntity.erase(it);
- // }
- vectorEntity.clear();
- cout << vectorEntity.size();
- destroy_sample(sample);
- destroy_sample(smpExplose);
- destroy_bitmap(sprite0);
- destroy_bitmap(buffer);
- allegro_exit();
- return 0;
- }
- END_OF_MAIN()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement