Enemy.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 std::string &fileMusic,
  8. float damage,
  9. float health,
  10. const Vec3f &position,
  11. const Vec3f &rotation,
  12. const float &scale)
  13. {
  14. model = Model::load(fileName);
  15. this->position = position;
  16. this->rotation = rotation;
  17. this->scale = scale;
  18. this->canCollide = true;
  19. target = position;
  20. speed = 1;
  21. radius = 10;
  22. xp = health;
  23. this->health = health;
  24. this->damage = damage;
  25. hasTarget = false;
  26. hit_sound_id = CrystalPoint::GetSoundSystem().LoadSound(fileMusic.c_str(), false);
  27. music = CrystalPoint::GetSoundSystem().GetSound(hit_sound_id);
  28. attack = false;
  29. }
  30. Enemy::~Enemy()
  31. {
  32. music->Stop();
  33. CrystalPoint::GetSoundSystem().UnloadSound(hit_sound_id);
  34. if (model)
  35. Model::unload(model);
  36. }
  37. void Enemy::draw()
  38. {
  39. Entity::draw();
  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. void Enemy::collide(const Entity * entity)
  52. {
  53. Vec3f difference = position - entity->position; //zou misschien omgedraait moeten worden
  54. difference.y = 0;
  55. difference.Normalize();
  56. difference = difference * (entity->model->radius + 0.01f);
  57. position.x = difference.x + entity->position.x;
  58. position.z = difference.z + entity->position.z;
  59. }
  60. void Enemy::update(float delta)
  61. {
  62. music->SetPos(position, Vec3f());
  63. if (hasTarget)
  64. {
  65. if (music->IsPlaying() == false)
  66. {
  67. music->Play();
  68. }
  69. //just 2d walking
  70. float dx, dz, length;
  71. dx = target.x - position.x;
  72. dz = target.z - position.z;
  73. length = sqrt(dx*dx + dz*dz);
  74. if (length > 1)
  75. {
  76. attack = false;
  77. dx /= length;
  78. dz /= length;
  79. dx *= speed*delta;
  80. dz *= speed*delta;
  81. position.x += dx;
  82. position.z += dz;
  83. }
  84. else
  85. {
  86. attack = true;
  87. }
  88. rotation.y = atan2f(dx, dz) * 180 / M_PI;
  89. }
  90. }