Enemy.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. const bool &hasCollision)
  12. {
  13. model = Model::load(fileName);
  14. this->position = position;
  15. this->rotation = rotation;
  16. this->scale = scale;
  17. this->canCollide = hasCollision;
  18. target = position;
  19. speed = 1;
  20. radius = 10;
  21. hasTarget = false;
  22. openal = new OpenAL();
  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::update(float delta)
  45. {
  46. if (!openal->isMusicPlaying()) {
  47. openal->playMusic();
  48. }
  49. if (hasTarget)
  50. {
  51. //just 2d walking
  52. float dx, dz, length;
  53. dx = target.x - position.x;
  54. dz = target.z - position.z;
  55. length = sqrt(dx*dx + dz*dz);
  56. if (length > 0.03)
  57. {
  58. dx /= length;
  59. dz /= length;
  60. dx *= speed*delta;
  61. dz *= speed*delta;
  62. position.x += dx;
  63. position.z += dz;
  64. }
  65. rotation.y = atan2f(dx, dz) * 180 / M_PI;
  66. }
  67. }