Weapon.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. this->damage = 1;
  26. };
  27. Weapon::~Weapon(){
  28. }
  29. void Weapon::rotateWeapon(Vec3f rotation){
  30. if(rotation.x < maxRotation.x && rotation.x > minRotation.x){
  31. rotationWeapon.x = rotation.x;
  32. }
  33. if(rotation.z < maxRotation.y && rotation.z > minRotation.y){
  34. rotationWeapon.z = rotation.z;
  35. }
  36. rotationWeapon.y = rotation.y;
  37. }
  38. void Weapon::rotate(Vec2f rotation){
  39. this->rotation.y = -rotation.y;
  40. }
  41. void Weapon::move(Vec3f location){
  42. position = location;
  43. }
  44. Vec3f multiply(float matrix[16], Vec3f vec)
  45. {
  46. Vec3f result;
  47. for(int i = 0; i < 4; i++)
  48. {
  49. for(int p = 0; p < 4; p++)
  50. {
  51. result[i] += matrix[i * p] * vec[p];
  52. }
  53. }
  54. return result;
  55. }
  56. void Weapon::draw(){
  57. if (weaponmodel != nullptr)
  58. {
  59. glPushMatrix();
  60. //Player position and rotation
  61. glTranslatef(position.x, position.y, position.z);
  62. glRotatef(rotation.x, 1, 0, 0);
  63. glRotatef(rotation.y, 0, 1, 0);
  64. glRotatef(rotation.z, 0, 0, 1);
  65. //offset from player
  66. glTranslatef(offsetPlayer.x, offsetPlayer.y, offsetPlayer.z);
  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. //Test code for finding anchor point
  75. glTranslatef(collisionPoint.x, collisionPoint.y, collisionPoint.z);
  76. float matrix[16];
  77. glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
  78. Vec3f point = multiply(matrix, Vec3f(1,1,1));
  79. glScalef(scale, scale, scale);
  80. glPopMatrix();
  81. glPushMatrix();
  82. glTranslatef(point.x, point.y, point.z);
  83. glColor3ub(255, 255, 0);
  84. glBegin(GL_LINES);
  85. glVertex2f(0, 4);
  86. glVertex2f(0, -4);
  87. glVertex2f(4, 0);
  88. glVertex2f(-4, 0);
  89. glEnd();
  90. glPopMatrix();
  91. }
  92. }