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