Advertisement
Guest User

Untitled

a guest
May 13th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. #ifndef RIGIDBODY_HPP_
  2. #define RIGIDBODY_HPP_
  3.  
  4. #include "Physics/Physics.hpp"
  5.  
  6. #include <SFML/Graphics.hpp>
  7. #include <SFML/System.hpp>
  8.  
  9. #include <cmath>
  10.  
  11. namespace LGE{
  12.  
  13. enum RigidBodyType{
  14. RIGIDBODY_ELASTIC = 1, // The elastic resolution will displace and also bounce the colliding entity off by reducing the velocity by it's restituion coefficient
  15. RIGIDBODY_DISPLACE = 2, // The displace resolution will only move an entity outside of the space of the other and zero the velocity in that direction
  16. RIGIDBODY_NONE = 3 // Collision will be ignored
  17. };
  18.  
  19. class RigidBody{
  20. public:
  21. RigidBody(sf::FloatRect rect, RigidBodyType type, bool dynamic = true); // If body isn't dynamic, it will be parsed as kinematic
  22. RigidBody(RigidBodyType type = RIGIDBODY_ELASTIC, bool dynamic = true);
  23.  
  24. void setDynamic(bool dynamic){ this->dynamic = dynamic; };
  25. void setMass(int mass);
  26. void setMaxVelocity(sf::Vector2f maxVelocity){ this->maxVelocity = maxVelocity; };
  27. void updatePhysics(sf::Time dt, RigidBody **bodys, int numBodys = 1);
  28.  
  29. bool isDynamic(){ return this->dynamic; };
  30. bool checkCollisionAndResolve(RigidBody *object);
  31.  
  32. int getLeft(){ return this->rect.left; };
  33. int getRight(){ return this->rect.left + this->rect.width; };
  34. int getTop(){ return this->rect.top; };
  35. int getBottom(){ return this->rect.top + this->rect.height; };
  36. int getWidth(){ return this->rect.width; };
  37. int getHeight(){ return this->rect.height; };
  38. int getMass(){ return this->mass; };
  39. int getDistance(RigidBody *object){ return sqrt((int)(this->rect.left - object->rect.left) ^ 2
  40. + (int)(this->rect.top - object->rect.top) ^ 2); };
  41.  
  42. float getRestitution(){ return this->restitution; };
  43.  
  44. sf::Vector2f getCenter(){ return sf::Vector2f(this->rect.left + this->rect.width / 2,
  45. this->rect.top + this->rect.height / 2); };
  46. sf::FloatRect rect;
  47. protected:
  48. sf::Vector2f velocity;
  49. sf::Vector2f acceleration;
  50. sf::Vector2f maxVelocity;
  51.  
  52. RigidBodyType bodyType;
  53.  
  54. bool dynamic;
  55.  
  56. int mass = 1;
  57. float restitution = 0.2;
  58. };
  59.  
  60. };
  61.  
  62. #endif // RIGIDBODY_HPP
  63.  
  64.  
  65. #ifndef ITEM_HPP_
  66. #define ITEM_HPP_
  67.  
  68. #include "Physics/RigidBody.hpp"
  69. #include "Item/ItemInfo.hpp"
  70.  
  71. #include <SFML/Graphics.hpp>
  72. #include <SFML/System.hpp>
  73.  
  74. #include <string>
  75.  
  76. namespace LGE{
  77.  
  78. class Item : public RigidBody{
  79. public:
  80. Item(ItemInfo *info, sf::Texture texture);
  81. Item();
  82.  
  83. Item *drop();
  84.  
  85. virtual void update(sf::Time dt);
  86. virtual void draw(sf::RenderWindow *window);
  87. virtual void updateSpriteRect(bool neighbors[8]);
  88.  
  89. void setTexture(sf::Texture &texture){ this->sprite.setTexture(texture); };
  90. void setPosition(int x, int y){ this->rect.left = x; this->rect.top = y; };
  91.  
  92. int getId(){ return this->info->getId(); };
  93. int getCost(){ return this->info->getCost(); };
  94. int getStackMax(){ return this->info->getStackMax(); };
  95. int getDamage(){ return this->info->getDamage(); };
  96. int getWidth(){ return this->info->getWidth(); };
  97. int getHeight(){ return this->info->getHeight(); };
  98.  
  99. bool isPlaceable(){ return this->info->isPlaceable(); };
  100. bool isSolid(){ return this->info->isSolid(); };
  101. bool isDropped(){ return this->dropped; };
  102.  
  103. std::string getName(){ return this->info->getName(); };
  104. std::string getDescription(){ return this->info->getDescription(); };
  105. std::string getTexturePath(){ return this->info->getTexturePath(); };
  106.  
  107. ItemType getType(){ return this->info->getType(); };
  108. protected:
  109. ItemInfo *info;
  110.  
  111. sf::Sprite sprite;
  112.  
  113. bool dropped;
  114. };
  115.  
  116. class Air : public Item{
  117. public:
  118. Air(ItemInfo *info, sf::Texture texture);
  119.  
  120. void update(sf::Time dt);
  121. void draw(sf::RenderWindow *window);
  122. void updateSpriteRect(bool neighbors[8]);
  123. private:
  124. };
  125.  
  126. };
  127.  
  128. #endif // ITEM_HPP_
  129.  
  130. #ifndef BLOCK_HPP_
  131. #define BLOCK_HPP_
  132.  
  133. #include "Item/Item.hpp"
  134. #include "Item/ItemInfo.hpp"
  135.  
  136. namespace LGE{
  137.  
  138. class Block : public Item{
  139. public:
  140. Block(ItemInfo *info, sf::Texture texture);
  141.  
  142. void update(sf::Time dt);
  143. void draw(sf::RenderWindow *window);
  144. void updateSpriteRect(bool neighbors[8]);
  145. private:
  146.  
  147. };
  148.  
  149. };
  150.  
  151. #endif // BLOCK_HPP_
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement