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