Enemy.cpp 2.3 KB

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