Enemy.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #define _USE_MATH_DEFINES
  2. #include <cmath>
  3. #include "Enemy.h"
  4. #include "Model.h"
  5. #include "Player.h"
  6. #include <iostream>
  7. Enemy::Enemy(const std::string &fileName,
  8. const std::string &fileMusic,
  9. float damage,
  10. float health,
  11. const Vec3f &position,
  12. const Vec3f &rotation,
  13. const float &scale)
  14. {
  15. model = Model::load(fileName);
  16. this->position = position;
  17. this->rotation = rotation;
  18. this->scale = scale;
  19. this->canCollide = true;
  20. target = position;
  21. speed = 1;
  22. radius = 10;
  23. xp = health;
  24. this->health = health;
  25. this->damage = damage;
  26. hasTarget = false;
  27. hit_sound_id = CrystalPoint::GetSoundSystem().LoadSound(fileMusic.c_str(), false);
  28. music = CrystalPoint::GetSoundSystem().GetSound(hit_sound_id);
  29. attack = false;
  30. }
  31. Enemy::~Enemy()
  32. {
  33. if (model)
  34. Model::unload(model);
  35. CrystalPoint::GetSoundSystem().UnloadSound(hit_sound_id);
  36. }
  37. void Enemy::draw()
  38. {
  39. Entity::draw();
  40. glPushMatrix();
  41. glTranslatef(position.x, position.y, position.z);
  42. glBegin(GL_LINE_LOOP);
  43. for (int i = 0; i < 360; i++)
  44. {
  45. //convert degrees into radians
  46. float degInRad = i*(M_PI / 180.0);
  47. glVertex3f(cos(degInRad)*radius, 1*scale,sin(degInRad)*radius);
  48. }
  49. glEnd();
  50. glPopMatrix();
  51. }
  52. void Enemy::inEyeSight(Vec3f & TargetPosition)
  53. {
  54. if (position.Distance(TargetPosition) <= radius)
  55. {
  56. hasTarget = true;
  57. target = TargetPosition;
  58. }
  59. else
  60. hasTarget = false;
  61. }
  62. void Enemy::collide(const Entity * entity)
  63. {
  64. Vec3f difference = position - entity->position; //zou misschien omgedraait moeten worden
  65. difference.y = 0;
  66. difference.Normalize();
  67. difference = difference * (entity->model->radius + 0.01f);
  68. position.x = difference.x + entity->position.x;
  69. position.z = difference.z + entity->position.z;
  70. }
  71. void Enemy::hit(int damage){
  72. health -= damage;
  73. }
  74. bool Enemy::isDead(){
  75. return health < 0;
  76. }
  77. void Enemy::update(float delta)
  78. {
  79. music->SetPos(position, Vec3f());
  80. if (hasTarget)
  81. {
  82. if (music->IsPlaying() == false)
  83. {
  84. music->Play();
  85. }
  86. //just 2d walking
  87. float dx, dz, length;
  88. dx = target.x - position.x;
  89. dz = target.z - position.z;
  90. length = sqrt(dx*dx + dz*dz);
  91. if (length > 1)
  92. {
  93. attack = false;
  94. dx /= length;
  95. dz /= length;
  96. dx *= speed*delta;
  97. dz *= speed*delta;
  98. position.x += dx;
  99. position.z += dz;
  100. }
  101. else
  102. {
  103. attack = true;
  104. if (music->IsPlaying() == true)
  105. {
  106. // music->Pause();
  107. music->Stop();
  108. }
  109. }
  110. rotation.y = atan2f(dx, dz) * 180 / M_PI;
  111. }
  112. Player *player = Player::getInstance();
  113. if(inObject(player->position + player->leftWeapon->collisionPoint)){
  114. if(!isHit){
  115. isHit = true;
  116. hit(player->leftWeapon->damage);
  117. }
  118. std::cout << "HIT1";
  119. }else if(inObject(player->rightWeapon->collisionPoint)){
  120. if(!isHit){
  121. isHit = true;
  122. hit(player->rightWeapon->damage);
  123. }
  124. std::cout << "HIT2";
  125. }else{
  126. isHit = false;
  127. }
  128. }