如何修复太空飞船与边界的互动以及更多?

时间:2016-10-05 12:49:19

标签: c++ sfml

#pragma once
#include "Globals.h"
#include "ResourceManager.h"
#include "ResourceIdentifier.h"
#include "Entity.h"
#include <iostream>
#include <cmath>

class Shooter : public Entity{

private:
    bool m_isClicked;
    bool m_ableToMove;
    float m_lastAngle;
    bool m_mouseInBounds;
    sf::Vector2i m_targetPos;

public:
    Shooter();
    void initialize(const TextureManager&);
    void moveShip(float dt);
    void angleShipToMouse(const sf::Vector2i&);
    void handleInput(const sf::Event&, const sf::Vector2i&);
    void update(const sf::Vector2i&, float);
    void adjustingTarget();
};


#include "SpaceShooter.h"

Shooter::Shooter() : m_ableToMove(false), m_isClicked(false), m_lastAngle(0)
{
}

void Shooter::initialize(const TextureManager& text)
{
    m_speed = sf::Vector2i(500, 500);
    this->m_sprite.setTexture(text.get(TextureID::Shooter));
    this->m_sprite.setOrigin(m_sprite.getGlobalBounds().width / 2.0f, m_sprite.getGlobalBounds().height / 2.0f);
    this->m_sprite.setPosition(Globals::_windowWidth / 2.0f, Globals::_windowHeight / 2.0f); //start 50 units out
}

void Shooter::moveShip(float dt) {

    if (m_isClicked && !m_ableToMove) {
        m_ableToMove = true;
        m_isClicked = false;   
    }

    if (m_ableToMove) {
        adjustingTarget();
        sf::Vector2f shipComponentDistances(abs(m_targetPos.x - getX()), abs(m_targetPos.y - getY()));
        sf::Vector2f shipVelocity(cos(m_angle * Globals::deg2rad) * dt * m_speed.x,
        sin(m_angle*Globals::deg2rad) * dt * m_speed.y);

        float targetDistance = sqrt(pow(shipComponentDistances.x, 2) + pow(shipComponentDistances.y, 2));
        float distanceToTravel = sqrt(pow(shipVelocity.x, 2) + pow(shipVelocity.y, 2));

        if (targetDistance > distanceToTravel) {
            this->m_sprite.move(shipVelocity);
            m_lastAngle = m_angle;
    //  std::cout << distance << std::endl;
        }
        else {
            this->m_sprite.setPosition(m_targetPos.x - 0.01, m_targetPos.y - 0.01);
            std::cout << m_lastAngle << std::endl;
            this->m_sprite.setRotation(m_lastAngle);
            std::cout << this->m_sprite.getRotation() << std::endl;
            m_ableToMove = false;
            m_isClicked = false;
        }
    }
}

void Shooter::angleShipToMouse(const sf::Vector2i& mousePosition) {

    //position of mouse relative to ship in 0 rotation reference frame(absolute)
    sf::Vector2f mouseRelativeShip(mousePosition.x - getX(), mousePosition.y - getY());


    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

    this->m_sprite.setRotation(rotation);

    m_angle = this->m_sprite.getRotation();
}

void Shooter::handleInput(const sf::Event& event, const sf::Vector2i& mousePos) {
    if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {  
        if (!m_ableToMove && !m_isClicked) {
            m_isClicked = true;
            m_targetPos = mousePos;
        }
    }
}

void Shooter::update(const sf::Vector2i& mousePosition, float dt) {
    angleShipToMouse(mousePosition);
    moveShip(dt);
}

void Shooter::adjustingTarget()
{
    if (m_targetPos.x < spriteWidth() / 2.0f) {
        m_targetPos.x = spriteWidth() / 2.0f;
    }
    else if (m_targetPos.x > Globals::_windowWidth - spriteWidth() / 2.0f) {
        m_targetPos.x = Globals::_windowWidth - spriteWidth() / 2.0f;
    }
    else if(m_targetPos.y < spriteHeight() / 2.0f){
        m_targetPos.y = spriteHeight() / 2.0f;
    }
    else if (m_targetPos.y > Globals::_windowHeight - spriteHeight() / 2.0f) {
        m_targetPos.y = Globals::_windowHeight - spriteHeight() / 2.0f;
    }
}

我的宇宙飞船的问题是: 1.尽管在移动时不允许更改targetPos,但在移动到其他位置时单击会导致它重定向。按住船使其跟随鼠标光标。

  1. 一旦船到达targetPosition,船的旋转设置为0,我试图使其在到达目的地之前保持角度的旋转(例如,到targetPos的距离为0)。 / p>

  2. 当它到达边界时,有时它可以通过它,尽管我调整了targetPosition如果鼠标太远进入死区,船无法进入。

0 个答案:

没有答案