Enemy.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. dx /= length;
  52. dz /= length;
  53. dx *= speed*delta;
  54. dz *= speed*delta;
  55. position.x += dx;
  56. position.z += dz;
  57. rotation.y = atan2f(target.x - position.x, target.z - position.z) * 180 / M_PI;
  58. }
  59. }