Enemy.cpp 2.1 KB

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