Enemy.cpp 2.1 KB

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