Weapon.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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->collision = collision;
  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. void Weapon::draw(){
  44. if (weaponmodel != nullptr)
  45. {
  46. glPushMatrix();
  47. //Player position and rotation
  48. glTranslatef(position.x, position.y, position.z);
  49. glRotatef(rotation.x, 1, 0, 0);
  50. glRotatef(rotation.y, 0, 1, 0);
  51. glRotatef(rotation.z, 0, 0, 1);
  52. //offset from player
  53. glTranslatef(offsetPlayer.x, offsetPlayer.y, offsetPlayer.z);
  54. //Rotate weapon itself, from specific anker point
  55. glTranslatef(ankerPoint.x, ankerPoint.y, ankerPoint.z);
  56. glRotatef(rotationWeapon.z, 0, 0, 1);
  57. glRotatef(rotationWeapon.y, 0, 1, 0);
  58. glRotatef(rotationWeapon.x, 1, 0, 0);
  59. glTranslatef(-ankerPoint.x, -ankerPoint.y, -ankerPoint.z);
  60. glScalef(scale, scale, scale);
  61. weaponmodel->draw();
  62. glPopMatrix();
  63. }
  64. }