Enemy.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #define _USE_MATH_DEFINES
  2. #include <cmath>
  3. #include "Enemy.h"
  4. #include "Model.h"
  5. #include "CrystalPoint.h"
  6. #include <iostream>
  7. Enemy::Enemy(const std::string &fileName,
  8. const Vec3f &position,
  9. Vec3f &rotation,
  10. const float &scale)
  11. {
  12. model = Model::load(fileName);
  13. this->position = position;
  14. this->rotation = rotation;
  15. this->scale = scale;
  16. this->canCollide = true;
  17. target = position;
  18. speed = 1;
  19. radius = 10;
  20. hasTarget = false;
  21. hit_sound_id = CrystalPoint::GetSoundSystem().LoadSound("WAVE/Sound.wav", 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::inEyeSight(Vec3f & TargetPosition)
  44. {
  45. if (position.Distance(TargetPosition) <= radius)
  46. {
  47. hasTarget = true;
  48. target = TargetPosition;
  49. }
  50. else
  51. hasTarget = false;
  52. }
  53. bool Enemy::hasCollison(Vec3f &)
  54. {
  55. return false;
  56. }
  57. void Enemy::update(float delta)
  58. {
  59. if (hasTarget)
  60. {
  61. //just 2d walking
  62. float dx, dz, length;
  63. dx = target.x - position.x;
  64. dz = target.z - position.z;
  65. length = sqrt(dx*dx + dz*dz);
  66. if (length > 0.03)
  67. {
  68. dx /= length;
  69. dz /= length;
  70. dx *= speed*delta;
  71. dz *= speed*delta;
  72. position.x += dx;
  73. position.z += dz;
  74. }
  75. rotation.y = atan2f(dx, dz) * 180 / M_PI;
  76. }
  77. if (false)
  78. {
  79. Sound* sound = CrystalPoint::GetSoundSystem().GetSound(hit_sound_id);
  80. sound->SetPos(position, Vec3f());
  81. sound->Play();
  82. }
  83. }