Enemy.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #define _USE_MATH_DEFINES
  2. #include <cmath>
  3. #include "Enemy.h"
  4. #include "Model.h"
  5. #include <iostream>
  6. Enemy::Enemy(const std::string &fileName,
  7. const Vec3f &position,
  8. Vec3f &rotation,
  9. const float &scale,
  10. const bool &hasCollision)
  11. {
  12. model = Model::load(fileName);
  13. this->position = position;
  14. this->rotation = rotation;
  15. this->scale = scale;
  16. this->canCollide = hasCollision;
  17. target = position;
  18. speed = 1;
  19. radius = 10;
  20. hasTarget = false;
  21. }
  22. Enemy::~Enemy()
  23. {
  24. if (model)
  25. Model::unload(model);
  26. }
  27. void Enemy::draw()
  28. {
  29. Entity::draw();
  30. glPushMatrix();
  31. glTranslatef(position.x, position.y, position.z);
  32. glBegin(GL_LINE_LOOP);
  33. for (int i = 0; i < 360; i++)
  34. {
  35. //convert degrees into radians
  36. float degInRad = i*(M_PI / 180.0);
  37. glVertex3f(cos(degInRad)*radius, 1*scale,sin(degInRad)*radius);
  38. }
  39. glEnd();
  40. glPopMatrix();
  41. }
  42. void Enemy::update(float delta)
  43. {
  44. if (hasTarget)
  45. {
  46. //just 2d walking
  47. float dx, dz, length;
  48. dx = target.x - position.x;
  49. dz = target.z - position.z;
  50. length = sqrt(dx*dx + dz*dz);
  51. if (length > 0.03)
  52. {
  53. dx /= length;
  54. dz /= length;
  55. dx *= speed*delta;
  56. dz *= speed*delta;
  57. position.x += dx;
  58. position.z += dz;
  59. }
  60. rotation.y = atan2f(dx, dz) * 180 / M_PI;
  61. }
  62. }