Enemy.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #define _USE_MATH_DEFINES
  2. #include <cmath>
  3. #include "OpenAL.h"
  4. #include "Enemy.h"
  5. #include "Model.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. // openal = new OpenAL();
  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. bool Enemy::hasCollison(Vec3f &)
  54. {
  55. }
  56. void Enemy::update(float delta)
  57. {
  58. // if (!openal->isMusicPlaying()) {
  59. // openal->playMusic();
  60. // }
  61. if (hasTarget)
  62. {
  63. //just 2d walking
  64. float dx, dz, length;
  65. dx = target.x - position.x;
  66. dz = target.z - position.z;
  67. length = sqrt(dx*dx + dz*dz);
  68. if (length > 0.03)
  69. {
  70. dx /= length;
  71. dz /= length;
  72. dx *= speed*delta;
  73. dz *= speed*delta;
  74. position.x += dx;
  75. position.z += dz;
  76. }
  77. rotation.y = atan2f(dx, dz) * 180 / M_PI;
  78. }
  79. }