Enemy.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #define _USE_MATH_DEFINES
  2. #include <cmath>
  3. #include "Enemy.h"
  4. #include "Model.h"
  5. #include "Player.h"
  6. #include <iostream>
  7. Enemy::Enemy(const std::string &fileName,
  8. const std::string &fileMusic,
  9. float damage,
  10. float health,
  11. const Vec3f &position,
  12. const Vec3f &rotation,
  13. const float &scale)
  14. {
  15. model = Model::load(fileName);
  16. this->position = position;
  17. this->rotation = rotation;
  18. this->scale = scale;
  19. this->canCollide = true;
  20. target = position;
  21. speed = 1;
  22. radius = 10;
  23. xp = health;
  24. this->health = health;
  25. this->damage = damage;
  26. hasTarget = false;
  27. hit_sound_id = CrystalPoint::GetSoundSystem().LoadSound(fileMusic.c_str(), false);
  28. music = CrystalPoint::GetSoundSystem().GetSound(hit_sound_id);
  29. attack = false;
  30. }
  31. Enemy::~Enemy()
  32. {
  33. music->Stop();
  34. CrystalPoint::GetSoundSystem().UnloadSound(hit_sound_id);
  35. if (model)
  36. Model::unload(model);
  37. }
  38. void Enemy::draw()
  39. {
  40. Entity::draw();
  41. }
  42. void Enemy::inEyeSight(Vec3f & TargetPosition)
  43. {
  44. if (position.Distance(TargetPosition) <= radius)
  45. {
  46. hasTarget = true;
  47. target = TargetPosition;
  48. }
  49. else
  50. hasTarget = false;
  51. }
  52. void Enemy::collide(const Entity * entity)
  53. {
  54. Vec3f difference = position - entity->position; //zou misschien omgedraait moeten worden
  55. difference.y = 0;
  56. difference.Normalize();
  57. difference = difference * (entity->model->radius + 0.01f);
  58. position.x = difference.x + entity->position.x;
  59. position.z = difference.z + entity->position.z;
  60. }
  61. void Enemy::hit(int damage){
  62. health -= damage;
  63. }
  64. bool Enemy::isDead(){
  65. return health < 0;
  66. }
  67. void Enemy::update(float delta)
  68. {
  69. music->SetPos(position, Vec3f());
  70. if (hasTarget)
  71. {
  72. if (music->IsPlaying() == false)
  73. {
  74. music->Play();
  75. }
  76. //just 2d walking
  77. float dx, dz, length;
  78. dx = target.x - position.x;
  79. dz = target.z - position.z;
  80. length = sqrt(dx*dx + dz*dz);
  81. if (length > 1)
  82. {
  83. attack = false;
  84. dx /= length;
  85. dz /= length;
  86. dx *= speed*delta;
  87. dz *= speed*delta;
  88. position.x += dx;
  89. position.z += dz;
  90. }
  91. else
  92. {
  93. attack = true;
  94. }
  95. rotation.y = atan2f(dx, dz) * 180 / M_PI;
  96. }
  97. Player *player = Player::getInstance();
  98. /* if(inObject(player->position + player->leftWeapon->collisionPoint)){
  99. if(!isHit){
  100. isHit = true;
  101. hit(player->leftWeapon->damage);
  102. }
  103. std::cout << "HIT1";
  104. }else if(inObject(player->rightWeapon->collisionPoint)){
  105. if(!isHit){
  106. isHit = true;
  107. hit(player->rightWeapon->damage);
  108. }
  109. std::cout << "HIT2";
  110. }else{
  111. isHit = false;
  112. }*/
  113. }