Enemy.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. const Vec3f &position,
  10. const Vec3f &rotation,
  11. const float &scale)
  12. {
  13. model = Model::load(fileName);
  14. this->position = position;
  15. this->rotation = rotation;
  16. this->scale = scale;
  17. this->canCollide = true;
  18. target = position;
  19. speed = 1;
  20. radius = 10;
  21. xp = 10;
  22. hasTarget = false;
  23. hit_sound_id = CrystalPoint::GetSoundSystem().LoadSound(fileMusic.c_str(), false);
  24. music = CrystalPoint::GetSoundSystem().GetSound(hit_sound_id);
  25. attack = false;
  26. }
  27. Enemy::~Enemy()
  28. {
  29. if (model)
  30. Model::unload(model);
  31. CrystalPoint::GetSoundSystem().UnloadSound(hit_sound_id);
  32. }
  33. void Enemy::draw()
  34. {
  35. Entity::draw();
  36. glPushMatrix();
  37. glTranslatef(position.x, position.y, position.z);
  38. glBegin(GL_LINE_LOOP);
  39. for (int i = 0; i < 360; i++)
  40. {
  41. //convert degrees into radians
  42. float degInRad = i*(M_PI / 180.0);
  43. glVertex3f(cos(degInRad)*radius, 1*scale,sin(degInRad)*radius);
  44. }
  45. glEnd();
  46. glPopMatrix();
  47. }
  48. void Enemy::inEyeSight(Vec3f & TargetPosition)
  49. {
  50. if (position.Distance(TargetPosition) <= radius)
  51. {
  52. hasTarget = true;
  53. target = TargetPosition;
  54. }
  55. else
  56. hasTarget = false;
  57. }
  58. void Enemy::collide(const Entity * entity)
  59. {
  60. Vec3f difference = position - entity->position; //zou misschien omgedraait moeten worden
  61. difference.y = 0;
  62. difference.Normalize();
  63. difference = difference * (entity->model->radius + 0.01f);
  64. position.x = difference.x + entity->position.x;
  65. position.z = difference.z + entity->position.z;
  66. }
  67. void Enemy::hit(int damage){
  68. }
  69. void Enemy::update(float delta)
  70. {
  71. music->SetPos(position, Vec3f());
  72. if (hasTarget)
  73. {
  74. if (music->IsPlaying() == false)
  75. {
  76. music->Play();
  77. }
  78. //just 2d walking
  79. float dx, dz, length;
  80. dx = target.x - position.x;
  81. dz = target.z - position.z;
  82. length = sqrt(dx*dx + dz*dz);
  83. if (length > 1)
  84. {
  85. attack = false;
  86. dx /= length;
  87. dz /= length;
  88. dx *= speed*delta;
  89. dz *= speed*delta;
  90. position.x += dx;
  91. position.z += dz;
  92. }
  93. else
  94. {
  95. attack = true;
  96. if (music->IsPlaying() == true)
  97. {
  98. // music->Pause();
  99. music->Stop();
  100. }
  101. }
  102. rotation.y = atan2f(dx, dz) * 180 / M_PI;
  103. }
  104. Player *player = Player::getInstance();
  105. if(inObject(player->leftWeapon->collisionPoint)){
  106. if(!isHit){
  107. isHit = true;
  108. hit(player->leftWeapon->damage);
  109. }
  110. }else if(inObject(player->rightWeapon->collisionPoint)){
  111. if(!isHit){
  112. isHit = true;
  113. hit(player->rightWeapon->damage);
  114. }
  115. }else{
  116. isHit = false;
  117. }
  118. }