Ecco il codice:
//Le entità sono di tipo 'Entity'
class Entity {
public:
int action = codeStill;
std::vector <SDL_Texture*> Still;
std::vector <SDL_Texture*> Attack;
std::vector <SDL_Texture*> Hurt;
SDL_Rect rect = { 0,0, 200,200 };
int anim_state = 0;
bool oriented = true; //true=destra, false=sinistra
int delay[3] = { 500,100 ,200};
int NumberOfSprites[3] = { 1, 2, 2 };
int speed = 3;
int range = 10;
int Damage;
int HP;
int full_HP;
int ID;
std::vector <int> Spawn_point = {};
};
//'First' unica entità al momento esistente.
Entity First() {
Entity obj;
extern SDL_Renderer *screen;
extern std::vector <Entity> All_Entities;
//STILL Sprite
SDL_Surface *img = IMG_Load("./Sprites/First/still1.png");
SDL_Texture *texture = SDL_CreateTextureFromSurface(screen, img);
obj.Still.push_back(texture);
img = IMG_Load("./Sprites/First/still2.png");
texture = SDL_CreateTextureFromSurface(screen, img);
obj.Still.push_back(texture);
//ATTACK Sprite
img = IMG_Load("./Sprites/First/Attack1.png");
texture = SDL_CreateTextureFromSurface(screen, img);
obj.Attack.push_back(texture);
img = IMG_Load("./Sprites/First/Attack2.png");
texture = SDL_CreateTextureFromSurface(screen, img);
obj.Attack.push_back(texture);
img = IMG_Load("./Sprites/First/Attack3.png");
texture = SDL_CreateTextureFromSurface(screen, img);
obj.Attack.push_back(texture);
obj.Hurt = obj.Attack;
obj.NumberOfSprites[codeAttack] = 2;
obj.action = codeStill;
All_Entities.push_back(obj);
obj.rect = { 1300,500,70,100 };
obj.speed = 2;
obj.Damage = 2;
obj.HP = 100;
obj.full_HP = 100;
obj.Spawn_point = { (((rand() % 150 + 110)+2)/3)*3, 120, 120 };
return obj;
}
//Questa funzione controlla un'entità la cui posizione nel vettore 'All_Entitites' è indicata dall'argomento 'Check'
//'int code' indica l'ID dell'entità, per facilitarmi la vita in altre funzioni.
void Animation_Entities(int Check, int code) {
extern std::vector <Entity> All_Entities;
extern bool done;
extern int Level;
extern SDL_Rect wallpaper_pos;
while (!done) {
bool stop = true;
while (stop == true) {
SDL_Delay(10);
switch (Level) {
case 0:
if (wallpaper_pos.x == All_Entities.at(Check).Spawn_point[Level]) {
stop = false;
All_Entities.at(Check) = First();
break;
}
}
}
while (All_Entities.at(Check).HP > 0) {
if (All_Entities.at(Check).anim_state != All_Entities.at(Check).NumberOfSprites[All_Entities.at(Check).action]) {
All_Entities.at(Check).anim_state++;
}
SDL_Delay(All_Entities.at(Check).delay[All_Entities.at(Check).action]);
}
if (!All_Entities.at(Check).ID == code) {
break;
}
}
}
//Qui dovrebbe indicare quali entità sono presenti nei vari livelli.
void UpdateLevel() {
extern int Level;
extern std::vector <Entity> All_Entities;
if (Level == 0) {
Entity NewEntity = First();
NewEntity.ID = 1;
NewEntity.HP = 0;
All_Entities.push_back(NewEntity);
std::thread First_Animation(Animation_Entities, 1, 1); //gestione sprite Prima entità
NewEntity = First();
NewEntity.ID = 2;
NewEntity.HP = 0;
vector <Entity>::iterator iterator = All_Entities.begin();
All_Entities.insert(iterator + 2, NewEntity);
std::thread Second_Animation(Animation_Entities, 2, 2); /gestione Sprite seconda entità
}
}