Weapon.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Created by janco on 25-5-16.
  3. //
  4. #include "Weapon.h"
  5. #include "Model.h"
  6. #include <iostream>
  7. #include <string>
  8. #include <cmath>
  9. Weapon::Weapon(std::string name, int damage, Element e, std::string modelFilename, float scale, Vec3f location, Vec2f rotation,
  10. Vec3f offsetPlayer, Vec3f ankerPoint,
  11. Vec2f maxRotation, Vec2f minRotation,
  12. Vec3f collision){
  13. weaponmodel = Model::load(modelFilename);
  14. rotate(rotation);
  15. move(location);
  16. this->scale = scale;
  17. this->name = name;
  18. this->damage = damage;
  19. this->element = e;
  20. this->offsetPlayer = offsetPlayer;
  21. this->ankerPoint = ankerPoint;
  22. this->maxRotation = maxRotation;
  23. this->minRotation = minRotation;
  24. this->collisionPoint = collisionPoint;
  25. };
  26. Weapon::~Weapon(){
  27. }
  28. void Weapon::rotateWeapon(Vec3f rotation){
  29. if(rotation.x < maxRotation.x && rotation.x > minRotation.x){
  30. rotationWeapon.x = rotation.x;
  31. }
  32. if(rotation.z < maxRotation.y && rotation.z > minRotation.y){
  33. rotationWeapon.z = rotation.z;
  34. }
  35. rotationWeapon.y = rotation.y;
  36. }
  37. void Weapon::rotate(Vec2f rotation){
  38. this->rotation.y = -rotation.y;
  39. }
  40. void Weapon::move(Vec3f location){
  41. position = location;
  42. }
  43. Vec3f multiply(float matrix[16], Vec3f vec)
  44. {
  45. Vec3f result;
  46. for(int i = 0; i < 4; i++)
  47. {
  48. for(int p = 0; p < 4; p++)
  49. {
  50. result[i] += matrix[i * p] * vec[p];
  51. }
  52. }
  53. return result;
  54. }
  55. void Weapon::draw(){
  56. if (weaponmodel != nullptr)
  57. {
  58. glPushMatrix();
  59. //Player position and rotation
  60. glTranslatef(position.x, position.y, position.z);
  61. glRotatef(rotation.x, 1, 0, 0);
  62. glRotatef(rotation.y, 0, 1, 0);
  63. glRotatef(rotation.z, 0, 0, 1);
  64. //offset from player
  65. glTranslatef(offsetPlayer.x, offsetPlayer.y, offsetPlayer.z);
  66. glScalef(scale, scale, scale);
  67. //Rotate weapon itself, from specific anker point
  68. glTranslatef(ankerPoint.x, ankerPoint.y, ankerPoint.z);
  69. glRotatef(rotationWeapon.z, 0, 0, 1);
  70. glRotatef(rotationWeapon.y, 0, 1, 0);
  71. glRotatef(rotationWeapon.x, 1, 0, 0);
  72. glTranslatef(-ankerPoint.x, -ankerPoint.y, -ankerPoint.z);
  73. weaponmodel->draw();
  74. glPopMatrix();
  75. glPushMatrix();
  76. glPopMatrix();
  77. }
  78. }