Enemy.cpp 1.9 KB

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