You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

175 lines
4.0 KiB

#include "..\C-Engine\CEngine.hpp"
#include "generateur.hpp"
#include "monstres.hpp"
MonsterPattern::MonsterPattern(char * vtag , Animation * vanim, int vanimnb, int vfreq, int vmax, int vidscript)
{
tag = vtag;
anim = vanim;
nbanim = vanimnb;
freq = vfreq;
max = vmax;
idscript = vidscript;
}
Generateur::Generateur()
{
M_nombre = 0;
M_Tag = NULL;
M_Anim = NULL;
M_Anim_nb = NULL;
M_Max = NULL;
M_Frequence = NULL;
active = false;
show = false;
}
void Generateur::ShowBox()
{
show = true;
}
void Generateur::SetBox(int x1 , int x2 , int y1 , int y2)
{
if(x1 > x2)
{
int b = x1;
x1 = x2;
x2 = b;
}
if(y1 > y2)
{
int c = y1;
y1 = y2;
y2 = c;
}
GetObject()->GetTransform()->SetXY(x1,y1);
GetObject()->AddRigidBody();
GetObject()->GetRigidBody()->UseFixeBody(x2-x1,y2-y1);
}
void Generateur::SetTab( int nbmonstre , MonsterPattern * tabpattern)
{
if(nbmonstre > 0 && tabpattern != NULL)
{
M_nombre = nbmonstre;
M_Tag = new char*[M_nombre];
M_Anim = new Animation*[M_nombre];
M_Anim_nb = new int[M_nombre];
M_Max = new int[M_nombre];
M_Frequence = new int[M_nombre];
M_Couldown = new int[M_nombre];
M_Id_Script = new int[M_nombre];
for(int i = 0; i < M_nombre ; i++ )
{
M_Tag[i] = tabpattern[i].tag;
M_Anim[i] = tabpattern[i].anim;
M_Anim_nb[i] = tabpattern[i].nbanim;
M_Max[i] = tabpattern[i].max;
M_Frequence[i] = tabpattern[i].freq;
M_Couldown[i] = 0;
M_Id_Script[i] = tabpattern[i].idscript;
}
}
}
void Generateur::Update()
{
if(GetObject()->GetCollisionTag("Perso"))active = true;
else active = false;
if(show)
{
GetEngine()->UpdateRelativePosition();
ML_rectangle(GetObject()->GetTransform()->GetRelativeX(),63 - GetObject()->GetTransform()->GetRelativeY(),GetObject()->GetTransform()->GetRelativeX() + GetObject()->GetRigidBody()->GetWidht() , 63 - GetObject()->GetTransform()->GetRelativeY() - GetObject()->GetRigidBody()->GetHeight(),1, ML_BLACK , ML_WHITE);
}
}
int Generateur::RandX()
{
int min = GetObject()->GetTransform()->GetX();
int max = GetObject()->GetTransform()->GetX() + GetObject()->GetRigidBody()->GetWidht();
return rand_int_ab( min , max );
}
int Generateur::RandY()
{
int min = GetObject()->GetTransform()->GetY();
int max = GetObject()->GetTransform()->GetY() + GetObject()->GetRigidBody()->GetHeight();
return rand_int_ab( min , max );
}
void Generateur::CreateMonster(int id)
{
Object * Buffer = new Object;
Buffer->GetTransform()->SetXY( RandX() , RandY() );
Buffer->AffectTag(M_Tag[id]);
Buffer->GetRender()->SetRender(M_Anim[id], M_Anim_nb[id]);
Buffer->AddRigidBody();
switch(M_Id_Script[id])
{
case 0:
IaSlime * ScriptBuffer = new IaSlime;
Buffer->AffectScript(ScriptBuffer);
break;
case 1:
break;
default: break;
}
GetEngine()->AddObject(Buffer);
while(Buffer->GetRigidBody()->CollisionDecor(Buffer->GetTransform()->GetX(),Buffer->GetTransform()->GetY()))
{
Buffer->GetTransform()->SetXY( RandX() , RandY() );
}
}
void Generateur::UpdateTimer(int id)
{
M_Couldown[id]++;
if(M_Couldown[id] > 60 / M_Frequence[id])
{
CreateMonster(id);
M_Couldown[id] = 0;
}
}
void Generateur::UpdateEverySecond()
{
if(active)
{
for(int i = 0; i < M_nombre; i++)
{
if(CheckNbMonster(i) < M_Max[i])UpdateTimer(i);
}
}
}
int Generateur::CheckNbMonster( int id)
{
int nbmonster = 0;
for(int i = 0; i < GetEngine()->GetNbObject();i++)
{
if(strcmp(GetEngine()->GetListeObject()[i]->GetTag() , M_Tag[id]) == 0 && GetEngine()->GetListeObject()[i]->GetEnable() == true)
{
if(GetObject()->Collision(i))nbmonster++;
}
}
return nbmonster;
}