Commit 2f2d3da7 authored by Benjamin Castel's avatar Benjamin Castel
Browse files

feat: Enabled cleaner movement for spaceships

parent 860ad906
Loading
Loading
Loading
Loading
+1 −1
Changes for include/model/Entity.hpp: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ class Entity {
        glm::quat get_rotation() const;

        glm::vec3 get_position() const;
        virtual glm::vec3 get_velocity() const;
        virtual glm::vec3 get_velocity(const float &deltaTime) const;

        void move(glm::vec3 translation);
        void rotate(glm::quat rotation);
+9 −5
Changes for include/model/Spaceship.hpp: 9 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -8,21 +8,25 @@

class Spaceship : public Entity {
    private:
        glm::vec3    acceleration;
        float        acceleration;
        Player       playerNumber;
        bool         thrusting;
        bool         blocked;
        float        fireCooldown;
        unsigned int healthPoints;

    public:
        Spaceship(const Player &playerNumber, const glm::mat4 &modelWorld);

        const glm::vec3    &get_acceleration()  const;
        const float        &get_acceleration()  const;
        const Player       &get_player_number() const;
        const bool         &is_thrusting()      const;
        const bool         &is_blocked()        const;
        const unsigned int &get_health_points() const;

        void set_acceleration(const glm::vec3 &newAcceleration);
        glm::vec3 get_velocity(const float &deltaTime) const override;

        void set_acceleration(const float &newAcceleration);
        void set_thrusting(const bool &newThrusting);
        
        void take_damage();
@@ -34,6 +38,6 @@ class Spaceship : public Entity {
        // TODO : REMOVE AND REPLACE
        void thrust(const float &deltaTime,
                    const std::unique_ptr<RenderObject> &renderObjectThis,
                    const Entity &other,
                    const std::unique_ptr<RenderObject> &renderObjectOther);
                    const Entity &otherShip,
                    const std::unique_ptr<RenderObject> &renderObjectOtherShip);
};
+1 −1
Changes for src/GlApplication.cpp: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -359,7 +359,7 @@ void GlApplication::continuousKey(float deltaTime)
}

void GlApplication::processPlayer1Input(GLFWwindow *window, float deltaTime) {
    float rads = glm::radians(20.0f*deltaTime);
    float rads = glm::radians(40.0f*deltaTime);
    if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { // Spaceship 1 thrusters
        this->m_spaceshipPlayer1->thrust(deltaTime, this->m_models[ModelKeys::SPACESHIP_1], *this->m_entities[ModelKeys::SPACESHIP_2][0], this->m_models[ModelKeys::SPACESHIP_2]);
    }
+1 −1
Changes for src/model/Entity.cpp: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -257,4 +257,4 @@ glm::vec3 Entity::get_position() const { return this->m_position; }

glm::quat Entity::get_rotation() const { return this->m_rotation; }

glm::vec3 Entity::get_velocity() const { return glm::vec3(0); }
glm::vec3 Entity::get_velocity(const float &deltaTime) const { return glm::vec3(0); }
+40 −7
Changes for src/model/Spaceship.cpp: 40 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -2,12 +2,14 @@

Spaceship::Spaceship(const Player &playerNumber, const glm::mat4 &modelWorld)
        : Entity(),
          acceleration(glm::vec3(1, 0, 0)), 
          thrusting(false),
          blocked(false),
          acceleration(1), 
          playerNumber(playerNumber),
          fireCooldown(0),
          healthPoints(3) {}

const glm::vec3 &Spaceship::get_acceleration() const { return this->acceleration; }
const float &Spaceship::get_acceleration() const { return this->acceleration; }

const Player &Spaceship::get_player_number() const { return this->playerNumber; }

@@ -15,7 +17,7 @@ const bool &Spaceship::is_thrusting() const { return this->thrusting; }

const unsigned int &Spaceship::get_health_points() const { return this->healthPoints; }

void Spaceship::set_acceleration(const glm::vec3 &newAcceleration) {
void Spaceship::set_acceleration(const float &newAcceleration) {
    this->acceleration = newAcceleration;
}

@@ -30,15 +32,42 @@ void Spaceship::take_damage() {
// TODO : REMOVE AND REPLACE
void Spaceship::thrust(const float &deltaTime,
                       const std::unique_ptr<RenderObject> &renderObjectThis,
                       const Entity &other,
                       const std::unique_ptr<RenderObject> &renderObjectOther) {
    if (!Entity::check_box_intersection(renderObjectThis, other, renderObjectOther, true, normalize(this->m_localZ)*deltaTime)) {
        Entity::move(normalize(this->m_localZ)*deltaTime);
                       const Entity &otherShip,
                       const std::unique_ptr<RenderObject> &renderObjectOtherShip) {
    this->thrusting = true;
    if (!Entity::check_box_intersection(renderObjectThis, otherShip, renderObjectOtherShip, true, this->get_velocity(deltaTime), otherShip.get_velocity(deltaTime))) {
        this->blocked = false; // Entity::move(normalize(this->m_localZ)*deltaTime);
    } else {
        this->blocked = true;
    }
}

void Spaceship::update(const float &deltaTime) {
    this->fireCooldown -= deltaTime;
    
    if (thrusting) {
        if (this->acceleration < 1) {
            this->acceleration = 1;
        } else if (this->acceleration >= 10) {
            this->acceleration = 10;
        } else {
            this->acceleration *= 1.01;
        }
    } else {
        if (this->acceleration < 0) {
            this->acceleration = 0;
        } else {
            this->acceleration -= 0.05;
        }
    }

    this->thrusting = false;

    if (this->blocked) {
        this->acceleration = 0;
    } else {
        Entity::move(normalize(this->m_localZ)*this->acceleration*deltaTime);
    }
}

std::shared_ptr<Missile> Spaceship::generate_missile() {
@@ -50,3 +79,7 @@ std::shared_ptr<Missile> Spaceship::generate_missile() {

    return std::shared_ptr<Missile>(new Missile(this));
}

glm::vec3 Spaceship::get_velocity(const float &deltaTime) const {
    return this->acceleration*glm::normalize(this->m_localZ)*deltaTime;
}