-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspaceship.cpp
More file actions
74 lines (64 loc) · 2.05 KB
/
Copy pathspaceship.cpp
File metadata and controls
74 lines (64 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "spaceship.hpp"
#include <SFML/Graphics.hpp>
#include "entity.hpp"
#include "constants.hpp"
const sf::Texture Spaceship::SPACESHIP_TEXTURE = Entity::loadTexture("../images/player.png");
const float Spaceship::ACCELERATION = 0.002f;
const float Spaceship::MAX_SPEED = 0.1f;
const float Spaceship::ROTATION_SPEED = 5.0f;
const float Spaceship::SPACESHIP_LIFE = 3.0f;
Spaceship::Spaceship() :
Entity(sf::Vector2f(static_cast<float>(WIDTH) / 2.0f, static_cast<float>(HEIGHT) / 2.0f), 0.0f, SPACESHIP_LIFE),
speed_(0.0f, 0.0f)
{
sprite_.setTexture(SPACESHIP_TEXTURE);
sprite_.setOrigin(sprite_.getGlobalBounds().width / 2.0f, sprite_.getGlobalBounds().height / 2.0f);
sprite_.setPosition(position_);
sprite_.setScale(0.15f, 0.15f);
}
void Spaceship::rotate(const float &direction)
{
angle_ += direction * ROTATION_SPEED;
direction_.x = cos(angle_ * DEG2RAD);
direction_.y = sin(angle_ * DEG2RAD);
}
void Spaceship::move(const float &direction)
{
speed_.x += (direction * ACCELERATION * direction_.x);
speed_.y += (direction * ACCELERATION * direction_.y);
if ((speed_.x * speed_.x) > (MAX_SPEED * MAX_SPEED))
{
speed_.x = (speed_.x > 0) ? MAX_SPEED : -MAX_SPEED;
}
if ((speed_.y * speed_.y) > (MAX_SPEED * MAX_SPEED))
{
speed_.y = (speed_.y > 0) ? MAX_SPEED : -MAX_SPEED;
}
}
void Spaceship::update(sf::Time deltaTime)
{
float radius = sprite_.getLocalBounds().width / 10.0;
if (position_.x + radius < 0.0f)
{
position_.x = static_cast<float>(WIDTH) + radius;
sprite_.setPosition(position_);
}
else if (position_.x - radius > static_cast<float>(WIDTH))
{
position_.x = 0.0f - radius;
sprite_.setPosition(position_);
}
if (position_.y + radius < 0.0f)
{
position_.y = static_cast<float>(HEIGHT) + radius;
sprite_.setPosition(position_);
}
else if (position_.y - radius > static_cast<float>(HEIGHT))
{
position_.y = 0.0f - radius;
sprite_.setPosition(position_);
}
sprite_.move(speed_);
sprite_.setRotation(angle_ + deltaTime.asSeconds());
position_ = sprite_.getPosition();
}