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.

generateur.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "..\C-Engine\CEngine.hpp"
  2. #include "generateur.hpp"
  3. #include "monstres.hpp"
  4. MonsterPattern::MonsterPattern(char * vtag , Animation * vanim, int vanimnb, int vfreq, int vmax, int vidscript)
  5. {
  6. tag = vtag;
  7. anim = vanim;
  8. nbanim = vanimnb;
  9. freq = vfreq;
  10. max = vmax;
  11. idscript = vidscript;
  12. }
  13. Generateur::Generateur()
  14. {
  15. M_nombre = 0;
  16. M_Tag = NULL;
  17. M_Anim = NULL;
  18. M_Anim_nb = NULL;
  19. M_Max = NULL;
  20. M_Frequence = NULL;
  21. active = false;
  22. show = false;
  23. }
  24. void Generateur::ShowBox()
  25. {
  26. show = true;
  27. }
  28. void Generateur::SetBox(int x1 , int x2 , int y1 , int y2)
  29. {
  30. if(x1 > x2)
  31. {
  32. int b = x1;
  33. x1 = x2;
  34. x2 = b;
  35. }
  36. if(y1 > y2)
  37. {
  38. int c = y1;
  39. y1 = y2;
  40. y2 = c;
  41. }
  42. GetObject()->GetTransform()->SetXY(x1,y1);
  43. GetObject()->AddRigidBody();
  44. GetObject()->GetRigidBody()->UseFixeBody(x2-x1,y2-y1);
  45. }
  46. void Generateur::SetTab( int nbmonstre , MonsterPattern * tabpattern)
  47. {
  48. if(nbmonstre > 0 && tabpattern != NULL)
  49. {
  50. M_nombre = nbmonstre;
  51. M_Tag = new char*[M_nombre];
  52. M_Anim = new Animation*[M_nombre];
  53. M_Anim_nb = new int[M_nombre];
  54. M_Max = new int[M_nombre];
  55. M_Frequence = new int[M_nombre];
  56. M_Couldown = new int[M_nombre];
  57. M_Id_Script = new int[M_nombre];
  58. for(int i = 0; i < M_nombre ; i++ )
  59. {
  60. M_Tag[i] = tabpattern[i].tag;
  61. M_Anim[i] = tabpattern[i].anim;
  62. M_Anim_nb[i] = tabpattern[i].nbanim;
  63. M_Max[i] = tabpattern[i].max;
  64. M_Frequence[i] = tabpattern[i].freq;
  65. M_Couldown[i] = 0;
  66. M_Id_Script[i] = tabpattern[i].idscript;
  67. }
  68. }
  69. }
  70. void Generateur::Update()
  71. {
  72. if(GetObject()->GetCollisionTag("Perso"))active = true;
  73. else active = false;
  74. if(show)
  75. {
  76. GetEngine()->UpdateRelativePosition();
  77. 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);
  78. }
  79. }
  80. int Generateur::RandX()
  81. {
  82. int min = GetObject()->GetTransform()->GetX();
  83. int max = GetObject()->GetTransform()->GetX() + GetObject()->GetRigidBody()->GetWidht();
  84. return rand_int_ab( min , max );
  85. }
  86. int Generateur::RandY()
  87. {
  88. int min = GetObject()->GetTransform()->GetY();
  89. int max = GetObject()->GetTransform()->GetY() + GetObject()->GetRigidBody()->GetHeight();
  90. return rand_int_ab( min , max );
  91. }
  92. void Generateur::CreateMonster(int id)
  93. {
  94. Object * Buffer = new Object;
  95. Buffer->GetTransform()->SetXY( RandX() , RandY() );
  96. Buffer->AffectTag(M_Tag[id]);
  97. Buffer->GetRender()->SetRender(M_Anim[id], M_Anim_nb[id]);
  98. Buffer->AddRigidBody();
  99. switch(M_Id_Script[id])
  100. {
  101. case 0:
  102. IaSlime * ScriptBuffer = new IaSlime;
  103. Buffer->AffectScript(ScriptBuffer);
  104. break;
  105. case 1:
  106. break;
  107. default: break;
  108. }
  109. GetEngine()->AddObject(Buffer);
  110. while(Buffer->GetRigidBody()->CollisionDecor(Buffer->GetTransform()->GetX(),Buffer->GetTransform()->GetY()))
  111. {
  112. Buffer->GetTransform()->SetXY( RandX() , RandY() );
  113. }
  114. }
  115. void Generateur::UpdateTimer(int id)
  116. {
  117. M_Couldown[id]++;
  118. if(M_Couldown[id] > 60 / M_Frequence[id])
  119. {
  120. CreateMonster(id);
  121. M_Couldown[id] = 0;
  122. }
  123. }
  124. void Generateur::UpdateEverySecond()
  125. {
  126. if(active)
  127. {
  128. for(int i = 0; i < M_nombre; i++)
  129. {
  130. if(CheckNbMonster(i) < M_Max[i])UpdateTimer(i);
  131. }
  132. }
  133. }
  134. int Generateur::CheckNbMonster( int id)
  135. {
  136. int nbmonster = 0;
  137. for(int i = 0; i < GetEngine()->GetNbObject();i++)
  138. {
  139. if(strcmp(GetEngine()->GetListeObject()[i]->GetTag() , M_Tag[id]) == 0 && GetEngine()->GetListeObject()[i]->GetEnable() == true)
  140. {
  141. if(GetObject()->Collision(i))nbmonster++;
  142. }
  143. }
  144. return nbmonster;
  145. }