Enemy.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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::inEyeSight(Vec3f & TargetPosition)
  42. {
  43. if (position.Distance(TargetPosition) <= radius)
  44. {
  45. hasTarget = true;
  46. target = TargetPosition;
  47. }
  48. else
  49. hasTarget = false;
  50. }
  51. bool Enemy::hasCollison(Vec3f &)
  52. {
  53. }
  54. void Enemy::update(float delta)
  55. {
  56. if (hasTarget)
  57. {
  58. //just 2d walking
  59. float dx, dz, length;
  60. dx = target.x - position.x;
  61. dz = target.z - position.z;
  62. length = sqrt(dx*dx + dz*dz);
  63. if (length > 0.03)
  64. {
  65. dx /= length;
  66. dz /= length;
  67. dx *= speed*delta;
  68. dz *= speed*delta;
  69. position.x += dx;
  70. position.z += dz;
  71. }
  72. rotation.y = atan2f(dx, dz) * 180 / M_PI;
  73. }
  74. }