Enemy.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. }
  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::update(float delta)
  44. {
  45. if (hasTarget)
  46. {
  47. OpenAL();
  48. //just 2d walking
  49. float dx, dz, length;
  50. dx = target.x - position.x;
  51. dz = target.z - position.z;
  52. length = sqrt(dx*dx + dz*dz);
  53. if (length > 0.03)
  54. {
  55. dx /= length;
  56. dz /= length;
  57. dx *= speed*delta;
  58. dz *= speed*delta;
  59. position.x += dx;
  60. position.z += dz;
  61. }
  62. rotation.y = atan2f(dx, dz) * 180 / M_PI;
  63. }
  64. }