Enemy.cpp 2.1 KB

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