Guest User

Untitled

a guest
Oct 6th, 2016
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. #pragma once
  2. #include "SFML\Graphics.hpp"
  3. #include "ResourceIdentifier.h"
  4. #include "ResourceManager.h"
  5.  
  6. class Entity {
  7.  
  8. public:
  9.  
  10. Entity() : m_angle(0) {
  11. }
  12.  
  13. float getX() const noexcept { return m_sprite.getPosition().x; }
  14.  
  15. float getY() const noexcept { return m_sprite.getPosition().y; }
  16.  
  17. float left() const noexcept { return getX() - m_sprite.getGlobalBounds().width / 2.0f; }
  18.  
  19. float right() const noexcept { return getX() + m_sprite.getGlobalBounds().width / 2.0f; }
  20.  
  21. float top() const noexcept { return getY() - m_sprite.getGlobalBounds().height / 2.0f; }
  22.  
  23. float bottom() const noexcept { return getY() + m_sprite.getGlobalBounds().height / 2.0f; }
  24.  
  25. float spriteWidth() const noexcept { return m_sprite.getGlobalBounds().width; }
  26.  
  27. float spriteHeight() const noexcept { return m_sprite.getGlobalBounds().height; }
  28.  
  29. void draw(sf::RenderWindow& window) {
  30. window.draw(m_sprite);
  31. }
  32.  
  33. bool checkCollision(const Entity& entity) const {
  34. return (m_sprite.getGlobalBounds().intersects(entity.m_sprite.getGlobalBounds()));
  35. }
  36.  
  37. const sf::Sprite& getSprite() const {
  38. return m_sprite;
  39. }
  40.  
  41. virtual ~Entity() {
  42. }
  43.  
  44. protected:
  45.  
  46. double m_angle;
  47. sf::Sprite m_sprite;
  48. int m_speed;//speed that you travel every second
  49.  
  50. };
  51.  
  52.  
  53.  
  54. #pragma once
  55. #include "Globals.h"
  56. #include "ResourceManager.h"
  57. #include "ResourceIdentifier.h"
  58. #include "Entity.h"
  59. #include <iostream>
  60. #include <cmath>
  61.  
  62. class Shooter : public Entity{
  63.  
  64. private:
  65. bool m_moving;
  66. bool m_targetInDeadzone;
  67. sf::Vector2f m_targetPos;
  68. const int m_deadzoneRadius = 5;
  69. public:
  70. Shooter();
  71. void initialize(const TextureManager&);
  72. void moveShip(float dt);
  73. void angleShipToMouse(const sf::Vector2i&);
  74. void handleInput(const sf::Event&, const sf::Vector2i&);
  75. void update(const sf::Vector2i&, float);
  76. void adjustingTargetPosition();
  77. bool inDeadzone();
  78. };
  79.  
  80. #include "SpaceShooter.h"
  81.  
  82. Shooter::Shooter() : m_moving(false)
  83. {
  84. }
  85.  
  86. void Shooter::initialize(const TextureManager& text)
  87. {
  88. m_speed = 700;
  89. this->m_sprite.setTexture(text.get(TextureID::Shooter));
  90. this->m_sprite.setOrigin(m_sprite.getGlobalBounds().width / 2.0f, m_sprite.getGlobalBounds().height / 2.0f);
  91. this->m_sprite.setPosition(Globals::m_windowWidth / 2.0f, Globals::m_windowHeight / 2.0f);
  92. }
  93.  
  94. void Shooter
  95. ::moveShip(float dt)
  96. {
  97. if (m_moving) {
  98.  
  99. if (m_targetInDeadzone) {
  100. adjustingTargetPosition(); //angle can change the width and height of sprite and this changes the size of the deadzone
  101. }
  102.  
  103. sf::Vector2f shipVelocity(cos(m_angle * Globals::deg2rad) * m_speed * dt, sin(m_angle * Globals::deg2rad) * m_speed * dt);
  104.  
  105. float targetDistance = sqrt(pow(m_targetPos.x - getX(), 2) + pow(m_targetPos.y - getY(), 2));
  106. float distanceToTravel = sqrt(pow(shipVelocity.x, 2) + pow(shipVelocity.y, 2));
  107.  
  108. if (targetDistance > distanceToTravel) {
  109. this->m_sprite.move(shipVelocity.x, shipVelocity.y);
  110. std::cout << m_targetPos.y << std::endl;
  111. // std::cout << targetDistance << " > " << distanceToTravel << std::endl;
  112. }
  113. else {
  114. this->m_sprite.setPosition(m_targetPos.x, m_targetPos.y);
  115. m_moving = false;
  116. }
  117. }
  118. }
  119.  
  120. void Shooter::angleShipToMouse(const sf::Vector2i& mousePosition) {
  121.  
  122. sf::Vector2f mouseRelativeShip(mousePosition.x - getX(), mousePosition.y - getY());
  123.  
  124. //5 pixel circular deadzone where mouse position cant change ship angle
  125. if (sqrt(pow(mouseRelativeShip.x, 2) + pow(mouseRelativeShip.y, 2)) <= m_deadzoneRadius) {
  126. return;
  127. }
  128.  
  129. float rotation = atan2(mouseRelativeShip.y, mouseRelativeShip.x)*Globals::rad2deg; //atan2 produces negative angles if vector is in QUADS 1&2, positive in QUADS 3&4
  130.  
  131. this->m_sprite.setRotation(rotation);
  132.  
  133. m_angle = this->m_sprite.getRotation();
  134. }
  135.  
  136. void Shooter::handleInput(const sf::Event& event, const sf::Vector2i& mousePos) {
  137. if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {
  138. if (!m_moving) {
  139. m_moving = true;
  140. m_targetPos = static_cast<sf::Vector2f>(mousePos);
  141. m_targetInDeadzone = inDeadzone();
  142. }
  143. }
  144. }
  145.  
  146. void Shooter::update(const sf::Vector2i& mousePosition, float dt) {
  147. angleShipToMouse(mousePosition);
  148. moveShip(dt);
  149. }
  150.  
  151. void Shooter::adjustingTargetPosition()
  152. {
  153. if (m_targetPos.x < spriteWidth() / 2.0f) {
  154. m_targetPos.x = spriteWidth() / 2.0f;
  155. }
  156. else if (m_targetPos.x > Globals::m_windowWidth - spriteWidth() / 2.0f) {
  157. m_targetPos.x = Globals::m_windowWidth - spriteWidth() / 2.0f;
  158. }
  159. else if(m_targetPos.y < spriteHeight() / 2.0f){
  160. m_targetPos.y = spriteHeight() / 2.0f;
  161. }
  162. else if (m_targetPos.y > Globals::m_windowHeight - spriteHeight() / 2.0f) {
  163. m_targetPos.y = Globals::m_windowHeight - spriteHeight() / 2.0f;
  164. }
  165. }
  166.  
  167. bool Shooter::inDeadzone()
  168. {
  169. if (m_targetPos.x < spriteWidth() / 2.0f || m_targetPos.x > Globals::m_windowWidth - spriteWidth() / 2.0f ||
  170. m_targetPos.y < spriteHeight() / 2.0f || m_targetPos.y > Globals::m_windowHeight - spriteHeight() / 2.0f) {
  171. return true;
  172. } return false;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment