Enemy.cpp 2.6 KB

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