Enemy.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. 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. if (hasTarget)
  66. {
  67. //just 2d walking
  68. float dx, dz, length;
  69. dx = target.x - position.x;
  70. dz = target.z - position.z;
  71. length = sqrt(dx*dx + dz*dz);
  72. if (length > 1)
  73. {
  74. attack = false;
  75. dx /= length;
  76. dz /= length;
  77. dx *= speed*delta;
  78. dz *= speed*delta;
  79. position.x += dx;
  80. position.z += dz;
  81. }
  82. else
  83. {
  84. attack = true;
  85. }
  86. rotation.y = atan2f(dx, dz) * 180 / M_PI;
  87. }
  88. if (false)
  89. {
  90. Sound* sound = CrystalPoint::GetSoundSystem().GetSound(hit_sound_id);
  91. sound->SetPos(position, Vec3f());
  92. sound->Play();
  93. }
  94. }