Enemy.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. canAttack = false;
  29. lastAttacked = 0;
  30. }
  31. Enemy::~Enemy()
  32. {
  33. music->Stop();
  34. CrystalPoint::GetSoundSystem().UnloadSound(hit_sound_id);
  35. if (model)
  36. Model::unload(model);
  37. }
  38. void Enemy::draw()
  39. {
  40. Entity::draw();
  41. glPushMatrix();
  42. glTranslatef(position.x, position.y, position.z);
  43. glBegin(GL_LINE_LOOP);
  44. for (int i = 0; i < 360; i++)
  45. {
  46. //convert degrees into radians
  47. float degInRad = i*(M_PI / 180.0);
  48. glVertex3f(cos(degInRad)*radius, 1*scale,sin(degInRad)*radius);
  49. }
  50. glEnd();
  51. glPopMatrix();
  52. }
  53. void Enemy::inEyeSight(Vec3f & TargetPosition)
  54. {
  55. if (position.Distance(TargetPosition) <= radius)
  56. {
  57. hasTarget = true;
  58. target = TargetPosition;
  59. }
  60. else
  61. hasTarget = false;
  62. }
  63. void Enemy::collide(const Entity * entity)
  64. {
  65. Vec3f difference = position - entity->position; //zou misschien omgedraait moeten worden
  66. difference.y = 0;
  67. difference.Normalize();
  68. difference = difference * (entity->model->radius + 0.01f);
  69. position.x = difference.x + entity->position.x;
  70. position.z = difference.z + entity->position.z;
  71. }
  72. bool Enemy::attack()
  73. {
  74. float timeNow = glutGet(GLUT_ELAPSED_TIME)/1000.0f;
  75. if (canAttack && timeNow - lastAttacked > 0.8)
  76. {
  77. lastAttacked = timeNow;
  78. return true;
  79. }
  80. return false;
  81. }
  82. void Enemy::update(float delta)
  83. {
  84. music->SetPos(position, Vec3f());
  85. if (hasTarget)
  86. {
  87. if (music->IsPlaying() == false)
  88. {
  89. music->Play();
  90. }
  91. //just 2d walking
  92. float dx, dz, length;
  93. dx = target.x - position.x;
  94. dz = target.z - position.z;
  95. length = sqrt(dx*dx + dz*dz);
  96. if (length > 1)
  97. {
  98. canAttack = false;
  99. dx /= length;
  100. dz /= length;
  101. dx *= speed*delta;
  102. dz *= speed*delta;
  103. position.x += dx;
  104. position.z += dz;
  105. }
  106. else
  107. {
  108. canAttack = true;
  109. }
  110. rotation.y = atan2f(dx, dz) * 180 / M_PI;
  111. }
  112. }