-
Notifications
You must be signed in to change notification settings - Fork 4
/
UFO.cpp
60 lines (46 loc) · 1.42 KB
/
UFO.cpp
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
#include "UFO.h"
#include "MoreInfo.h"
UFO::UFO(sf::Texture& texture, int speed) : Entity(texture), speed(speed) {
entity.setOrigin(texture.getSize().x / 2, texture.getSize().y / 2);
entity.setScale(1 * 1.5, 1 * 1.5);
}
void UFO::update(size_t timeNumber) {
sf::Vector2<float> movement(0.f, 0.f);
auto elapsed = std::chrono::duration<float>(std::chrono::steady_clock::now() - tp).count();
//Moves if the duration is larger than the timeNumber;
if (elapsed >= timeNumber && left == true) {
movement.x -= speed;
ufoAppear.setSound(UFO_FX, 50, false);
}
else if (elapsed >= timeNumber && left == false) {
movement.x += speed;
ufoAppear.setSound(UFO_FX, 50, false);
}
auto resetClock = std::chrono::steady_clock::now();
//Check if passes triggers
if (getX() <= 10 && left == true) {
setPosition(sf::Vector2<float>(-50, SKY_HEIGHT));
left = false;
tp = resetClock;
ufoAppear.stopSound();
}
if (getX() >= SCREEN_WIDTH - 10 && left == false) {
setPosition(sf::Vector2<float>(SCREEN_WIDTH + 50, SKY_HEIGHT));
left = true;
tp = resetClock;
ufoAppear.stopSound();
}
entity.move(movement);
isOnScreen();
}
void UFO::ufoCollisionSound() {
auto resetClock = std::chrono::steady_clock::now();
ufoAppear.setSound(UFO_FX, 50, false);
tp = resetClock;
}
bool UFO::isOnScreen() {
if (getX() <= SCREEN_WIDTH && getX() >= 0) {
return true;
}
return false;
}